Newbie here. if someone else has already asked i would appreciate being sent to that thread but i havent seen one for my problem yet.
I'm trying to turn on a specific LED based on an input value received from a potentiometer. my problem is that no matter what value im receiving, two of my LEDs are very faintly blinking and sometimes two of them will stay on, despite the fact that im trying to have only one on at a time. Thank you for taking the time.
int readValue;
void setup()
{readValue = analogRead(A0);
pinMode(3,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
}
void loop() {
{readValue = analogRead(A0);} //read the potentiometer:
if(analogRead(A0) <=300) digitalWrite(3,HIGH);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
// this works OK, but the other two lights blink faintly
if (analogRead((A0)>300 && (A0)<= 500))digitalWrite(3,LOW);
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
//this line and the line underneath will leave 5 and 6 on at the same time
if (analogRead((A0)>600))digitalWrite(3,LOW);
digitalWrite(5,LOW);
digitalWrite(6,HIGH);}
that did stop output 5 and 6 from blinking faintly, but when i turn my potentiometer to any value above 300 both 5 and 6 turn on at the same time. new code looks like this.
int readValue;
void setup()
{readValue = analogRead(A0);
pinMode(3,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
}
void loop() {
{readValue = analogRead(A0);} //read the potentiometer:
if(analogRead(A0) <=300) {digitalWrite(3,HIGH);
digitalWrite(5,LOW);
digitalWrite(6,LOW);}
// this works OK, but the other two lights blink faintly
if (analogRead((A0)>300 && (A0)<= 500)){digitalWrite(3,LOW);
digitalWrite(5,HIGH);
digitalWrite(6,LOW);}
//this line and the line underneath will leave 5 and 6 on at the same time
if (analogRead((A0)>600)){digitalWrite(3,LOW);
digitalWrite(5,LOW);
digitalWrite(6,HIGH);}}
Hello
Is this a school assignment, isn´t it?
Take some time and study the IPO model to design and make a structured sketch. This will impress your teacher to give a A+++.
INPUT: analogRead per loop
PROCESSING: generate led pattern using SWITCH/CASE
OUTPUT: presentation of led pattern using leds
UPDATE: I've managed to make it work. Unfortunately, im not sure exactly what changed that seemed to do the trick. it worked after i changed several things at once so im not sure which one did it or if it was a combination of the all of them. I altered the way I had been doing () and i added (>0) to my very first conditional. I also changed the number range that i was working with. I Appreciate all the help you all were willing to offer. I'll attach the updated code so you all can see and maybe shed some light on the situation. I also intend to mark this thread as solved but i want to leave it up so you can see it as this is my first post and i dont know if it will disapear if i mark it solved. Thank you all again. I really do appreciate the time and effort.
int readValue=0;
int writeValue=0;
void setup()
{
readValue = analogRead(A0);
pinMode(3,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
Serial.begin(9600);
readValue=analogRead(A0);
writeValue=analogRead(A0);
}
void loop(){
{analogRead(A0);}
//experimenting with braces and parentheses because i don't understand those folks in the forum:
if ((analogRead(A0)<=100)&&(analogRead(A0)>0))
{
digitalWrite(3,HIGH);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
analogRead(A0);
} //code works fine up to here:
if ((analogRead(A0)>100)&& (analogRead(A0)<=300))
{
digitalWrite(3,LOW);
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
analogRead(A0);
}
if ((analogRead(A0)>300)&&(analogRead(A0)<1000))
{
digitalWrite(3,LOW);
digitalWrite(5,LOW);
digitalWrite(6,HIGH);
analogRead(A0);
}
}
Your first code was chock-a-block full of errors. Errors which nevertheless were valid code in some cases.
Just look at working examples or tutorials on basic syntax. The compiler is not going to look, see what you meant and fix those, as we have done here to help you along.
We all started knowing nothing, and have probably made similar errors and speak for myself, still do from time to time.