In: Computer Science
Arduino Project -
Part 1
• Connect 3 LEDs on 3 digital pins
• Blink LEDs at: – 1 at 1 Hz – 2 at 4 Hz – 3 at 5 Hz
• Blink LEDs at: – 1 Hz – 3 Hz – 5 Hz 36
What to submit
• 2 Arduino files – one for 1,4,5 Hz blinking – one for 1,3,5 Hz blinking
ANSWER:
CODE TEXT
/*CODE 1-
Blink LEDs at: – 1 at 1 Hz – 2 at 4 Hz – 3 at 5 Hz*/
// Pin 2, 3, 4 has an LED connected
// defining led pins
int led1 = 2;
int led1 = 3;
int led1 = 4;
// defining the setup routine
void setup() {
// initialize the digital pins as an output.
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}
// defining the loop routine
void loop() {
/*Blinking Logic
For 1Hz- LED becomes High and LOW once in a second so 500ms for
every state
For 4Hz- LED becomes High and LOW four times in a second so 125ms
for every state
For 5Hz- LED becomes High and LOW five times in a second so 100ms
for every state
As all three proccess has to be implemented synchronously in a
second, defining a algo,
HCF of 100, 125 and 500 is => 25
if 25*(Multiple of 4) => LED3 State changes
if 25*(Multiple of 5) => LED2 State changes
if 25*(Multiple of 20) => LED1 State changes
*/
// initially turning all LEDs HIGH
digitalWrite(led1, HIGH); // LED1 HIGH
digitalWrite(led2, HIGH); // LED2 HIGH
digitalWrite(led3, HIGH); // LED3 HIGH
// for loop 40 times with delay of 25 ms to complete a second
for(i =1;i<=40;i++){
// delaying for 25 ms
delay(25);
// checking for counter multiples
// for LED1
if(i%20==0){
// changing state of LED1
digitalWrite(led1, !digitalRead(led1));
}
// for LED2
if(i%5==0){
// changing state of LED2
digitalWrite(led2, !digitalRead(led2));
}
// for LED3
if(i%4==0){
// changing state of LED3
digitalWrite(led3, !digitalRead(led3));
}
}
}
/*CODE 2-
Blink LEDs at: – 1 at 1 Hz – 2 at 3 Hz – 3 at 5 Hz*/
// Pin 2, 3, 4 has an LED connected
// defining led pins
int led1 = 2;
int led1 = 3;
int led1 = 4;
// defining the setup routine
void setup() {
// initialize the digital pins as an output.
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}
// defining the loop routine
void loop() {
/*Blinking Logic
For 1Hz- LED becomes High and LOW once in a second so 500ms for
every state
For 3Hz- LED becomes High and LOW four times in a second so 166ms
for every state
For 5Hz- LED becomes High and LOW five times in a second so 100ms
for every state
As all three proccess has to be implemented synchronously in a
second, defining a algo,
HCF of 100, 166 and 500 is => 2
if 2*(Multiple of 50) => LED3 State changes
if 2*(Multiple of 83) => LED2 State changes
if 2*(Multiple of 250) => LED1 State changes
*/
// initially turning all LEDs HIGH
digitalWrite(led1, HIGH); // LED1 HIGH
digitalWrite(led2, HIGH); // LED2 HIGH
digitalWrite(led3, HIGH); // LED3 HIGH
// for loop 500 times with delay of 2 ms to complete a second
for(i =1;i<=500;i++){
// delaying for 2 ms
delay(2);
// checking for counter multiples
// for LED1
if(i%250==0){
// changing state of LED1
digitalWrite(led1, !digitalRead(led1));
}
// for LED2
if(i%83==0){
// changing state of LED2
digitalWrite(led2, !digitalRead(led2));
}
// for LED3
if(i%50==0){
// changing state of LED3
digitalWrite(led3, !digitalRead(led3));
}
}
}
CODE IMAGE