I am Building a 4 legged walker with 2 servos for School and was wondering if it was possible to trigger the Walking with one button and when the button is pressed again to turn it off(note it´s a push button)
For the second button i want to do the same Thing is this possible? Btw I am new to coding so yeah...
Here is an example of how to use the state change detection to toggle an output pin (pin 13, on board LED).
// This is to illustrate using tne state change detectin method to
// toggle the state of a pin (Pin 13, the on board LED)
// a momentary switch (pushbutton) is wired from pin 8 to ground
// by C Goulding aka groundFungus
// this constant won't change:
const int buttonPin = 8; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
boolean buttonState = 0; // current state of the button
boolean lastButtonState = 0; // previous state of the button
void setup()
{
// initialize the button pin as a input with internal pullup enabled
pinMode(buttonPin, INPUT_PULLUP);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop()
{
//check the button state every 50 milliseconds
static unsigned long timer = 0;
unsigned long interval = 50;
if (millis() - timer >= interval)
{
timer = millis(); // reset timer
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState)
{
if (buttonState == LOW)
{
// if the current state is LOW then the button
// went from off to on:
digitalWrite(ledPin, !digitalRead(ledPin)); // toggle the output
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
}