hello!
a week ago i bought an arduino starter set, trying to understand and master the arduino.
i tried a few things from the book, but im also trying to build my own creations.
the thing i tried to build is a simple car direction indicator.
2 leds are being controlled by a potentiometer.
scale 0-1023,
0-340 = turn left led on
341-682 = no led
683-1023 = right led
the problem is, that the current situation is as following:
0-340 = left led on 341-682 = both leds on
683-1023 right led on
i dont understand why this is happening?
this is the current code:
int Lled = 2;
int Rled = 3;
int potMeter = A0;
int val = 0;
void setup() {
// put your setup code here, to run once:
pinMode (Lled, OUTPUT);
pinMode (Rled, OUTPUT);
pinMode (potMeter, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
val = analogRead(potMeter);
Serial.println(val);
if(val<341)
{
digitalWrite(Lled,HIGH);
digitalWrite(Rled,LOW);
}
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(2, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(2, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
i made the left led blink with this code, this appears to work normal
What happens if the the value is 341,342,681 or 682?
It would make sense to change all your "<" & ">" to "<=" (less than or equal to) and ">=" (greater than or equal to) so you can account for any value within your range.
You can also shorten your code some by using && (and). Such as
if(val >= 341 && val <= 681){
digitalWrite(Rled,LOW);
digitalWrite(Lled,LOW);
}
EDIT::: Wow, I need to learn to tap faster on this cell phone. Others already mentioned the non-included numbers. But the use of && can still be beneficial.
If you connect one leg to ground (the proper one, of course) and one leg to the digital pin, setting the pin HIGH will turn the LED on. Setting the pin LOW will turn the LED off.
If you connect one leg to 5V and one leg to the digital pin, setting the pin LOW will turn the LED on. Setting the pin HIGH will turn the LED off.
What I wanted you to do was print something in the blink sketch, showing ON and OFF in the Serial Monitor. If you see OFF when the LED is on, and ON when the LED is off, that is a clear indication that the LED is wired incorrectly.
With just yellow wires, too long, and a poor camera angle, it is impossible to tell just how your LEDs are wired.