/*
Clayton's Busy Box - Enjoy at own risk
YMMV
*/
#include <Arduino.h>
#include <U8g2lib.h>
#include <TM1637.h>
#include "pitches.h"
#define SPEAKER_PIN 7
#define switchpin 9 // pin for the potentiometer switch
//U8G2_MAX7219_32X8_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 11, /* data=*/ 12, /* cs=*/ 10, /* dc=*/ U8X8_PIN_NONE, /* reset=*/ U8X8_PIN_NONE);
U8G2_MAX7219_8X8_1_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 11, /* data=*/ 12, /* cs=*/ 10, /* dc=*/ U8X8_PIN_NONE, /* reset=*/ U8X8_PIN_NONE);
TM1637 TM; // intialize 7 segment display
int potpinL = 0; // analog pin used to connect the left potentiometer
int potpinR = 1; // analog pin used to connect the right potentiometer
int valL;
int valR; // variable to read the value from the left pot
int targetX;
int targetY;
int oldValL = 0;
int oldValR = 0;
int interactions = 0;
int gridmode = 0; // starting gridmode in left pot only box mode
int gridx = 0; // init grid x pixel position
int gridy = 0; // init grid y pixel position
int trafficmode = 0; // switch between button toggle and light chase game for Traffic Leds
int trafficLedPins[] = {23,25,27};
int trafficButtonPins[] = {14,15,16};
int organLedPins[] = {35,36,37,38,39,40,41,42};
int redLed1 = 23; // digital pin for red led 1
int yellowLed1 = 25; // digital pin for yellow led 1
int greenLed1 = 27; // digital pin for green led 1
int redButton1 = 14; // digital pin for red button 1
int yellowButton1 = 15; // digital pin for yellow button 1
int greenButton1 = 16; // digital pin for green button 1
int modeSwitch1 = 29;
int ButtonStateRed;
int ButtonStateYellow;
int ButtonStateGreen;
int trafficMode = 0;
int ButtonStateGame = 1; // Set the starting value of the Button State for the traffic Game
int nextLed = random(0,3); // pick random LED value between 0 and 2
int nextNote = random(0,8); // set new random note
int pitch = 0;
const int buttonTones[] = {
NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4,
NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5
};
void setup(void) {
pinMode(switchpin, INPUT_PULLUP);
pinMode(trafficLedPins[0], OUTPUT);
pinMode(trafficLedPins[1], OUTPUT);
pinMode(trafficLedPins[2], OUTPUT);
for (int i = 0; i <= 7; i++){
pinMode(trafficLedPins[i], OUTPUT);
}
pinMode(trafficButtonPins[0], INPUT_PULLUP);
pinMode(trafficButtonPins[1], INPUT_PULLUP);
pinMode(trafficButtonPins[2], INPUT_PULLUP);
pinMode(modeSwitch1, INPUT_PULLUP);
pinMode(SPEAKER_PIN, OUTPUT);
TM.begin(6, 5, 4); // clockpin, datapin
TM.displayClear();
u8g2.begin();
u8g2.setContrast(10*16);
targetX = random(0,8); // pick new target dot X value between 0 and 7
targetY = random(0,8); // pick new target dot Y value between 0 and 7
}
void loop(void) {
// Display interaction counter on 7-segment dispoay
TM.displayInt(interactions);
// 8x8 grid update
gridmode = digitalRead(switchpin); // check if right pot switch is off
if (gridmode == 0){
leftpot(); // if yes, do leftpot box mode
} else {
dotmover(); // if no do dot mode
}
// end 8x8 grid update
// Traffic Leds/buttons
trafficMode = digitalRead(modeSwitch1);
if (trafficMode == 0){
trafficLeds();
} else {
trafficSimon();
}
}
void leftpot(void) {
valL = analogRead(potpinL); // reads the value of the potentiometer (value between 0 and 1023)
valL = map(valL, 0, 1023, 0, 14); // scale it to use it with the 8x8 Grid but with overflow
if (valL > oldValL) interactions++;
if (valL < oldValL) interactions--;
u8g2.clearBuffer(); // clear the internal memory
u8g2.drawBox((valL-7),0,8,8); // draw a box corresponding to the position of the pot
//delay(100);
u8g2.sendBuffer(); // update the display
oldValL = valL;
}
void dotmover(void) {
valL = analogRead(potpinL); // reads the value of the left potentiometer (value between 0 and 1023)
valL = map(valL, 0, 1023, 0, 7); // scale it to use it with the 8x8 grid
if (valL > oldValL) interactions++;
if (valL < oldValL) interactions--;
valR = analogRead(potpinR); // reads the value of the right potentiometer (value between 0 and 1023)
valR = map(valR, 0, 1023, 0, 7); // scale it to use it with the 8x8 grid
if (valR > oldValR) interactions = (interactions+10);
if (valR < oldValR) interactions = (interactions-10);
// Check for target collision
if (((7-valR) == targetY) && (valL == targetX)) { // collision detected
// do some celebratory sound or something here
// Flash the target because it got hit
for (int i = 0; i <= 7; i++) {
u8g2.clearBuffer();
u8g2.sendBuffer(); // update the display
pitch = buttonTones[i];
tone(SPEAKER_PIN, pitch);
digitalWrite(organLedPins[i], HIGH);
delay(50);
// u8g2.drawPixel(valL, (7-valR)); // draw the dot corresponding to the pot positions on the grid
u8g2.drawPixel(targetX, targetY); // draw the target dot
u8g2.sendBuffer(); // update the display
noTone(SPEAKER_PIN);
delay(50);
}
for (int i = 0; i <= 7; i++) {
digitalWrite(organLedPins[i], LOW);
}
targetX = random(0,8); // pick new target dot X value between 0 and 7
targetY = random(0,8); // pick new target dot Y value between 0 and 7
interactions = (interactions+1000);
}
u8g2.clearBuffer();
u8g2.drawPixel(valL, (7-valR)); // draw the dot corresponding to the pot positions on the grid
u8g2.drawPixel(targetX, targetY); // draw the target dot
u8g2.sendBuffer(); // update the display
oldValL = valL;
oldValR = valR;
}
void trafficLeds(void) {
ButtonStateRed = digitalRead(trafficButtonPins[0]);
ButtonStateYellow = digitalRead(trafficButtonPins[1]);
ButtonStateGreen = digitalRead(trafficButtonPins[2]);
if (ButtonStateRed == 0){
digitalWrite(trafficLedPins[0], HIGH);
interactions++;
} else {
digitalWrite(trafficLedPins[0], LOW);
}
if (ButtonStateYellow ==0){
digitalWrite(trafficLedPins[1], HIGH);
interactions++;
} else {
digitalWrite(trafficLedPins[1], LOW);
}
if (ButtonStateGreen ==0){
digitalWrite(trafficLedPins[2], HIGH);
interactions++;
} else {
digitalWrite(trafficLedPins[2], LOW);
}
}
void trafficSimon(void) {
digitalWrite(trafficLedPins[nextLed], HIGH); // light the random LED
ButtonStateGame = digitalRead(trafficButtonPins[nextLed]); // Check the button for that LED
if (ButtonStateGame == 0){
pitch = buttonTones[nextNote];
tone(SPEAKER_PIN, pitch);
delay(50);
nextNote = random(0,8); // set new random note
digitalWrite(trafficLedPins[nextLed], LOW);
nextLed = random(0,3); // pick new random LED value between 0 and 2
interactions++;
delay(100);
noTone(SPEAKER_PIN);
}
}