Skip to content

Instantly share code, notes, and snippets.

@alextaujenis
Last active December 20, 2018 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alextaujenis/171b769b4be1f1672acfe2258ca9c6ca to your computer and use it in GitHub Desktop.
Save alextaujenis/171b769b4be1f1672acfe2258ca9c6ca to your computer and use it in GitHub Desktop.
Whack-a-mole game written using the Arduino Robots + Big Data libraries http://robotsbigdata.com/
// Arduino Whack-a-mole game.
// Copyright 2018 Alex Taujenis
// MIT License
#include <RBD_Timer.h> // https://github.com/alextaujenis/RBD_Timer
#include <RBD_Light.h> // https://github.com/alextaujenis/RBD_Light
#include <RBD_Capacitance.h> // https://github.com/alextaujenis/RBD_Capacitance
#include <RBD_Threshold.h> // https://github.com/alextaujenis/RBD_Threshold
#include <RBD_HumanSensor.h> // https://github.com/alextaujenis/RBD_HumanSensor
// define the lights
RBD::Light light1(22);
RBD::Light light2(24);
RBD::Light light3(26);
RBD::Light light4(28);
RBD::Light light5(30);
RBD::Light light6(32);
RBD::Light light7(34);
// define the human touch sensors
RBD::HumanSensor sensor1(36, 37); // send, receive pin
RBD::HumanSensor sensor2(38, 39);
RBD::HumanSensor sensor3(40, 41);
RBD::HumanSensor sensor4(42, 43);
RBD::HumanSensor sensor5(44, 45);
RBD::HumanSensor sensor6(46, 47);
RBD::HumanSensor sensor7(48, 49);
// define a timer for each light
RBD::Timer timer1();
RBD::Timer timer2();
RBD::Timer timer3();
RBD::Timer timer4();
RBD::Timer timer5();
RBD::Timer timer6();
RBD::Timer timer7();
void setup() {
// start serial output
Serial.begin(115200);
// define the human sensor near ranges
// we don't care about this, but it must be lower than the setTouchValue
sensor1.setNearValue(1);
sensor2.setNearValue(1);
sensor3.setNearValue(1);
sensor4.setNearValue(1);
sensor5.setNearValue(1);
sensor6.setNearValue(1);
sensor7.setNearValue(1);
// define the human sensor touch ranges
// these numbers are what you should adjust when calibrating the device
sensor1.setTouchValue(200);
sensor2.setTouchValue(200);
sensor3.setTouchValue(200);
sensor4.setTouchValue(200);
sensor5.setTouchValue(200);
sensor6.setTouchValue(200);
sensor7.setTouchValue(200);
// leave the human sensor pickup range as default (very high)
// prepare each timer with a random value
timer1.setTimeout(randomWait());
timer2.setTimeout(randomWait());
timer3.setTimeout(randomWait());
timer4.setTimeout(randomWait());
timer5.setTimeout(randomWait());
timer6.setTimeout(randomWait());
timer7.setTimeout(randomWait());
// start each timer
timer1.restart();
timer2.restart();
timer3.restart();
timer4.restart();
timer5.restart();
timer6.restart();
timer7.restart();
}
void loop() {
// update all human sensors
sensor1.update();
sensor2.update();
sensor3.update();
sensor4.update();
sensor5.update();
sensor6.update();
sensor7.update();
// trigger the sensorTouched() function when touched
if(sensor1.onTouch()) { sensorTouched(1) };
if(sensor2.onTouch()) { sensorTouched(2) };
if(sensor3.onTouch()) { sensorTouched(3) };
if(sensor4.onTouch()) { sensorTouched(4) };
if(sensor5.onTouch()) { sensorTouched(5) };
if(sensor6.onTouch()) { sensorTouched(6) };
if(sensor7.onTouch()) { sensorTouched(7) };
// switch which lights turn on and off (at random)
updateLights();
// uncomment and calibrate each sensor individually below to find their setTouchValue()
// Serial.println(sensor1.getValue());
// Serial.println(sensor2.getValue());
// Serial.println(sensor3.getValue());
// Serial.println(sensor4.getValue());
// Serial.println(sensor5.getValue());
// Serial.println(sensor6.getValue());
// Serial.println(sensor7.getValue());
}
void sensorTouched(int i) {
// trigger the hit or miss function depending upon if the light is on
if (i == 1) { if (light1.isOn()) { hit(i); } else { miss(i); } }
if (i == 2) { if (light2.isOn()) { hit(i); } else { miss(i); } }
if (i == 3) { if (light3.isOn()) { hit(i); } else { miss(i); } }
if (i == 4) { if (light4.isOn()) { hit(i); } else { miss(i); } }
if (i == 5) { if (light5.isOn()) { hit(i); } else { miss(i); } }
if (i == 6) { if (light6.isOn()) { hit(i); } else { miss(i); } }
if (i == 7) { if (light7.isOn()) { hit(i); } else { miss(i); } }
}
void updateLights() {
// if the timer is expired
if (timer1.onExpired()) {
// check if the light is off
if(light1.isOff()) {
// turn the light on for a random time
light1.on();
timer1.setTimeout(randomLight());
} else {
// the light is on, so we should turn it off, then wait to turn it back on
light1.off();
timer1.setTimeout(randomWait());
}
}
if (timer2.onExpired()) {
if(light2.isOff()) {
light2.on();
timer2.setTimeout(randomLight());
} else {
light2.off();
timer2.setTimeout(randomWait());
}
}
if (timer3.onExpired()) {
if(light3.isOff()) {
light3.on();
timer3.setTimeout(randomLight());
} else {
light3.off();
timer3.setTimeout(randomWait());
}
}
if (timer4.onExpired()) {
if(light4.isOff()) {
light4.on();
timer4.setTimeout(randomLight());
} else {
light4.off();
timer4.setTimeout(randomWait());
}
}
if (timer5.onExpired()) {
if(light5.isOff()) {
light5.on();
timer5.setTimeout(randomLight());
} else {
light5.off();
timer5.setTimeout(randomWait());
}
}
if (timer6.onExpired()) {
if(light6.isOff()) {
light6.on();
timer6.setTimeout(randomLight());
} else {
light6.off();
timer6.setTimeout(randomWait());
}
}
if (timer7.onExpired()) {
if(light7.isOff()) {
light7.on();
timer7.setTimeout(randomLight());
} else {
light7.off();
timer7.setTimeout(randomWait());
}
}
}
void hit(int i) {
Serial.println(i + " HIT!");
if (i == 1) {
// the sensor was hit with the light on, so turn it off
light1.off();
// wait to turn this light back on again
timer1.setTimeout(randomWait());
}
if (i == 2) { light2.off(); timer2.setTimeout(randomWait()); }
if (i == 3) { light3.off(); timer3.setTimeout(randomWait()); }
if (i == 4) { light4.off(); timer4.setTimeout(randomWait()); }
if (i == 5) { light5.off(); timer5.setTimeout(randomWait()); }
if (i == 6) { light6.off(); timer6.setTimeout(randomWait()); }
if (i == 7) { light7.off(); timer7.setTimeout(randomWait()); }
}
void miss(int i) {
Serial.println(i + " MISS!");
}
long randomWait() {
// each mole will wait between 3 and 15 seconds to turn on
return random(3000, 15000);
}
long randomLight() {
// the mole will be on between 1 and 4 seconds
return random(1000, 4000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment