Skip to content

Instantly share code, notes, and snippets.

@jondoesntgit
Created August 16, 2016 19:38
Show Gist options
  • Save jondoesntgit/94046eca5f48467ee40d65bcc225734a to your computer and use it in GitHub Desktop.
Save jondoesntgit/94046eca5f48467ee40d65bcc225734a to your computer and use it in GitHub Desktop.
Crosswalk Arduino
#include <Servo.h>
// Power pin is normally OFF
// Walk pin is normall ON
int walkPin = 2;
int powerPin = 3;
int togglePin = 3;
int bellPin = 12;
int redLightPin = 13;
int greenLightPin = 11;
int yellowLightPin = 10;
int redButtonPin = 5;
int yellowButtonPin = 6;
int greenButtonPin = 7;
long mills = 0;
int countdownTimeLeft = 0;
int buzzerPin = 4;
bool ignition = false;
unsigned long buzzerMillisLeft = 0;
int buzzerToggle = false;
int programmingPin = 9;
int ignitionPin =8;
bool toggle = false;
bool buttonPressed = false;
String buttonPresses = "";
String combination = "ggrryygg";
void setup() {
// put your setup code here, to run once:
pinMode(ignitionPin, INPUT_PULLUP);
pinMode(programmingPin, INPUT_PULLUP);
pinMode(walkPin, OUTPUT);
pinMode(powerPin, OUTPUT);
pinMode(togglePin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(redLightPin, OUTPUT);
pinMode(yellowLightPin, OUTPUT);
pinMode(greenLightPin, OUTPUT);
pinMode(bellPin, OUTPUT);
pinMode(redButtonPin, INPUT_PULLUP);
pinMode(yellowButtonPin, INPUT_PULLUP);
pinMode(greenButtonPin, INPUT_PULLUP);
digitalWrite(powerPin, LOW);
digitalWrite(walkPin, HIGH);
digitalWrite(redLightPin, HIGH);
digitalWrite(yellowLightPin, HIGH);
digitalWrite(greenLightPin, HIGH);
digitalWrite(bellPin, HIGH);
//SET UP THE TIMER
OCR0A = 0xAF;
TIMSK0 |= _BV(OCIE0A);
//delay(3000);
//countdownFrom(20);
// delay(24000);
}
void countdownFrom(int timeLeft){
countdownTimeLeft = timeLeft + 2;
}
SIGNAL(TIMER0_COMPA_vect){
unsigned long currentMillis = millis();
mills++;
if (buzzerMillisLeft > 0) {
buzzerMillisLeft--;
buzzerToggle = !buzzerToggle;
digitalWrite(buzzerPin, buzzerToggle);
}
if (mills > 500){
//buzzer(100);
mills = 0;
toggle = !toggle;
bool countingDown = (countdownTimeLeft > 0);
if (toggle) {
countdownTimeLeft--;
}
if (countingDown) {
digitalWrite(walkPin, LOW);
if (countdownTimeLeft > 1){
digitalWrite(powerPin, toggle);
} else {
digitalWrite(powerPin, LOW);
}
} else {
digitalWrite(walkPin, HIGH);
digitalWrite(powerPin, LOW);
}
}
if (digitalRead(programmingPin)) {
;
}
if (!digitalRead(ignitionPin)) {
//buzzer(100);
//countdownFrom(20);
}
}
void countdown(int seconds){
digitalWrite(powerPin, HIGH); // Turn off power
digitalWrite(walkPin, LOW); // Set to hand signal
for (int i = 0; i < seconds; i++) {
delay(300);
digitalWrite(powerPin, LOW);
delay(700);
digitalWrite(powerPin, HIGH);
}
delay(250);
digitalWrite(powerPin, LOW); // ENABLE CURRENT TO FLOW
digitalWrite(walkPin, HIGH);
//delay(1000);
}
void buzzer (unsigned long milli){
buzzerMillisLeft = milli;
/* buzzerMillisLeft = 10;
for (long i = 0; i++; i < buzzerMillisLeft){
delay(2);
digitalWrite(buzzerPin, HIGH);
delay(2);
digitalWrite(buzzerPin, LOW);
}*/
}
void checkCombo() {
Serial.print(buttonPresses);
bool success = (buttonPresses == combination);
if (success){
countdownTimeLeft = 0;
digitalWrite(bellPin, LOW);
buzzer(5000);
delay(1200);
digitalWrite(bellPin, HIGH);
delay(5000);
} else {
buzzer(100);
delay(200);
buzzer(100);
delay(200);
buzzer(200);
delay(300);
}
buttonPresses = "";
}
void registerPress(char pressedButtonChar){
if (buttonPressed){
return;
}
buttonPressed = true;
buttonPresses = buttonPresses + pressedButtonChar;
if (buttonPresses.length() >= 8){
checkCombo();
}
}
void checkInputs(){
ignition = digitalRead(ignitionPin);
if (!digitalRead(redButtonPin)){
delay(100); // debounce
if (!digitalRead(redButtonPin)) {
digitalWrite(redLightPin, LOW);
registerPress('r');
}
} else if (!digitalRead(yellowButtonPin)){
delay(100);
if (!digitalRead(yellowButtonPin)){
digitalWrite(yellowLightPin, LOW);
registerPress('y');
}
} else if (!digitalRead(greenButtonPin)){
delay(100);
if (!digitalRead(greenButtonPin)){
digitalWrite(greenLightPin, LOW);
registerPress('g');
}
} else {
if (buttonPressed) {
buzzer(200);
delay(300);
buttonPressed = false;
}
}
digitalWrite(redLightPin, digitalRead(redButtonPin));
digitalWrite(yellowLightPin, digitalRead(yellowButtonPin));
digitalWrite(greenLightPin, digitalRead(greenButtonPin));
}
void cycleLights(){
digitalWrite(yellowLightPin, LOW);
delay(700);
digitalWrite(yellowLightPin, HIGH);
digitalWrite(redLightPin, LOW);
delay(700);
digitalWrite(redLightPin, HIGH);
digitalWrite(greenLightPin, LOW);
delay(700);
digitalWrite(greenLightPin, HIGH);
}
void loop() {
/*if (countdownTimeLeft > 0) {
checkInputs();
}*/
ignition = digitalRead(ignitionPin);
if (ignition){
digitalWrite(bellPin, HIGH);
} else {
digitalWrite(bellPin, LOW);
}
//cycleLights();
//countdown(20);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment