For some reason the Green LED does the red LED's job. and the Red LED doesn't turn on or off at all.
I'm hoping I have the variables right
int led1 = 10; //red Attach red LED to pin 10
int led2 = 12; // green Attach Green LED to pin 12
void setup() {
Serial.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop() {
unsigned int d1 = analogRead(A0)*10; // This is what I'm not too sure about. Take voltage read at A0 and multiply it by ten to assign that number as "d1"
delay(100); // Wait 10 milliseconds
unsigned int d2= analogRead(A0)*10; // Take the following Voltage and multiply it by ten to assign that number as "d2".
int v1 =(d1-d2) * 100; // put d1 and d2 into equation to get a value and assign that value to v1
int s1 = d2 * 0.02; // take d2 and put in equation to find value of "s1"
if (v1 <= s1) { // if v1 is equal to or less than than s1, turn led1 on and led2 off
digitalWrite(led1, HIGH); //red
digitalWrite(led2, LOW); //red
}
else if (v1 > s1){ // if v1 is greater than s1, turn led2 on and led off
digitalWrite(led2, HIGH); //green
digitalWrite(led1, LOW); //red
}
}
// the entire thing should go in a loop so the leds are constantly adjusting.
What do you see that is wrong? Please help!
MODERATOR: [code] ... [/code]
tags added
Please use code tags like this
int led1 = 10; //red Attach red LED to pin 10
int led2 = 12; // green Attach Green LED to pin 12
void setup() {
Serial.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop() {
unsigned int d1 = analogRead(A0)*10; // This is what I'm not too sure about. Take voltage read at A0 and multiply it by ten to assign that number as "d1"
delay(100); // Wait 10 milliseconds
unsigned int d2= analogRead(A0)*10; // Take the following Voltage and multiply it by ten to assign that number as "d2".
int v1 =(d1-d2) * 100; // put d1 and d2 into equation to get a value and assign that value to v1
int s1 = d2 * 0.02; // take d2 and put in equation to find value of "s1"
if (v1 <= s1) { // if v1 is equal to or less than than s1, turn led1 on and led2 off
digitalWrite(led1, HIGH); //red
digitalWrite(led2, LOW); //red
}
else if (v1 > s1){ // if v1 is greater than s1, turn led2 on and led off
digitalWrite(led2, HIGH); //green
digitalWrite(led1, LOW); //red
}
}
This isnt the cause of the problem but I suspect you don't need the if after the else
if (v1 <= s1) { // if v1 is equal to or less than than s1, turn led1 on and led2 off
digitalWrite(led1, HIGH); //red
digitalWrite(led2, LOW); //red
}
else { // if v1 is greater than s1, turn led2 on and led off
digitalWrite(led2, HIGH); //green
digitalWrite(led1, LOW); //red
}
would be fine
Re: LEDS the wrong way round.
Could be a wiring problem
Make sure you have teh LEDS attached to he pins you think they are attached to
Also...
Have you wired the LED from the Pin to GND or Pin to VCC
Pin to VCC will cause the effect you are describing.
holy cow Watson, someone stole our tent !
I missed in initialization of A0
and void setup of the input pin for A0
I would recommend you add serial outputs to see what is happening on A0
if there is no change in the time elapsed then there will not be movement,
why bother with *10 ?
if A0 = 1 then 100
d1 and d2 can only increment in steps of 10.
you can have s1 = 110
d2-10010
then have d1-d2 = -90*100, always in steps of 10
the only actual calculation you have is to determine which reading is higher.
without knowing your input a simple,
if d1>d2 logic would do the same thing.
explain that you have a constantly changing input on A0 and that all the rest of the program is based on the constant change. a flow meter or some such.
people in the other thread seemed to think it was a manual pot or some such. that is why they say to add a switch.
thinking about the timing of the readings.
d2 is 100 after d1, but d1 is one scan after d2
this would have them very close.
also, the use of delay is a blocking function all programming stops until the delay is timed out.
the better way is to use a timer.
If (millis()-interval1>100){
d1 = (analogRead,A0);
Interval2=millis();
}
If (millis()-interval2>100{
d2-(analogRead,A0);
Interval1=millis();
this would keep the readings from A0 separated by the interval of 100 and allow the rest of your program to run properly.
set interval1 in the setup so that it will be less than millis() when the program starts so it the first IF statement will be true the first time through the program.
Nothing to do with your problem but if you are going to give LEDs names then they might as well be meaningful. So rather than
int led1 = 10; //red Attach red LED to pin 10
int led2 = 12; // green Attach Green LED to pin 12
perhaps
const byte redLed = 10;
const byte greenLed = 12;
There is no real need for comments stating the obvious and you can save some memory along the way.