Question

In: Computer Science

Add to this arduino sketch such that you can add three externally-connected LEDs to your circuit...

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
}

Solutions

Expert Solution

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

  • We have to define 3 variable to store arduino pin number in which 3 leds are going to be connected(led1,led2,led3) and set them as output pins
  • Create another varaible which store the pin at which pot is connected and set them as input pin
  • In loop() function ,analog read the pot  pin and store it in variable val
  • create 3 if statement which check 3 conditions mentioned in questions (here we have to use Logical AND operator( &&) .This operator return TRUE only if  two conditions mentioned are true)  
  • In the body of each if statements turn on and off apropriate LEDs
  • introduce some delay in each if body

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
}
}


Related Solutions

Build a circuit for arcade game. You have 3 LEDS and 1 button. the LEDs should...
Build a circuit for arcade game. You have 3 LEDS and 1 button. the LEDs should recycle through 000,100,110,111,011, 001(back to 000). The user needs to hit the button during the 111 cycle 3 times in a row to win. The win is shown as an LED which is HIGH if the user won the last attempt of 3 presses. The win LED will be LOW otherwise
For three resistors, R1, R2, and R3, sketch the schematic for the circuit if all three...
For three resistors, R1, R2, and R3, sketch the schematic for the circuit if all three are connected in parallel and determine in general the equivalent resistance. For three resistors, R1, R2, and R3, sketch the schematic for the circuit if all three are connected in series and determine in general the equivalent resistance. What will the equivalent resistance be for three 300 Ohm resistors connected in parallel?
Provide a circuit diagram and explanation for controlling a linear actuator using an Arduino. How can...
Provide a circuit diagram and explanation for controlling a linear actuator using an Arduino. How can you provide more power to the motor than Arduino can handle?
Three identical light bulbs are connected to a 30V battery in a circuit. Bulb A is...
Three identical light bulbs are connected to a 30V battery in a circuit. Bulb A is on main loop and closest to battery. Bulbs B and C are in parallel and the junction is after Bulb A S1 is in the loop with bulb B and S2 is in loop with bulb C Initially switch 1 is closed and switch 2 is opened. When switch 2 is closed what happens to the brightness of bulbs A,B and C Please explain
1.A circuit of resistors connected in series is plugged in a 120 V outlet. What can...
1.A circuit of resistors connected in series is plugged in a 120 V outlet. What can you tell about the voltage on each of resistor and current in each resistor? 2.A circuit of capacitors connected in parallel is plugged in a 120 V outlet. What can you tell about the voltage on each capacitor and current in each capacitor?
Question 5 a) Draw the circuit of a Three-phase bridge inverter using a Y-connected load and...
Question 5 a) Draw the circuit of a Three-phase bridge inverter using a Y-connected load and explain its operation using 180 degree conduction clearly showing the gating sequence and the corresponding output line voltages for each phase. (10) b) For the Y-connected load determine the line to neutral voltages for the three modes of operations. Draw the equivalent circuits for each mode and then draw the phase voltage waveforms for the 180-degree conduction. (7)
One light bulb is connected to a battery. Would the brightness change as you add another...
One light bulb is connected to a battery. Would the brightness change as you add another light bulb in series with the first light bulb? Explain.
How can you add more foods rich in essential minerals to your meals?
How can you add more foods rich in essential minerals to your meals?
1. Sketch a connected 2-dimensional complex with three 2-cells, 8 1- cells, and 5 0-cells. Draw...
1. Sketch a connected 2-dimensional complex with three 2-cells, 8 1- cells, and 5 0-cells. Draw three examples of a connected 1-dimensional complex with 7 0-cells and 6 1-cells. Sketch a connected 1-dimensional complex with 7 0-cells and 7 1-cells. Sketch a connected 1-dimensional complex with 7 0-cells and 8 1-cells. Is it possible to construct a connected 1-dimensional complex with 8 0-cells and 6 1-cells? Explain.
please rephrase/ rewrite this paragraph and you can add your own ideas The idea of using...
please rephrase/ rewrite this paragraph and you can add your own ideas The idea of using the sun’s energy was thought of the late 1870 when two American scienists became interested in the work of an electrican named willouhby smith who was testing underwater telegraph lines for faults using a material called selenium. Smith realized that electricity travelled through selenium well when it was in light, but it didn’t in the absence of light. It was not long before the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT