Last active
December 22, 2015 14:48
-
-
Save landaire/6487964 to your computer and use it in GitHub Desktop.
Sample arduino code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int ledSpeed = 1, buttonVal = 0, ledState = 0; | |
const int buttonPin = 7, buttonPressAcknowledgedPin = 11;//, allOnPin = 11, blinkingPin = 4; | |
const int ledPins[] = | |
{ | |
2, // red | |
8, // yellow | |
12 // blue | |
}; | |
void ledsBlink(int duration) | |
{ | |
// this is required to ensure that all lights reset after a button press | |
for (int i = 0; i < 3; i++) | |
{ | |
digitalWrite(ledPins[i], LOW); | |
} | |
// turn each LED on for the duration | |
for (int i = 0; i < 3; i++) | |
{ | |
digitalWrite(ledPins[i], HIGH); | |
delay(duration); | |
digitalWrite(ledPins[i], LOW); | |
} | |
} | |
void ledsOff() | |
{ | |
for (int i = 0; i < 3; i++) | |
{ | |
digitalWrite(ledPins[i], LOW); | |
} | |
} | |
void ledsOn() | |
{ | |
for (int i = 0; i < 3; i++) | |
{ | |
digitalWrite(ledPins[i], HIGH); | |
} | |
} | |
void setup() | |
{ | |
Serial.begin(9600); | |
Serial.println("Initializing pins"); // initialize pins as outputs | |
for (int i = 0; i < 3; i++) | |
{ | |
pinMode(ledPins[i], OUTPUT); | |
} | |
pinMode(buttonPin, INPUT); | |
pinMode(buttonPressAcknowledgedPin, OUTPUT); | |
Serial.println("Entering loop"); | |
} | |
void loop() | |
{ | |
// read the button state | |
if (digitalRead(buttonPin) == HIGH) | |
{ | |
digitalWrite(buttonPressAcknowledgedPin, HIGH); | |
delay(2000); | |
digitalWrite(buttonPressAcknowledgedPin, LOW); | |
Serial.println(ledState); | |
if (ledState == 2) | |
{ | |
ledState = -1; | |
} | |
ledState++; | |
// for ghetto debugging | |
if (ledState == 0) | |
{ | |
Serial.println("All LEDs should now be off"); | |
} | |
else if (ledState == 1) | |
{ | |
Serial.println("All LEDs should now be on"); | |
} | |
else if (ledState == 2) | |
{ | |
Serial.println("The LEDs should now be blinking"); | |
} | |
Serial.println(ledState); | |
} | |
// Check the button value. If it's off, then let the lights do their thing | |
if (ledState == 2) | |
{ | |
ledsBlink(ledSpeed); | |
if (ledSpeed >= 1000) | |
{ | |
ledSpeed = 1; | |
} | |
else | |
{ | |
ledSpeed *= 2; | |
} | |
} | |
// if the button is on, then turn all lights on | |
else if (ledState == 1) | |
{ | |
ledsOn(); | |
} | |
else if (ledState == 0) | |
{ | |
ledsOff(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment