- Published on
Level 3 Solutions
Table of Contents
Assignment 1
Make a count up timer from 0 to 7. Represent the numbers in 3-bit binary digits using 3 LEDs. On running your code the LEDs should light up to represent 1 and switch off to represent 0. You must also include a push-button to reset the counter whenever you want.
void setup()
{
//Initialize all pins
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, INPUT);
Serial.begin(9600);
}
void loop() {
reset();
for(int i = 0; i < 8; ++i) //i loops from 0-7
{
//Each LED assigned a bit of binary value of i
digitalWrite( 5, ((i >> 2) % 2) == 1);
digitalWrite( 4, ((i >> 1) % 2) == 1);
digitalWrite( 3, ((i >> 0) % 2) == 1);
delay(1000);
Serial.println(digitalRead(6));
if (digitalRead(6)==1)
{
i=9;
}
}
}
//Reset function
void reset()
{
//makes all LEDs off
digitalWrite( 5, 0);
digitalWrite( 4, 0);
digitalWrite( 3, 0);
}
Have a look at our binary couter!
-->