Project tutorial
Arduino Game - The Crazy Potentiometers

Arduino Game - The Crazy Potentiometers © CC BY-NC-SA

Can you tune the three V. Us using the pots in just 15 seconds?

  • 3 views
  • 0 comments
  • 0 respects

Components and supplies

About this project

At some point on the internet I came across someone who had made a game like that, but unfortunately, I missed the bookmark. So I decided to make my version, basing myself a bit on what I remembered the little game I saw.

The game consists of calibrating the three V.U.s using the three potentiometers. It sounds like a cinch, but with each round, the pots are drawn. So without always the blue potentiometer will move the V.U pointer. with the Blue Led. And yet, not always the pointer of the V.U. and the potentiometer move in the same direction. You have 15 seconds to find the calibration or the system restarts and replaces all the positions. There are 48 different possibilities!

Code

The Crazy Potentiometer GameArduino
Game with Arduino, three pots, three leds and three VUs
/**
   App:               THE CRAZY POTS
   Desenvolvido por:  Djair Guilherme (Nicolau dos Brinquedos)
   Usando ARDUINO NANO
   Abril de 2018

                      +-----+
         +------------| USB |------------+
         |            +-----+            |
    B5   | [ ]D13/SCK        MISO/D12[ ] |   B4
         | [ ]3.3V           MOSI/D11[ ]~|   B3
         | [ ]V.ref     ___    SS/D10[ ]~|   B2
    C0   | [ ]A0       / N \       D9[ ]~|   B1
    C1   | [ ]A1      /  A  \      D8[ ] |   B0
    C2   | [ ]A2      \  N  /      D7[ ] |   D7
    C3   | [ ]A3       \_0_/       D6[ ]~|   D6
    C4   | [ ]A4/SDA               D5[ ]~|   D5
    C5   | [ ]A5/SCL               D4[ ] |   D4
         | [ ]A6              INT1/D3[ ]~|   D3
         | [ ]A7              INT0/D2[ ] |   D2
         | [ ]5V                  GND[ ] |
    C6   | [ ]RST                 RST[ ] |   C6
         | [ ]GND   5V MOSI GND   TX1[ ] |   D0
         | [ ]Vin   [ ] [ ] [ ]   RX1[ ] |   D1
         |          [ ] [ ] [ ]          |
         |          MISO SCK RST         |
         | NANO-V3                       |
         +-------------------------------+

         http://busyducks.com/ascii-art-arduinos

*/
#define DEBUG

// Led Pins
#define blue  6
#define yellow 7
#define red 8

// Vu meters
#define left 9
#define center 10
#define right 11

// Potentiometers
#define bpot A7
#define ypot A6
#define rpot A5

// The readings of pins 3, 4 and 5 will detect correct number
// Connected to Vu meters, convert PWM readings into Analog Reading

#define read0 3
#define read1 4
#define read2 5

// Outputs
#define buzzer 12
#define relay 13

// Time for clue
#define timeclue 1000

//Winning Numbers 935
#define winner0 9
#define winner1 3
#define winner2 5

unsigned long ultimaRodada = 0;
unsigned long tempoRodada = 15000;

// Array tha contains pin numbers

int led[3] = {blue, yellow, red}; // Blue, Yellow, Red
int vu[3] = {left, center, right};
int pot[3] = {bpot, ypot, rpot};
int readings[3] = {read0, read1, read2};

int winner[3] = {winner0, winner1, winner2};

int pot0, pot1, pot2;
int out0, out1, out2;

const byte inactive [2] = {1, 2};

// To follow puzzle States

enum PuzzleState {Initialising, Running, Solved};
PuzzleState puzzleState = Initialising;

bool lastState = false;
int sensorValue = 0;
int outputValue = 0;

void setup() {
  for (int i = 0; i < 3; i++) {
    pinMode(led[i], OUTPUT);
    pinMode(vu[i], OUTPUT);
    pinMode(readings[i], INPUT);
  }

  // Avoiding noises!!!

  for (int j = 0; j < 9; j++) {
    pinMode(inactive [j], INPUT_PULLUP);
  }

  pinMode (relay, OUTPUT);

  randomSeed (analogRead(0));

#ifdef DEBUG
  Serial.begin(9600);
  Serial.println(F("Starting Serial Communication... SETUP"));
#endif
  digitalWrite (blue, LOW);
  digitalWrite (yellow, LOW);
  digitalWrite (red, LOW);

  puzzleState = Running;
}

