Last active
August 29, 2015 14:08
-
-
Save eyesee1/c978a5e5880491318631 to your computer and use it in GitHub Desktop.
arduino pinball lanes simulator
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
/* Pinball rollover lanes simulator | |
* | |
* Isaac Csandl http://nerkles.com/ | |
* Copyleft 2014 All Rights Reversed. | |
*/ | |
// pins for rollover targets: | |
const int laneSwitches[] = {5,6,7}; | |
// pins for LEDs: | |
const int laneLEDs[] = {2,3,4}; | |
const int cycleDelay = 60; | |
const int laneCount = 3; // 0-indexed | |
volatile int lanePreviousState[] = {LOW,LOW,LOW}; | |
volatile int LEDState[] = {LOW,LOW,LOW}; | |
void setup() { | |
int i; | |
for (i=0; i<laneCount; i=i+1) { | |
pinMode(laneSwitches[i], INPUT); | |
pinMode(laneLEDs[i], OUTPUT); | |
pinMode(8, OUTPUT); | |
digitalWrite(laneLEDs[i], LOW); | |
} | |
cycleLanes(3 ); | |
Serial.begin(9600); | |
} | |
void restoreLanes(int states[]) { | |
int i; | |
for (i=0; i<laneCount; i=i+1) { | |
digitalWrite(laneLEDs[i], states[i]); | |
LEDState[i] = states[i]; | |
lanePreviousState[i] = states[i]; | |
} | |
} | |
void resetLanes() { | |
int i; | |
for (i=0; i<laneCount; i=i+1) { | |
digitalWrite(laneLEDs[i], LOW); | |
LEDState[i] = LOW; | |
Serial.print("LN:"); | |
Serial.print(i); | |
Serial.print(":"); | |
Serial.println(0); | |
} | |
} | |
void cycleLanes(int times) { | |
int i, j; | |
for (i=0; i<times; i=i+1) { | |
for (j=0; j<laneCount; j=j+1) { | |
digitalWrite(laneLEDs[j], HIGH); | |
delay(cycleDelay); | |
digitalWrite(laneLEDs[j], LOW); | |
} | |
} | |
resetLanes(); | |
} | |
void completeLanesSound() { | |
int i, j; | |
// repeat 3x | |
for (i=0; i<3; i=i+1) { | |
// make a downward lasery sound: | |
for(j=1200; j>200; j=j-100) { | |
tone(8, j); | |
delay(15); | |
} | |
} | |
noTone(8); | |
} | |
void loop() { | |
int laneIndex; | |
int state; | |
int onCount = 0; | |
for (laneIndex=0; laneIndex<laneCount; laneIndex=laneIndex+1) { | |
state = digitalRead(laneSwitches[laneIndex]); | |
if (lanePreviousState[laneIndex]==HIGH && state==LOW) { | |
digitalWrite(laneLEDs[laneIndex], HIGH); | |
LEDState[laneIndex] = HIGH; | |
Serial.print("LN:"); | |
Serial.print(laneIndex); | |
Serial.print(":"); | |
Serial.println(1); | |
tone(8, 140); | |
delay(100); | |
tone(8, 440); | |
delay(100); | |
noTone(8); | |
} | |
if (LEDState[laneIndex]==HIGH) { | |
onCount = onCount + 1; | |
} | |
lanePreviousState[laneIndex] = state; | |
} | |
if (onCount==3) { | |
Serial.println("lanes done!"); | |
delay(200); | |
completeLanesSound(); | |
cycleLanes(5); | |
delay(80); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment