Okay, I’m not really sure how to implement a debouncer, but I have a different problem. First of all, here is my current clumsy mess of code in its entirety:
#include <stdio.h>
// ------------------------------------------------------------
// SparkFun LCD display API
// ------------------------------------------------------------
extern void LCDOpen (); // open the LCD interface
extern void LCDClear (); // clear the screen
extern void LCDText ( int xPos , int yPos , char * message ); // write text at a location
extern void LCDSetSplash (); // save current screen as the splash screen
extern void LDCSetBright ( int perCent ); // set the display brightness in percent
// ------------------------------------------------------------
// LCDopen - open the LCD display on the serial interface
// ------------------------------------------------------------
void LCDOpen()
{
// open the serial connection
Serial.begin ( 9600 );
// clear the screen
LCDClear();
}
// ------------------------------------------------------------
// LCDclear- clear the LCD display
// ------------------------------------------------------------
void LCDClear()
{
// send the screen clear command
Serial.print ( 0xFE , BYTE ); // special display command
Serial.print ( 0x01 , BYTE ); // clear screen
}
// ------------------------------------------------------------
// LCDText - draw a text message to the display at specified position
// ------------------------------------------------------------
void LCDText ( int xPos , int yPos , char * message )
{
int crsPos;
// compute the cursor position for the device
switch ( yPos )
{
case 0 : crsPos = 0x00; break;
case 1 : crsPos = 0x40; break;
case 2 : crsPos = 0x10; break;
case 3 : crsPos = 0x50; break;
default : crsPos = 0x0; break;
}
crsPos += xPos; crsPos |= 0x80;
// position the cursor
Serial.print ( 0xFE , BYTE ); // special display command
Serial.print ( crsPos , BYTE ); // set the cursor position
// print the message
Serial.print ( message );
}
// ------------------------------------------------------------
// LCDSetSplash - save the current screen as the splash screen to EEPROM
// ------------------------------------------------------------
void LCDSetSplash()
{
// send commands to save the current
Serial.print ( 0x7C , BYTE ); // special EEPROM command
Serial.print ( 0x0A , BYTE ); // save screen as splash
// confirm to the display
delay ( 3000 ); // give the EEPROM time to write
LCDClear(); // clear the display
LCDText ( 0 , 0 , “splash set” ); // confirmation message
delay ( 3000 ); // let the user see it
}
// ------------------------------------------------------------
// LDCSetBright - set the display brightness in percent
// ------------------------------------------------------------
void LCDSetBright ( int perCent )
{
// compute backlight code from percentage
int blCode = 128 + ( 157 - 128 ) * perCent / 100;
// send command
Serial.print ( 0x7C , BYTE ); // special EEPROM command
Serial.print ( blCode , BYTE ); // send the brightness value
// confirm to the display
char msg[40];
sprintf ( msg , “brt %d%%” , perCent , blCode );
delay ( 3000 ); // give the EEPROM time to write
LCDClear(); // clear the display
LCDText ( 0 , 0 , msg ); // confirmation message
delay ( 3000 ); // let the user see it
}
// ------------------------------------------------------------
//
// ------------------------------------------------------------
int a = 300;
int b = 400;
int c = 600;
int x = 0;
int switch1 = 3;
int switch2 = 4;
int switch3 = 5;
int led1 = 13;
int led2 = 14;
int led3 = 15;
int val1 = 0; // variable for reading the pin status
int val2 = 0; // variable for reading the pin status
int val3 = 0; // variable for reading the pin status
void setup ()
{
pinMode(switch1, INPUT);
pinMode(switch2, INPUT);
pinMode(switch3, INPUT);
digitalWrite(switch1, LOW);
digitalWrite(switch2, LOW);
digitalWrite(switch3, LOW);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
// open the display
LCDOpen();
// set the splash screen and brigthness ( need this only once to configure )
// LCDText ( 0 , 0 , “in case of fire” );
// LCDText ( 0 , 1 , " break glass " );
// delay (300);
// LCDSetSplash();
// LCDSetBright ( 80 );
}
void loop ()
{
val1 = digitalRead(switch1); // read input value
if (val1 == HIGH) { // check if the input is HIGH
digitalWrite(led1, LOW); // turn LED OFF
x = x + a;
delay (1000);
}
else {
digitalWrite(led1, HIGH); // turn LED ON
x = x - a;
delay (1000);
}
val2 = digitalRead(switch2); // read input value
if (val2 == HIGH) { // check if the input is HIGH
digitalWrite(led2, LOW); // turn LED OFF
x = x + b;
}
else {
digitalWrite(led2, HIGH); // turn LED ON
x = x - b;
}
val3 = digitalRead(switch3); // read input value
if (val3 == HIGH) { // check if the input is HIGH
digitalWrite(led3, LOW); // turn LED OFF
x = x + c;
}
else {
digitalWrite(led3, HIGH); // turn LED ON
x = x - c;
}
// clear the screen
LCDClear();
// draw message to the display
LCDText ( 0 , 0 , "X used: " );
Serial.print (x);
// don’t overrun the display
delay ( 100 );
}
Okay, as you can see, the pull up/down resistors are the ones built in to the atmega644p that I am using on a breadboard. My problem is that as it loops again and again, it adds and subtracts and adds over and over again, as it sees each switch on and off. How would I go about fixing this? Also, I apparently need a debouncer or something.