void loop() {

  digitalWrite (relay, LOW);
  maxMinVu (0);
  delay (500);

  setLed (HIGH, LOW, LOW);
  analogWrite (left, 255);
  delay(timeclue);

  setLed (LOW, HIGH, LOW);
  analogWrite (center, 255);
  delay(timeclue);

  setLed (LOW, LOW, HIGH);
  analogWrite (right, 255);
  delay(timeclue);

  maxMinVu(0);

  ultimaRodada = millis();

  int choice = random (1, 48); // 48 probabilities!

  while ((millis() - ultimaRodada < tempoRodada)) {

    sorteio (choice);

    // reading VU values
    int number0 = map (readPWM(read0), 0, 1023, 0, 9);
    int number1 = map (readPWM(read1), 0, 1023, 0, 9);
    int number2 = map (readPWM(read2), 0, 1023, 0, 9);

    //Detect if evaluated numbers are the winners!;
    if (evaluate (number2, number1, number0)) {
      onSolve();
      delay(10000);
    } else if (!evaluate) {
      onUnsolve();
    }
  }
  Serial.println ("Next round");
}

// Here are the custom functions:

void maxMinVu(int flag) {
  if (flag == 0) {
    setLed (LOW, LOW, LOW);
    analogWrite (left, 0);
    analogWrite (center, 0);
    analogWrite (right, 0);
  } else if (flag == 1) {
    analogWrite (left, 255);
    analogWrite (center, 255);
    analogWrite (right, 255);
  }
}

void normal (int sense, int vumeter) {
  // Pot and VU at same direction
  int potentiometro = analogRead(sense);
  int saida = map (potentiometro, 1023, 0 , 0, 255);
  analogWrite (vumeter, saida);
}

void reverso (int sense, int vumeter) {
  // Pot and VU are reversed
  int potentiometro = analogRead(sense);
  int saida = map (potentiometro, 0, 1023 , 0, 255);
  analogWrite (vumeter, saida);
}

void setLed (int stateBlue, int stateYellow, int stateRed) {
  // Led is on if Pot and VU matches
  digitalWrite (blue, stateBlue);
  digitalWrite (yellow, stateYellow);
  digitalWrite (red, stateRed);
}

int readPWM(int digitalPin) {
  // Detect PWM
  int var = pulseIn (digitalPin, HIGH, 4200);
  if (var == 0 && digitalRead (digitalPin) == 1) {
    var = 2100;
  }
  var = map (var, 0 , 2100, 0, 1023);
  return var;
}

bool evaluate (int num0, int num1, int num2) {
  // Evaluate Results to detect Winner:
#ifdef DEBUG
  Serial.print (num0);
  Serial.print (" ");
  Serial.print (num1);
  Serial.print (" ");
  Serial.println (num2);
  Serial.println ("-----");
#endif

   if (num0 == winner[0] && num1 == winner[1] && num2 == winner[2]) {
    return true;
  } else if (num0 != winner[0] || num1 != winner[1] || num2 != winner[2]) {
    return false;
  }
}


void onSolve() {
  // Aciona o rel se o quebra-cabea foi resolvido
  digitalWrite (relay, HIGH);
#ifdef DEBUG
  Serial.println(F("Quebra Cabeca Resolvido!"));
#endif
  // E toca cinco bipes!
  for (int i = 0; i < 5; i++) {
    tone (buzzer, 2500, 1000);
    setLed (HIGH, HIGH, HIGH);
    delay(200);
    noTone(buzzer);
    setLed (LOW, LOW, LOW);
    delay(200);
  }
  puzzleState = Solved;
}

void onUnsolve() {
#ifdef DEBUG
  //Serial.println(F("Quebra Cabeca Desmanchado!"));
#endif
  puzzleState = Running;
}

