int i; // counter variable
int j; // pgm outputs codes 00000 to 00111 in portd 2-4 bits
int count=0;
int SW1=8; //Set SW1 to Pin 8
int SwState= 0;
void setup(){
DDRD = DDRD | B00011100; // set direction bits for pins 2 to 4, leave 0 and 1 untouched
//(xx | 00 = xx) same as pinMode(pin, OUTPUT) for pins 2 to 4
Serial.begin(9600);
pinMode(SW1,INPUT);
digitalWrite(SW1, HIGH); //enable pullup. Switch connected to ground
}
void loop(){
while (digitalRead(SW1)==LOW) // check if switch is released
{
count++;
if (count>7){ //restart after 7
count=0;
}
// 3 bits manipulated 0 to 7
PORTD = PORTD & B11100011; // clear out bits 2 -4, leave pins 0 and 1 untouched (xx & 11 = xx)
j = (count << 2); // shift variable up to pins 2 - 7 - to avoid pins 0 and 1
PORTD = PORTD | j; // combine the port information with the new information for LED pins
Serial.print("count=");
Serial.println(count);
Serial.print("PORTD in BINary=");
Serial.println(PORTD, BIN); // debug to show masking
delay(200);
}
delay(200);
}