I am currently working on a project that involves steering wheels, and I would like a way for Arduino to wait for a change in value. A potentiometer is used and I would like a program to run (say, like lights blinking ) until there is a change in value of the potentiometer ( like an increase in 300).
Thanks in advance.
EDIT I realized that I did not give enough information about what I want, so here is the actual task I am trying to achieve :
The lights goes on when :
Button is pressed
The lights goes off when :
The button is pressed again
or
Another button is pressed
or
Potentiometer increases or (only one of it) decreases by 300
Plus I didn't mention that the value of the potentiometer will be constantly changing so the old value for the potentiometer should be the one right after the button press.
Soooo sorry for the inconvenience caused. ( I am really bad at programming)
3 Answers 3
int oldValue;
void setup()
{
oldValue = analogRead(A1);
}
void loop()
{
int value = analogRead(A1);
while( abs(value-oldValue)<300 )
delay(1);
oldValue = value;
// start blinking lights here, as the value has changed by at least 300
}
-
Gerben thank you but I would like to wait for a change in value, and from my understanding analogRead just reads the value of the pin. Very sorry I didn't mentioned earlier that the analog value must not be zero at the start.See Jian Shin– See Jian Shin2016年07月23日 14:49:17 +00:00Commented Jul 23, 2016 at 14:49
-
See my edit. It will trigger when the value is increased or decreased by at least 300.Gerben– Gerben2016年07月23日 15:05:58 +00:00Commented Jul 23, 2016 at 15:05
-
Soo sorry but I updated the questionSee Jian Shin– See Jian Shin2016年07月23日 15:59:37 +00:00Commented Jul 23, 2016 at 15:59
-
That's not really how this works (-: First give it a try yourself (more than 30 minutes). If you can't figure it out, come back, and we'll gladly help.Gerben– Gerben2016年07月23日 16:04:58 +00:00Commented Jul 23, 2016 at 16:04
A change in value is nothing more than that. A change between a value then and the value now.
All you need to do is remember what the value used to be, and compare it with what the value is now. If the two have changed, and the change is big enough, then you do something.
And of course you have to remember that "now" will be "then" "next time".
-
1Thank you Majenko !! ( Gerben basically put your words into c++)See Jian Shin– See Jian Shin2016年07月23日 15:32:02 +00:00Commented Jul 23, 2016 at 15:32
-
1No, Majenko put my code into words q-; (just check the timestamps)Gerben– Gerben2016年07月23日 15:55:49 +00:00Commented Jul 23, 2016 at 15:55
-
Soo sorry but I updated the questionSee Jian Shin– See Jian Shin2016年07月23日 15:59:56 +00:00Commented Jul 23, 2016 at 15:59
-
@SeeJianShin You might find this useful for introducing you to the concepts you need: hackingmajenkoblog.wordpress.com/2016/02/01/…Majenko– Majenko2016年07月23日 16:00:54 +00:00Commented Jul 23, 2016 at 16:00
Figured it out in the end. Although my solution doesn't exactly fit what I wanted but it works in this case. I used the FMS Majenko mentioned like this : Wait for potentiometer value to go beyond a certain value, then wait for it to come back to the value. If that happens then the lights stop blinking. Thanks guys( this is quite different from what I initially wanted but it is enough for what it is meant to do)