int i; // counter variable
int count=0;
int SW1=3; //Set SW1 to Pin 3
int SW2=4; //Set SW2 to Pin 4
void setup(){
DDRB = DDRB | B00001111; // set direction bits for pins 8 to 10
Serial.begin(9600);
pinMode(SW1,INPUT);
pinMode(SW2,INPUT);
digitalWrite(SW1, HIGH); //enable pullup. Switch connected to ground
digitalWrite(SW2, HIGH); //enable pullup. Switch connected to ground
}
void loop(){
while (digitalRead(SW1)==LOW) // check if COUNT_UP switch is released
{
count++;
if (count>15){ //restart after 7
count=0;
}
// 3 bits manipulated 0 to 7
PORTB = PORTB & B11110000; // clear out bits 2 -4, leave pins 0 and 1 untouched (xx & 11 = xx)
PORTB = PORTB | count; // combine the port information with the new information for LED pins
Serial.print("count=");
Serial.println(count);
Serial.print("PORTB in BINary=");
Serial.println(PORTB, BIN); // debug to show masking
delay(200);
}
while (digitalRead(SW2)==LOW) // check if COUNT_DOWN switch is released
{
count--;
if (count<0){ //restart after 7
count=15;
}
// 3 bits manipulated 0 to 7
PORTB = PORTB & B11110000; // clear out bits 2 -4, leave pins 0 and 1 untouched (xx & 11 = xx)
PORTB = PORTB | count; // combine the port information with the new information for LED pins
Serial.print("count=");
Serial.println(count);
Serial.print("PORTB in BINary=");
Serial.println(PORTB, BIN); // debug to show masking
delay(200);
}
delay(200);
}