I have put some code together that when a switch or button is pressed the spark core publishes a pressed state once only.
When released it publishes a released state, again once only.
When I compile is shows an error (iftttonce.cpp:25:1: error: expected unqualified-id before ‘else’)
Please can someone have a quick look and help me ?
int led = D0; // LED is connected to D0
int pushButton = D2; // Push button is connected to D2
bool button = false;
// This routine runs only once upon reset
void setup()
{
pinMode(led, OUTPUT); // Initialize D0 pin as output
pinMode(pushButton, INPUT_PULLUP);
// Initialize D2 pin as input with an internal pull-up resistor
}
void loop()
{
if (button == true) {
digitalWrite(led, HIGH); // Turn ON the LED
Spark.publish("pushButtonState", "Pressed", 60, PRIVATE);
button = false;
}
else {
}
}
else {
if (button == false) {
Spark.publish("pushButtonState", "Released", 60, PRIVATE);
button = true;
}
else {
}
}
I could be mistaken, but I believe one of your curly brackets closes the loop() before the second ‘else’.
Furthermore, you aren’t doing anything with your button at the moment. It never reads the button, thus never uses its value.
Currently you’re in a false/true loop; you start with ‘false’ which publishes and sets it to ‘true’ which publishes and sets it to ‘false’, and repeat. That will push you over the publish limit instantly. Try implementing a button read, and make sure to debounce it.
What I am struggling to achieve is while ever the button is pressed 1 Spark.publish is sent. Then when the button is released 1 different Spark.publish is sent.
Any help is appreciated and the second attempt is below:
// constants won't change. They're used here to
// set pin numbers:
int buttonPin = D2; // the number of the pushbutton pin
int ledPin = D0; // the number of the LED pin
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
// set initial LED state
digitalWrite(ledPin, ledState);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState = !ledState;
Spark.publish("pushButtonState", "Pressed", 60, PRIVATE);
}
}
}
// set the LED:
digitalWrite(ledPin, ledState);
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
}