I had a Zero a few months ago but couldn't really decide what I wanted to do with it (as usual, it's the 3rd Pi I've had!). Other than a Kodi media centre I've never really done anything else with them.
I'm pretty new to Arduino so this is the code I've written. It's a variation of a couple of Adafruit codes so there are probably better ways to write it but it does the job.
// this constant won't change:
const int buttonPin = 0; // the pin that the pushbutton is attached to
const int ledPinRED1 = 1; // the pin that the first RED LED is attached to
const int ledPinRED2 = 2; // the pin that the second RED LED is attached to
const int ledPinGRE1 = 3; // the pin that the first GREEN LED is attached to
const int ledPinGRE2 = 4; // the pin that the first GREEN 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(ledPinRED1, OUTPUT);
pinMode(ledPinRED2, OUTPUT);
pinMode(ledPinGRE1, OUTPUT);
pinMode(ledPinGRE2, OUTPUT);
// initialize serial communication:
//Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(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
// wend 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
// wend from on to off:
//Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
// turns on the Red/Green LEDs according to the
// number of button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
digitalWrite(ledPinRED1, LOW);
digitalWrite(ledPinRED2, LOW);
digitalWrite(ledPinGRE1, LOW);
digitalWrite(ledPinGRE2, LOW);
if (buttonPushCounter == 3) {
digitalWrite(ledPinRED1, HIGH);
digitalWrite(ledPinRED2, LOW);
digitalWrite(ledPinGRE1, LOW);
digitalWrite(ledPinGRE2, LOW);
}
if (buttonPushCounter ==6) {
digitalWrite(ledPinRED1, HIGH);
digitalWrite(ledPinRED2, HIGH);
digitalWrite(ledPinGRE1, LOW);
digitalWrite(ledPinGRE2, LOW);
}
if (buttonPushCounter ==9) {
digitalWrite(ledPinRED1, LOW);
digitalWrite(ledPinRED2, HIGH);
digitalWrite(ledPinGRE1, HIGH);
digitalWrite(ledPinGRE2, LOW);
}
if (buttonPushCounter ==11) {
digitalWrite(ledPinRED1, LOW);
digitalWrite(ledPinRED2, LOW);
digitalWrite(ledPinGRE1, HIGH);
digitalWrite(ledPinGRE2, HIGH);
}
}