void sorteio (int choice) {
  // Aqui so 48 possibilidades!!!
  switch (choice) {
    case 1:
      normal (bpot, left);
      normal (ypot, center);
      normal (rpot, right);
      setLed (HIGH, HIGH, HIGH);
      break;
    case 2:
      normal (bpot, left);
      normal (ypot, right);
      normal (rpot, center);
      setLed (HIGH, LOW, LOW);
      break;
    case 3:
      normal (bpot, right);
      normal (ypot, center);
      normal (rpot, left);
      setLed (LOW, HIGH, LOW);
      break;
    case 4:
      normal (bpot, right);
      normal (ypot, left);
      normal (rpot, center);
      setLed (LOW, LOW, LOW);
      break;
    case 5:
      normal (bpot, center);
      normal (ypot, left);
      normal (rpot, right);
      setLed (LOW, LOW, HIGH);
      break;
    case 6:
      normal (bpot, center);
      normal (ypot, right);
      normal (rpot, left);
      setLed (LOW, LOW, LOW);
      break;

    case 7:
      reverso (bpot, left);
      normal (ypot, center);
      normal (rpot, right);
      setLed (HIGH, HIGH, HIGH);
      break;
    case 8:
      reverso (bpot, left);
      normal (ypot, right);
      normal (rpot, center);
      setLed (HIGH, LOW, LOW);
      break;
    case 9:
      reverso (bpot, right);
      normal (ypot, center);
      normal (rpot, left);
      setLed (LOW, HIGH, LOW);
      break;
    case 10:
      reverso (bpot, right);
      normal (ypot, left);
      normal (rpot, center);
      setLed (LOW, LOW, LOW);
      break;
    case 11:
      reverso (bpot, center);
      normal (ypot, left);
      normal (rpot, right);
      setLed (LOW, LOW, HIGH);
      break;
    case 12:
      reverso (bpot, center);
      normal (ypot, right);
      normal (rpot, left);
      setLed (LOW, LOW, LOW);
      break;

    case 13:
      normal (bpot, left);
      reverso (ypot, center);
      normal (rpot, right);
      setLed (HIGH, HIGH, HIGH);
      break;
    case 14:
      normal (bpot, left);
      reverso (ypot, right);
      normal (rpot, center);
      setLed (HIGH, LOW, LOW);
      break;
    case 15:
      normal (bpot, right);
      reverso (ypot, center);
      normal (rpot, left);
      setLed (LOW, HIGH, LOW);
      break;
    case 16:
      normal (bpot, right);
      reverso (ypot, left);
      normal (rpot, center);
      setLed (LOW, LOW, LOW);
      break;
    case 17:
      normal (bpot, center);
      reverso (ypot, left);
      normal (rpot, right);
      setLed (LOW, LOW, HIGH);
      break;
    case 18:
      normal (bpot, center);
      reverso (ypot, right);
      normal (rpot, left);
      setLed (LOW, LOW, LOW);
      break;

    case 19:
      reverso (bpot, left);
      reverso (ypot, center);
      normal (rpot, right);
      setLed (HIGH, HIGH, HIGH);
      break;
    case 20:
      reverso (bpot, left);
      reverso (ypot, right);
      normal (rpot, center);
      setLed (HIGH, LOW, LOW);
      break;
    case 21:
      reverso (bpot, right);
      reverso (ypot, center);
      normal (rpot, left);
      setLed (LOW, HIGH, LOW);
      break;
    case 22:
      reverso (bpot, right);
      reverso (ypot, left);
      normal (rpot, center);
      setLed (LOW, LOW, LOW);
      break;
    case 23:
      reverso (bpot, center);
      reverso (ypot, left);
      normal (rpot, right);
      setLed (LOW, LOW, HIGH);
      break;
    case 24:
      reverso (bpot, center);
      reverso (ypot, right);
      normal (rpot, left);
      setLed (LOW, LOW, LOW);
      break;

    case 25:
      normal (bpot, left);
      normal (ypot, center);
      reverso (rpot, right);
      setLed (HIGH, HIGH, HIGH);
      break;
    case 26:
      normal (bpot, left);
      normal (ypot, right);
      reverso (rpot, center);
      setLed (HIGH, LOW, LOW);
      break;
    case 27:
      normal (bpot, right);
      normal (ypot, center);
      reverso (rpot, left);
      setLed (LOW, HIGH, LOW);
      break;
    case 28:
      normal (bpot, right);
      normal (ypot, left);
      reverso (rpot, center);
      setLed (LOW, LOW, LOW);
      break;
    case 29:
      normal (bpot, center);
      normal (ypot, left);
      reverso (rpot, right);
      setLed (LOW, LOW, HIGH);
      break;
    case 30:
      normal (bpot, center);
      normal (ypot, right);
      reverso (rpot, left);
      setLed (LOW, LOW, LOW);
      break;

    case 31:
      reverso (bpot, left);
      normal (ypot, center);
      reverso (rpot, right);
      setLed (HIGH, HIGH, HIGH);
      break;
    case 32:
      reverso (bpot, left);
      normal (ypot, right);
      reverso (rpot, center);
      setLed (HIGH, LOW, LOW);
      break;
    case 33:
      reverso (bpot, right);
      normal (ypot, center);
      reverso (rpot, left);
      setLed (LOW, HIGH, LOW);
      break;
    case 34:
      reverso (bpot, right);
      normal (ypot, left);
      reverso (rpot, center);
      setLed (LOW, LOW, LOW);
      break;
    case 35:
      reverso (bpot, center);
      normal (ypot, left);
      reverso (rpot, right);
      setLed (LOW, LOW, HIGH);
      break;
    case 36:
      reverso (bpot, center);
      normal (ypot, right);
      reverso (rpot, left);
      setLed (LOW, LOW, LOW);
      break;

    case 37:
      normal (bpot, left);
      reverso (ypot, center);
      reverso (rpot, right);
      setLed (HIGH, HIGH, HIGH);
      break;
    case 38:
      normal (bpot, left);
      reverso (ypot, right);
      reverso (rpot, center);
      setLed (HIGH, LOW, LOW);
      break;
    case 39:
      normal (bpot, right);
      reverso (ypot, center);
      reverso (rpot, left);
      setLed (LOW, HIGH, LOW);
      break;
    case 40:
      normal (bpot, right);
      reverso (ypot, left);
      reverso (rpot, center);
      setLed (LOW, LOW, LOW);
      break;
    case 41:
      normal (bpot, center);
      reverso (ypot, left);
      reverso (rpot, right);
      setLed (LOW, LOW, HIGH);
      break;
    case 42:
      normal (bpot, center);
      reverso (ypot, right);
      reverso (rpot, left);
      setLed (LOW, LOW, LOW);
      break;

    case 43:
      reverso (bpot, left);
      reverso (ypot, center);
      reverso (rpot, right);
      setLed (HIGH, HIGH, HIGH);
      break;
    case 44:
      reverso (bpot, left);
      reverso (ypot, right);
      reverso (rpot, center);
      setLed (HIGH, LOW, LOW);
      break;
    case 45:
      reverso (bpot, right);
      reverso (ypot, center);
      reverso (rpot, left);
      setLed (LOW, HIGH, LOW);
      break;
    case 46:
      reverso (bpot, right);
      reverso (ypot, left);
      reverso (rpot, center);
      setLed (LOW, LOW, LOW);
      break;
    case 47:
      reverso (bpot, center);
      reverso (ypot, left);
      reverso (rpot, right);
      setLed (LOW, LOW, HIGH);
      break;
    case 48:
      reverso (bpot, center);
      reverso (ypot, right);
      reverso (rpot, left);
      setLed (LOW, LOW, LOW);
      break;
  }
}

Schematics

Schematics
Crazypots ptuxa1qbcp

Comments

Similar projects you might like

Arduino Pocket Game Console + A-Maze - Maze Game

Project tutorial by Alojz Jakob

  • 12,331 views
  • 8 comments
  • 32 respects

Minesweeper Game with Arduino

Project tutorial by Lima NEXTPCB

  • 1,221 views
  • 0 comments
  • 0 respects

LED Roulette Game

Project tutorial by Arduino “having11” Guy

  • 14,027 views
  • 7 comments
  • 23 respects

Arduino Gyroscope Game with MPU-6050

Project tutorial by Mirko Pavleski

  • 3,534 views
  • 3 comments
  • 13 respects

Breadboard to PCB Part 1 - Making the Simon Says Game

Project tutorial by Katie Kristoff and Monica Houston

  • 9,847 views
  • 11 comments
  • 32 respects

Pixel Chaser Game

Project tutorial by Unexpected Maker

  • 6,819 views
  • 6 comments
  • 28 respects
Add projectSign up / Login