Progress has been made! But still running into some problems. Namely, I can't get only one random LED to blink. Anytime the serial monitor returns a 3 or 4 for randNum, I get two LEDs. I introduced a delay and confirmed that they're lighting up separately, I'm guessing because of the gameState, but I find it odd that it only does it when randNum is 3 or 4, and never for 2. I can't figure out for the life of me how to get the program to select a single LED, turn it on and keep it on without turning any others on. I tried commenting out the line in void showResults that also states digitalWrite(randNum, HIGH); but that didn't actually do the trick. Any help would be greatly appreciated!
//LEDs for the machine
int pin2 = 2;
int pin3 = 3;
int pin4 = 4;
int blinkCounter = 0;
long timestamp = 0; // long is a much larger integer
boolean ledState = LOW;
int delayLength = 500; // how long between intervals
int randNum;
//int maxnum = 1; //Blink LED 1 times
//int count = 0; //Blink counter
int gameState = 0; // 0 = 3 LEDs blinking + potentiometer / game play
// 1 = results
int val = 0; //used to store the state of the input pin
// INPUT: Potentiometer should be connected to 5V and GND
int potPin = 3; // Potentiometer output connected to analog pin 3
int potVal = 0; // Variable to store the input from the potentiometer
int potChoice; // the final choice from each game round
boolean gameResult = false; // did i win or not
boolean tieGame = false;
// LEDs for the potentiometer
int rockPin = 9; // Red LED, connected to digital pin 9
int paperPin = 10; // Green LED, connected to digital pin 10
int scissorsPin = 11; // Blue LED, connected to digital pin 11
// Program variables
int rockVal = 0; // Variables to store the values to send to the pins
int paperVal = 0;
int scissorsVal = 0;
// SOUND
int speakerPin = 8;
void setup() {
randomSeed(analogRead(0));
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT);
pinMode(rockPin, OUTPUT); // sets the players pins as output
pinMode(paperPin, OUTPUT);
pinMode(scissorsPin, OUTPUT);
// init Button Sound
pinMode(speakerPin, OUTPUT);
Serial.begin(9600);
startGame();
}
void loop() {
if (gameState == 0) {
// do the 3 blinks
blinkThreeTimes(); //this will blink and play sounds
checkPotentiometer(); // this will check the slider, and turn on corresponding LEDs
//checkResults(); // keep both LEDs on HIGH
} else {
// results
showResults(); // turn on the chosen random LED
playResultSounds(); // play either victory or defeat sound
}
}
void startGame() {
gameState = 0;
timestamp = millis();
blinkCounter = 0;
randomSeed(analogRead(0));
}
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
void blinkThreeTimes() {
if ( (millis() - timestamp > delayLength)) {
ledState = !ledState; // toggles the ledState between true/false
digitalWrite(pin2, ledState); // make the led change
digitalWrite(pin3, ledState);
digitalWrite(pin4, ledState);
timestamp = millis();
blinkCounter++;
Serial.print("blink:");
Serial.println(blinkCounter);
if (blinkCounter == 6) {
checkResults();
}
}
}
void checkPotentiometer() {
potVal = analogRead(potPin); // read the potentiometer value at the input pin
//potChoice = map(potVal, 0, 1023, 4, 2);
if (potVal < 341) // Lowest third of the potentiometer's range (0-340)
{
potVal = (potVal * 3) / 4; // Normalize to 0-255
rockVal = 256 - potVal; // Rock from full to off
paperVal = 1; // Paper from off to full
scissorsVal = 1; // Scissor off
potChoice = 2;
}
else if (potVal < 682) // Middle third of potentiometer's range (341-681)
{
potVal = ( (potVal-341) * 3) / 4; // Normalize to 0-255
rockVal = 1; // Rock off
paperVal = 256 - potVal; // Paper from full to off
scissorsVal = 1; // Scissor from off to full
potChoice = 3;
}
else // Upper third of potentiometer"s range (682-1023)
{
potVal = ( (potVal-683) * 3) / 4; // Normalize to 0-255
rockVal = 1; // Rock from off to full
paperVal = 1; // Paper off
scissorsVal = 256 - potVal; // Scissor from full to off
potChoice = 4;
}
analogWrite(rockPin, rockVal); // Write values to LED pins
analogWrite(paperPin, paperVal);
analogWrite(scissorsPin, scissorsVal);
}
void checkResults() {
//gameState = 1;
//select random lED
randNum=random(2,5);
//turn random LED on
digitalWrite(randNum, HIGH);
delay(1000);
digitalWrite(randNum, LOW);
Serial.print("potChoice:");
Serial.print(potChoice);
Serial.print(" random:");
Serial.println(randNum);
if (randNum == potChoice) {
Serial.println("TIE GAME");
// TIE GAME
tieGame = true;
}
else {
// it's not a tie. you either won or lost
gameResult = judgeGame(potChoice, randNum);
}
gameState = 1;
}
boolean judgeGame(int potChoice, int randNum) {
boolean finalResult;
switch (potChoice) {
case 2:
//do something when var equals 2
if (randNum == 4) {
finalResult = true;
}
else {
finalResult = false;
}
break;
case 3:
//do something when var equals 3
if (randNum == 2) {
finalResult = true;
}
else {
finalResult = false;
}
break;
case 4:
//do something when var equals 4
if (randNum == 3) {
finalResult = true;
}
else {
finalResult = false;
}
break;
default:
// if nothing else matches, do the default
// default is optional
Serial.print("the switch case broke");
Serial.println(potChoice);
finalResult = false;
}
return finalResult;
}
void showResults() {
if (tieGame == true) {
// do the tie blinky thing
}
else {
//lights go solid
//keep random LED on
digitalWrite(randNum, HIGH);
// if the pot value is faded
digitalWrite(potChoice, HIGH);
}
}
void playResultSounds() {
/*if (tieGame == true) {
// play tie sounds
}
else {
if (gameResult == true) {
//sound of victory
int length = 13; // power up.
int tempo = 30;
int tones[] = {5800, 5700, 5500, 4000, 3000, 2000, 1000, 500, 250, 200, 150, 120, 110};
int beats[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
for (int i=0; i < length; i++) {
if (tones[i] == 0) {
delay(beats[i] * tempo); // rest
} else {
playTone(tones[i], beats[i] * tempo);
}
}
}
else {
// play sound of defeat
int length = 4; // crazy, high, machine
int tempo = 30;
int tones[] = {1915, 1014, 700, 1915};
int beats[] = {1, 1, 1, 1 };
for (int i=0; i < length; i++) {
if (tones[i] == 0) {
delay(beats[i] * tempo); // rest
} else {
playTone(tones[i], beats[i] * tempo);
}
}
}
} */
}