Hi, need an urgent answer please,
i am reading a PWM signal using pulseIn() command and storing it in a variable (long duration)
and then i am comparing duration with HIGH in the if() statement, will it work for the condition to go true. code file is attached as well as code is published below also. Any improvements would be appreciated.
void loop() {
duration = pulseIn(rfid, HIGH);
if(duration == HIGH){ //also see analog or digital read //program only starts when arduino gets signal from the previous circuit
delay(5000); //wait for 5 sec before powering up the reciever
digitalWrite(power, HIGH); //power on the reciever
digitalWrite(ground, LOW);
long rfid_state1 = pulseIn(data1, HIGH);
if(rfid_state1 == HIGH){ //if tag1 is recieved rotate servo1 by 90 deg
myservo1.write(150);
delay(5000);
rfid_state1 = LOW;
}
long rfid_state2 = pulseIn(data2, HIGH);
if(rfid_state2 == HIGH){ //if tag2 is recieved rotate servo2 by 90 deg
myservo2.write(150);
delay(5000);
rfid_state2 = LOW;
}
long rfid_state3 = pulseIn(data3, HIGH);
if(rfid_state3 == HIGH){ //if tag3 is recieved rotate servo3 by 90 deg
myservo3.write(150);
delay(5000);
rfid_state3 = LOW;
}
You can do this but why would you want to? Why would you want to compare a time interval to a pin voltage level? Besides, HIGH is currently defined to be one but that could change.
Just compare to the time interval that you want. Use
#define comparisonValue 10
or something like that if you want your own definition that you have some control over.
By the way, delay(5000) occurs at least three times and each one wastes the considerable power of your Arduino for five seconds.
vaj4088:
You can do this but why would you want to? Why would you want to compare a time interval to a pin voltage level? Besides, HIGH is currently defined to be one but that could change.
Just compare to the time interval that you want. Use
#define comparisonValue 10
or something like that if you want your own definition that you have some control over.
By the way, delay(5000) occurs at least three times and each one wastes the considerable power of your Arduino for five seconds.
i got your point, the pwm signal was for running servo motors, now i want to take that signal and send it to arduino, once arduino reads that signal it would activate the receiver and would read the data pins and as per the condition give pwn signal to corresponding servo motor
i just simply want to check the condition, can i simply compare it with 0 to make the condition true if it is returning some value?
If pulseIn() returned some value, that value COULD be 0, if the pin never went HIGH before the timeout expired.
It still does not make sense to compare a time to a state. Oranges and chocolate cake are not comparable.
If you have some time that you want to compare the pulseIn() time to, then you can do that in an if statement. Just don't compare the pulseIn() time to some random value and expect anything meaningful to happen.