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?
You are considering three investments to add to your portfolio. The first is a bond that...
You are considering three investments to add to your portfolio. The first is a bond that is selling in the market at $1,100. The bond has a $1,000 par value, pays interest at 13 percent, and is scheduled to mature in 15 years. For bonds has a high risk rating (junk bond) and therefore you believe that a 14 percent rate of return should be required. The second investment that you are analysing is a preferred stock ($100 par value)...
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)
Three equal impedances, 60 + j30 Ω each, are delta-connected to a 230-V rms, three-phase circuit....
Three equal impedances, 60 + j30 Ω each, are delta-connected to a 230-V rms, three-phase circuit. Another three equal impedances, 40 + j10 Ω each, are wye-connected across the same circuit at the same points. Determine: (a) the line current (b) the total complex power supplied to the two loads (c) the power factor of the two loads combined
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?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT