In: Computer Science
Add to this arduino sketch such that you can add three externally-connected LEDs to your circuit board. Modify this code such that the first LED turns on if the ADC output value is between 0 and 340, the second LED turns on if the ADC output value is between 341 and 680, and the third LED turns on if the ADC output value is above 680
int potpin=0;// initialize analog pin 0 int ledpin=13;// initialize digital pin 13 int val=0;// define val, assign initial value 0 void setup() { pinMode(ledpin,OUTPUT);// set digital pin as “output” Serial.begin(9600);// set baud rate at 9600 } void loop() { digitalWrite(ledpin,HIGH);// turn on the LED on pin 13 delay(50);// wait for 0.05 second digitalWrite(ledpin,LOW);// turn off the LED on pin 13 delay(50);// wait for 0.05 second val=analogRead(potpin);// read the analog value of analog pin 0, and assign it to val Serial.println(val);// display val’s value }
Here our task is to create an arduino sketch to turn on and off 3 LEDs according to the ADC input values
Things to consider before coding
CODE
CURCUIT DIAGRAM
text version of the code given below
int potpin=A0;// initialize analog pin 0
int led1=13;// initialize digital pin 13
int led2=12;// initialize digital pin 13
int led3=11;// initialize digital pin 13
int val=0;// define val, assign initial value 0
void setup()
{
pinMode(led1,OUTPUT);// set led1 pin as “output”
pinMode(led2,OUTPUT);// set led2 pin as “output”
pinMode(led3,OUTPUT);// set led3 pin as “output”
Serial.begin(9600);// set baud rate at 9600
}
void loop()
{
val=analogRead(potpin);// read the analog value of analog pin 0,
and assign it to val
if ( val>0 && val <340){ // checking conditions
digitalWrite(led1,HIGH);// turn on the LED1 on pin 13
digitalWrite(led2,LOW);// turn off the LED2 on pin 12
digitalWrite(led3,LOW);// turn off the LED3 on pin 11
delay(50); //wait for 0.05 seconds
}
if ( val >340 && val<680 ) { // checking
conditions
digitalWrite(led1,HIGH);// turn on the LED1 on pin 13
digitalWrite(led2,HIGH);// turn on the LED2 on pin 12
digitalWrite(led2,LOW);// turn off the LED3 on pin 11
delay(50); //wait for 0.05 seconds
}
if (val >=680 ){ // checking conditions
digitalWrite(led1,HIGH);// turn on the LED1 on pin 13
digitalWrite(led2,HIGH);// turn on the LED2 on pin 12
digitalWrite(led2,HIGH);// turn on the LED3 on pin 11
delay(50); //wait for 0.05 seconds
}
}