I'm having trouble creating a toggle switch using an analog input connected to an RF receiver. I'm attempting to make a servo turn 180 degrees when the voltage reads higher than 800 and stay at 180 degrees until the button is pressed again. It only reads higher than 800 when I press a button on my key fob (RF transmitter). In my code it does this, but it doesn't hold at 180 degrees and wait until the button is pressed again to go back to 0 degrees.
Have you looked at the state change detection example? You could see that it compares the current reading to the previous reading. That the current and previous values are either 0 or 1 doesn't matter. it is that they are not the same that matters.
I did find that and had no luck. It just reads 'off' over and over. Here was my attempt at using that example:
// this constant won't change:
const int buttonPin = A0; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(2400);
}
void loop() {
// read the pushbutton input pin:
buttonState = analogRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}
else {
// if the current state is LOW then the button
// went from on to off:
Serial.println("off");
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (buttonPushCounter % 4 == 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
I have no idea. All I did was change the digital input button to be analog from the pin I'm reading the voltage from. As you can see I don't really know how to use that example for what I want. I understand what it's doing, but am having a difficult time using it.
I understand what it's doing, but am having a difficult time using it.
Then you can see that it compares the current state to the previous state, to determine if a change occurred. If so, it then compares the current state to HIGH to determine if the change is TO pressed or TO released.
An analog pin doesn't have a pressed state or a released state. What in can have is a current value and a previous value. What you do when the current value is, or is not, the same as the previous value is up to you.
What the example shows is the need to track the previous state/value.
OK. I'm pretty close with this code. I added a variable and a couple if statements. The LED turns on when the key fob is pressed, but doesn't turn off when pressed again. Thanks for the help guys.
I'm not sure how to respond to your question about where I'm storing the current state as the previous state. Can you give an example or tweak my code to show me? I'm no expert.
I removed the float line and replaced it with this:
int sensorValue;
I'm using delays as a debounce for the key fob button. You know a better way?
Is this going to hold the value read this time through loop? Or is it going to hold the value read last time? In either case, why is the name ambiguous?
Did you read and understand the state change detection example? There are only a handful of relevant lines.
I don't understand what you are trying to say about the sensorValue being ambiguous. It's just what I'm calling the analog reading. Also, don't understand your first question about holding the value read.
I do understand the state change example. I just don't know how to apply it using an analog input. I'm not using a traditional button (state).
For example: if (buttonState == HIGH) There is no "HIGH". Also, I don't know how to use the counter. This line specifically confuses me: if (buttonPushCounter % 4 == 0)
Code examples that explain what you are getting at would help me a ton. Thanks man.
Suppose, just for example, that PreviousSensorValue is 0 and sensorValue is 25. This says if(25 < threshold) do one thing. Otherwise, if(25 < threshold) do something else.