Hey, I'm working on project where I have to on the relays based on analog 0-5 input.
e.g if 0-1V Relay1
1-2 then Relay2
2-3V Relay3
3-5V Relay4
everything is good if I go through sequence gradually from 0-5, but if I sudden change value of analog input like from 0 to 4 volt then sequence change from 1-2-3-4 that is a problem I want it directly to go to sequence 4.
Any idea guys as I'm so confused.
Sounds like a problem in the sketch that you did not show. Perhaps if you showed the sketch we would be able to explain the source of the undesired behavior.
Here it is
int val=0;
float volt=0;
int relay1=2;
int relay2=3;
int relay3=4;
int inPin=0;
int inPin5=5;
int buttonstate=0;
void setup() {
Serial.begin(19200);
pinMode(inPin5, INPUT);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
while(digitalRead(inPin5)==HIGH and millis()<5000);
}
void loop() {
val=analogRead(inPin);
volt= val*(5/1023.0);
if(volt<2)
{ digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
}
else
if(volt>2 && volt<2.2)
{ digitalWrite(relay1, HIGH);
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
}
else
if(volt>2.2 && volt<2.65)
{ digitalWrite(relay1,HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, LOW);
}
else
if(volt>2.65 && volt<3.26)
{ digitalWrite(relay1,HIGH);
digitalWrite(relay2, LOW);
digitalWrite(relay3, HIGH);
}
else
if(volt>3.26 && volt<4)
{ digitalWrite(relay1,HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
}
else
if(volt>4 && volt<4.75)
{digitalWrite(relay1,LOW);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
}
Serial.println(volt);
Serial.println(val);
delay(500);
}
That 'sketch' doesn't even come close to compiling. You left out all of the variable declarations and the entire setup() function. On the bright side, it appears that the only way you will get the intermediate relays to close is to change the input very slowly. Where is the input voltage coming from?
/Users/john/Documents/Arduino/sketch_mar26a/sketch_mar26a.ino: In function 'void loop()':
sketch_mar26a:2: error: 'val' was not declared in this scope
val = analogRead(inPin);
^
sketch_mar26a:2: error: 'inPin' was not declared in this scope
val = analogRead(inPin);
^
sketch_mar26a:3: error: 'volt' was not declared in this scope
volt = val * (5 / 1023.0);
^
sketch_mar26a:5: error: 'relay1' was not declared in this scope
{ digitalWrite(relay1, LOW);
^
sketch_mar26a:6: error: 'relay2' was not declared in this scope
digitalWrite(relay2, LOW);
^
sketch_mar26a:7: error: 'relay3' was not declared in this scope
digitalWrite(relay3, LOW);
^
sketch_mar26a:10: error: 'relay1' was not declared in this scope
{ digitalWrite(relay1, HIGH);
^
sketch_mar26a:11: error: 'relay2' was not declared in this scope
digitalWrite(relay2, LOW);
^
sketch_mar26a:12: error: 'relay3' was not declared in this scope
digitalWrite(relay3, LOW);
^
sketch_mar26a:15: error: 'relay1' was not declared in this scope
{ digitalWrite(relay1, HIGH);
^
sketch_mar26a:16: error: 'relay2' was not declared in this scope
digitalWrite(relay2, HIGH);
^
sketch_mar26a:17: error: 'relay3' was not declared in this scope
digitalWrite(relay3, LOW);
^
sketch_mar26a:20: error: 'relay1' was not declared in this scope
{ digitalWrite(relay1, HIGH);
^
sketch_mar26a:21: error: 'relay2' was not declared in this scope
digitalWrite(relay2, LOW);
^
sketch_mar26a:22: error: 'relay3' was not declared in this scope
digitalWrite(relay3, HIGH);
^
sketch_mar26a:25: error: 'relay1' was not declared in this scope
{ digitalWrite(relay1, HIGH);
^
sketch_mar26a:26: error: 'relay2' was not declared in this scope
digitalWrite(relay2, HIGH);
^
sketch_mar26a:27: error: 'relay3' was not declared in this scope
digitalWrite(relay3, HIGH);
^
sketch_mar26a:30: error: 'relay1' was not declared in this scope
{ digitalWrite(relay1, LOW);
^
sketch_mar26a:31: error: 'relay2' was not declared in this scope
digitalWrite(relay2, HIGH);
^
sketch_mar26a:32: error: 'relay3' was not declared in this scope
digitalWrite(relay3, HIGH);
^
sketch_mar26a:36: error: expected '}' at end of input
delay(500);
^
exit status 1
'val' was not declared in this scope
While you are at it please read this:-
How to use this forum
Because your post is breaking the rules about posting code.
Use the code tags when you post ALL your code like the forum rules requests.
OK, please look at full code.
johnwasser:
That 'sketch' doesn't even come close to compiling. You left out all of the variable declarations and the entire setup() function. On the bright side, it appears that the only way you will get the intermediate relays to close is to change the input very slowly. Where is the input voltage coming from?
I,ve posted full code but how to slow down input ADC.
mohit-singh:
OK, please look at full code.
No. It is not in code tags like the instructions request.
Grumpy_Mike:
No. It is not in code tags like the instructions request.
I've edited the code in sane post.
mohit-singh:
I've edited the code in sane post.
No idea what that means but still no code tags. If you go to the doctors and he says where does it hurt do you cooperated with him or do you just say "guess"?
You are supposed to post it like this:
int val = 0;
float volt = 0;
const int relay1 = 2;
const int relay2 = 3;
const int relay3 = 4;
const int inPin = A0;
const int inPin5 = 5;
int buttonstate = 0;
void setup() {
Serial.begin(19200);
pinMode(inPin5, INPUT);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
while (digitalRead(inPin5) == HIGH and millis() < 5000);
}
void loop() {
val = analogRead(inPin);
volt = val * (5 / 1023.0);
if (volt < 2)
{ digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
}
else if (volt > 2 && volt < 2.2)
{ digitalWrite(relay1, HIGH);
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
}
else if (volt > 2.2 && volt < 2.65)
{ digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, LOW);
}
else if (volt > 2.65 && volt < 3.26)
{ digitalWrite(relay1, HIGH);
digitalWrite(relay2, LOW);
digitalWrite(relay3, HIGH);
}
else if (volt > 3.26 && volt < 4)
{ digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
}
else if (volt > 4 && volt < 4.75)
{ digitalWrite(relay1, LOW);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
}
Serial.println(volt);
Serial.println(val);
delay(500);
}
The sketch now compiles but the problem you reported does not seem to be something caused by the sketch so I suspect the hardware (which you have not described) is at fault.
but the problem you reported does not seem to be something caused by the sketch
I beg to differ.
The waking half second delay ensures that any intermittent value read will persist for half a second before it is read again thus firing an intermittent relay operation. I would suggest only launching into that long sequence of if statements once a reading had settled. Also remove all the else lines and just go with the ifs.
something like this:-
int val2;
void loop() {
val = analogRead(inPin);
val2 = analogRead(inPin);
while( val != val2 ){
val = analogRead(inPin);
val2 = analogRead(inPin);
}
volt = val * (5 / 1023.0);
if (volt < 2)
// ............. and so on