system
April 25, 2013, 8:32pm
1
Hello and thank you for your help.
Long story short, I'm going through the Arduino starter kit and have paused with the projects in the manual in attempts to ensure I'm "getting it".
I've attached a potentiometer to A0 and LED's to pins 1-3 in an attempt to light successive led's as the sensor value increases.
const int sensorPin = A0;
const float sensorValue = 0.0;
void setup (){
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
Serial.begin(9600);
}
void loop (){
int sensorValue = analogRead (A0);
Serial.println(sensorValue, DEC);
if(sensorValue = 0) {
digitalWrite (1, LOW);
digitalWrite (2, LOW);
digitalWrite (3, LOW);
}else if(sensorValue > 0 && sensorValue <= 341) {
digitalWrite (1, HIGH);
digitalWrite (2, LOW);
digitalWrite (3, LOW);
}else if(sensorValue > 341 && sensorValue < 682) {
digitalWrite (1, HIGH);
digitalWrite (2, HIGH);
digitalWrite (3, LOW);
}else if(sensorValue >= 682 && sensorValue <= 1023); {
digitalWrite (1, HIGH);
digitalWrite (2, HIGH);
digitalWrite (3, HIGH);
delay(1);
}
}
The potentiometer is running properly as I can see with the serial monitor, but all three LED's stay illuminated no matter the sensorValue.
Again, please be understanding that I am new and eager to learn and do not have the proficiency with coding yet. Be gentle. Thanks for your time!
system
April 25, 2013, 8:43pm
2
if(sensorValue = 0) {
Right there.
Always false.
Make it a comparison, not an assignment
if(sensorValue = 0) {
Needs to be:
if(sensorValue == 0) {
'=' is an assignment, '==' is equal to.
system
April 25, 2013, 8:54pm
4
Thank you both so much for your time and insight.
I will make the change immediately. Be well!
Arrch
April 25, 2013, 8:54pm
5
pinMode(1, OUTPUT);
...
Serial.begin(9600);
Serial communications use digital pins 0 and 1, so attaching anything to them can be problematic.
system
April 26, 2013, 12:09pm
6
I think that you might find it easier to see the logic of your program if you put each { on a new line and use Tools + Auto Format. I find that this is much more obvious than your code:
if(sensorValue = 0)
{
digitalWrite (1, LOW);
digitalWrite (2, LOW);
digitalWrite (3, LOW);
}
else if(sensorValue > 0 && sensorValue <= 341)
{
digitalWrite (1, HIGH);
digitalWrite (2, LOW);
digitalWrite (3, LOW);
}
else if(sensorValue > 341 && sensorValue < 682)
{
digitalWrite (1, HIGH);
digitalWrite (2, HIGH);
digitalWrite (3, LOW);
}
else if(sensorValue >= 682 && sensorValue <= 1023);
{
digitalWrite (1, HIGH);
digitalWrite (2, HIGH);
digitalWrite (3, HIGH);
delay(1);
}
It also makes it much more obvious when you do something dumb like add a semiccolon at the end of an if or else if statement, like you last one.
Finally, you could rearrange the code, to perform a lot fewer evaluations:
if(sensorValue >= 682)
{
}
else if(sensorValue >= 341)
{
}
else if(sensorValue > 0)
{
}
else
{
}
system
April 26, 2013, 11:16pm
7
I have taken all suggestions (using pins 2-4, using == instead of =, and fixed my "dumb" (thanks) mistake.
My potentiometer is reading via the serial monitor now which is an improvement but yet all three of my LED's stay illuminated no matter the value.
Any other suggestions would be appreciated.
Thank you -
system
April 26, 2013, 11:21pm
8
Downtownmjb:
Any other suggestions would be appreciated.
If you have a problem with your code, I suggest you post your code.
system
April 26, 2013, 11:24pm
9
Please scroll up Peter - I posted it originally.
The only things that have changed is a == instead of =, and the pins from 1-3 to 2-4.
system
April 26, 2013, 11:52pm
10
I've actually isolated the problem with this part of the code:
else if(sensorValue >= 682);
{
digitalWrite (2, HIGH);
digitalWrite (3, HIGH);
digitalWrite (4, HIGH);
I deleted this portion of code and uploaded sketch and the LED's light successively as the sensorValue increases. But once I add this code back in, all 3 LED's stay illuminated.
system
April 27, 2013, 12:01am
11
else if(sensorValue >= 682);
{
That semicolon is the problem.
-br
system
April 27, 2013, 2:08am
12
Downtownmjb:
Please scroll up Peter - I posted it originally.
But then you changed it, and didn't post the updated version. If you want help with your code, you need to post your actual code.
Downtownmjb:
Please scroll up Peter - I posted it originally.
Post your new code. Who knows what you actually did?
system
April 27, 2013, 12:06pm
14
It works perfectly now thanks to many of you guys help, thanks so much - I do appreciate it!
I apologize for not reposting my entire code with the minor changes I mentioned - I didn't want to be redundant with it on the thread.
But if that's the custom, I'll be all over it next time.
Thanks again!
system
April 28, 2013, 2:50am
15
It's the custom because typos make up about half of the debugging problems in code. You can describe your changes, but you can't guarantee that your actual code doesn't have a silly semicolon, or a period instead of a comma, or a missing capital, or a missing brace, without actually posting it. In addition, it speeds up visual debugging for the person helping you, as they don't have to mentally change your code, before identifying issues.
It's all part of the learning process. Glad you got your problems sorted out.