Back to Parent

//pins
int ledPinYes = A0;
int ledPinNo = A1;
int buttonPinYes = D3;
int buttonPinNo = D4;
int ledPin = D0; //use to show button availability

//status
int buttonStateYes = 0;
int buttonStateNo = 0;
int available = 1;
bool buttonReleased = true;

void setup() {
  //buttons
  pinMode(buttonPinYes, INPUT_PULLDOWN);
  pinMode(buttonPinNo, INPUT_PULLDOWN);
  //leds
  pinMode(ledPinYes, OUTPUT);
  pinMode(ledPinNo, OUTPUT);
  pinMode(ledPin, OUTPUT);
  //wait for webpage to reset the button availability
  Spark.function("reset", reset);
}

void loop() {
  buttonStateYes = digitalRead(buttonPinYes);
  buttonStateNo = digitalRead(buttonPinNo);
  digitalWrite(ledPin, LOW);

  if(!buttonReleased && buttonStateYes == 0 && buttonStateNo == 0){
    buttonReleased = true;
  }

  if(available == 1 && buttonReleased){
    digitalWrite(ledPin, HIGH);

    if (buttonStateYes == 1) {
      digitalWrite(ledPinYes, HIGH);
      Spark.publish("vote/yes");
      available = 0;
      buttonReleased = false;
    }

    if (buttonStateNo == 1){
      digitalWrite(ledPinNo, HIGH);
      Spark.publish("vote/no");
      available = 0;
      buttonReleased = false;
    }
  }
}

//wait for webpage to reset the button availability
int reset(String Args){
  available = 1;
  return 1;
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0