Last active
October 30, 2018 20:11
-
-
Save justbennett/a68a47d28f705d8b19310b4c5dab7e05 to your computer and use it in GitHub Desktop.
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
//Updated to add ambient light sensing and audio feedback on button presses. | |
// | |
//For the circuit and application explanation see http://www.instructables.com/id/Bot-Laser-Gallery-Game/ | |
// | |
const byte numberOfBots = 5; | |
const byte sensorPins[] = {A0,A1,A2,A3,A4}; // All analog inputs | |
const byte ledPins[] = {3,4,5,6,7}; // feed back leds | |
unsigned int sensorValues[numberOfBots]; // values of analogs | |
bool botStates[numberOfBots]; //state of analogs //I realize now that I should have made a struct or class buttt.. | |
const byte resetButton = 8; //reset button pin | |
const byte speaker = 10; //Speaker output | |
const byte upAdjust = 12; //If you want buttons for adjusting the Threshold. Untested | |
const byte downAdjust = 11; | |
byte threshold =70; //Might make this adjustable with button presses | |
bool isDone = false; //True if Robot has been turned off | |
byte ambient; //take a reading of the ambient light | |
void setup() { | |
pinMode(speaker, OUTPUT); | |
// declare the ledPins as an OUTPUT: | |
for (int i = 0; i < numberOfBots ; i++){ | |
pinMode(ledPins[i], OUTPUT); | |
} | |
pinMode(resetButton, INPUT_PULLUP); | |
pinMode(upAdjust, INPUT_PULLUP); | |
pinMode(downAdjust, INPUT_PULLUP); | |
Serial.begin(9600); | |
resetBots();//This will turn all the bots on | |
} | |
void loop() { | |
handleButtons(); //Function deals with button presses | |
handleSensors(); //Function deals with sensor reading | |
} | |
//Checks for button presses and takes action. There's no need for debounce on these button actions | |
void handleButtons(){ | |
if (!digitalRead(resetButton)){ | |
resetBots(); | |
Serial.println("RESET All bots on!.........."); | |
} | |
if (!digitalRead(upAdjust)){ | |
threshold+=10; | |
buttonPress(); | |
} | |
if (!digitalRead(downAdjust)){ | |
threshold-=10; | |
buttonPress(); | |
} | |
} | |
//Read sensors and respond accordingly | |
void handleSensors(){ | |
// read the value from the sensors: | |
int liveCount=0; //How many bots are still live | |
Serial.print(threshold); | |
Serial.print(" <-Thresh \t"); | |
Serial.print(ambient); | |
Serial.print(" <-Ambient \t"); | |
for (int i = 0; i < numberOfBots ; i++){ | |
sensorValues[i] = analogRead(sensorPins[i]); //Read each sensor | |
liveCount+=botStates[i]; //Checks to see if any bots are still on | |
Serial.print(i+1); | |
Serial.print("->"); | |
Serial.print(sensorValues[i]); | |
Serial.print("\t"); | |
if (sensorValues[i] > threshold+ambient && botStates[i]) {//If the LDR reads higher than the threshold | |
botStates[i] = false; //Turns off the bot | |
handleNote(); //Play a sound | |
} | |
digitalWrite(ledPins[i], botStates[i]); //Update all the bots | |
} | |
if (liveCount==0){ //Game won or game over | |
resetBots(); | |
} | |
Serial.println(" "); | |
} | |
//Handles debounce and audio feedback for threshold buttons | |
void buttonPress(){ | |
Serial.print("Threshold is "); | |
Serial.println(threshold); | |
tone(speaker, 200+threshold); | |
delay(250); | |
noTone(speaker); | |
} | |
//It resets the ledStates to HIGH and takes another ambient light reading | |
void resetBots(){ | |
ambient = 0; | |
for (int i = 0; i < numberOfBots ; i++){ | |
botStates[i] = true; | |
digitalWrite(ledPins[i], HIGH); | |
handleNote(); | |
ambient+=analogRead(sensorPins[i]); //Note this command can only happen after the ledPins are set to high | |
} | |
ambient = ambient/numberOfBots; //Average of all the bots LDR readings | |
} | |
//Plays a dyooup sound | |
void handleNote(){ | |
unsigned int note = 3000; | |
while (note > 31){ | |
tone(speaker,note); | |
delay(5); | |
note-=100; | |
} | |
noTone(speaker); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment