int i; // counter variable --pgm outputs codes 00000 to 00111 in portd 2-4 bits
int count=0;
int SW1=5; //Set SW1 to Pin 5
int SW2=6; //Set SW2 to Pin 6
void setup(){
DDRB = DDRB | B00111111; // set direction bits 0 to 7,
//(xx | 00 = xx) same as pinMode(pin, OUTPUT) for pins 2 to 4
Serial.begin(9600);
Serial.println ("Question 5");
Serial.println ("Current Limiting Resistor = 5-3/0.04=75, 5-3/0.01=200");
Serial.println("Therefore, using a 220 ohm resistor so bright enough to use and don't exceed 200mA maximum");
Serial.println ("Yellow switch = Up Counter, Green Switch = Down Counter");
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>7)
{
count=0;
}
// 3 bits manipulated 0 to 7
PORTB = PORTB & B00000000 ; // clear out bits 0 - 7,
PORTB = PORTB | count; // combine the port information with the new information for LED pins
Serial.print("PORTB in Binary=");
Serial.print(PORTB, BIN); // debug to show masking
Serial.print(", Count=");
Serial.println(count);
delay(200);
}
while (digitalRead(SW2)==LOW) // check if COUNT_DOWN switch is released
{
count--;
if (count<0) // Restart after 7
{
count=7;
}
// 3 bits manipulated 0 to 7
PORTB = PORTB & B00000000 ; // clear out bits 0 - 7,
PORTB = PORTB | count; // combine the port information with the new information for LED pins
Serial.print("PORTB in Binary=");
Serial.print(PORTB, BIN); // debug to show masking
Serial.print(", Count=");
Serial.println(count);
delay(200);
}
delay(200);
}