1

I am a true beginner here. Trying to build a "slot machine" type game and am in the beginning stages. Found a RNG for LCD code and tweaked it a bit to get three digits to display randomly. Worked just fine. But what I'm trying to accomplish is getting a red LED to blink when none of the RN match, blue when 2 RN match, and green when 3 RN match. What is happening now, is that the LCD will no longer generate random numbers when I press my button UNless I hold the button in for a couple of seconds. As soon as the random numbers appear my green led flashes briefly then the red and blue blink opposite of each other (red on 1 sec, off 1 sec, blue on 1 sec, off 1 sec, repeat......green only briefly when random numbers change). So after hours and hours of searching goggle and trying to figure this out on my own I am turning the folks who know what they are doing :)Please tell me what I am doing wrong.... Any help would be greatly appreciated. So why did the RNG stop working with a single press of my button, and where is my code wrong that gets the correct lighting operation? Here is my code so far

#include <WProgram.h>
 #include<Arduino.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

/* pin laayout of the LCD with the arduino:
arduino pin = LCD pins 
7=RS
8=E
9=D4
10=D5
11=D6
12=D7
RW=ground
*/

int inpin=2; //switchpin
int reading; // stocks switchpin

int inpin2=4; //switchpin
int reading2; //stocks switchpin

int inpin3=6; //switchpin
int reading3; //stocks switchpin

int ran; //stocks random number for dice roll 1 
int ran2; //stocks random number for dice roll 2
int ran3; //stocks random number for dice roll 3

int outpin=LOW;
int LED1=0; // red LED

int outpin2=LOW;
int LED2=1; // Blue LED

int outpin3=LOW;
int LED3=3; // Green LED

void setup()

 {
 lcd.begin(16,2);
   pinMode(LED1,OUTPUT);
   pinMode(LED2,OUTPUT);
   pinMode(LED3,OUTPUT);
 }

  void loop()
  {

    //reading of switch

    reading=digitalRead(inpin);
    reading2=digitalRead(inpin2);
    reading3=digitalRead(inpin3);

    //random numbers generation

    ran3=random(6);
    ran2=random(6);
    ran=random(6);

    {
    if (reading != reading2 != reading3);
      digitalWrite(LED1, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(1000);               // wait for a second
      digitalWrite(LED1, LOW);    // turn the LED off by making the voltage LOW
      delay(1000);               // wait for a second
  }

  {
    if (reading3 == reading2 || reading);
      digitalWrite(LED2, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(500);               // wait for a second
      digitalWrite(LED2, LOW);    // turn the LED off by making the voltage LOW
      delay(500);               // wait for a second
  }

  {
    if (reading==reading2==reading3)
      digitalWrite(LED3, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(250);               // wait for a second
      digitalWrite(LED3, LOW);    // turn the LED off by making the voltage LOW
      delay(250);               // wait for a second
  }

    // Set the screen for the PL1 and PL2 label on the top.

    lcd.setCursor(0,0);
    lcd.print("RL 1");

    lcd.setCursor(6,0);
    lcd.print("RL 2");

    lcd.setCursor(12,0);
    lcd.print("RL 3");

    //display of random number.

    if (reading==HIGH){
        lcd.setCursor(2,1);
        lcd.print(ran);
       delay(250);} // not really clean but used as a debouche founction. That way the µC can read only once the inpin within 250ms. i'll try to improve that. 


   if(reading2==HIGH){ //same as the player , only the location of the result on the screen is different  
       lcd.setCursor(6,0);
       lcd.print("RL 2");
       lcd.setCursor(8,1);
       lcd.print(ran2);
      delay(250);}

      if(reading3==HIGH){ //same as the player , only the location of the result on the screen is different  
       lcd.setCursor(12,0);
       lcd.print("RL 3");
       lcd.setCursor(14,1);
       lcd.print(ran3);
      delay(250);}

And here is the original code I found: http://www.desiquintans.com/articles.php?page=randuino (which also shows how it is wired up).

Tweaked to get the 3 random numbers and not just the two that it was set to:

#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

/* pin laayout of the LCD with the arduino:
arduino pin = LCD pins 
7=RS
8=E
9=D4
10=D5
11=D6
12=D7
RW=ground
*/

int inpin=2; //switchpin
int reading; // stocks switchpin

int inpin2=4; //switchpin
int reading2; //stocks switchpin

int inpin3=6;
int reading3;

int ran; //stocks randoms for player 1 
int ran2; //stocks random for player 2
int ran3;

void setup(){
  lcd.begin(16,2);
}

void loop(){

    //reading of switch

    reading=digitalRead(inpin);
    reading2=digitalRead(inpin2);
    reading3=digitalRead(inpin3);

    //random numbers generation

    ran3=random(6);
    ran2=random(6);
    ran=random(6);


    // Set the screen for the PL1 and PL2 label on the top.

    lcd.setCursor(0,0);
    lcd.print("RL 1");

    lcd.setCursor(6,0);
    lcd.print("RL 2");

    lcd.setCursor(12,0);
    lcd.print("RL 3");

    //display of random number.

    if (reading==HIGH){
      lcd.setCursor(2,1);
      lcd.print(ran);
      delay(250);} // not really clean but used as a debouche founction. That way the µC can read only once the inpin within 250ms. i'll try to improve that. 


   if(reading2==HIGH){ //same as the player , only the location of the result on the screen is different  
       lcd.setCursor(6,0);
       lcd.print("RL 2");
        lcd.setCursor(8,1);
        lcd.print(ran2);
      delay(250);}

      if(reading3==HIGH){ //same as the player , only the location of the result on the screen is different  
       lcd.setCursor(12,0);
       lcd.print("RL 3");
        lcd.setCursor(14,1);
        lcd.print(ran3);
      delay(250);}
}
  • reading != reading2 != reading3 doesn't work. Use reading != reading2 && reading != reading3. I don't get what the buttons are supposed to do. – Gerben Aug 1 '14 at 19:05
  • Thanks for replying Gerben :) will try that right away.. There is only one button and when it is pressed the random numbers are generated and displayed on the LCD, well at least that's what it did before I tried to add the lights. Thanks again – SWTORguy Aug 1 '14 at 19:08
  • 1 button? But you have 3 digitalReads? – Gerben Aug 1 '14 at 19:20
  • Did you have the button when your code was working with the LCD? Do you have a pull down resistor or any way to handle debouncing the switch? – Doowybbob Aug 1 '14 at 19:25
  • Yes.. one button and 3 digitalReads. I was under the impression that the digitalReads was the signal to the lcd from the arduino to generate 3 different random numbers when one button was pushed. – SWTORguy Aug 1 '14 at 19:26
2

So after a ton more research I figured out what I was doing wrong. For anyone who reads these answers, I found two web sites to be an enormous help. They are http://playground.arduino.cc/uploads/Main/arduino_notebook_v1-1.pdf and http://mikrohw.wz.cz/HakDuino/Arduino_Cookbook.pdf Good luck to you and have fun with your project. Thank you Gerben & Doowybbob for responding and trying to get me to look in the right direction. Turns out that a lot of my code was correctly heading in the right direction. But in my if statements, I wasn't addressing all the possible states. So now the LEDs light up at the appropriate time and I also added a buzzer. Here is my final code...... Thanks and Cheers, SWTORguy

#define PIEZO 5 // pin 5 is capable of PWM (Pulse width modulation) output to drive tones
#include <WProgram.h>
#include<Arduino.h>
#include <LiquidCrystal.h>
  LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

/* pin laayout of the LCD with the arduino:
arduino pin = LCD pins 
7=RS
8=E
9=D4
10=D5
11=D6
12=D7
RW=ground
*/

const int button = A5;  // changes 2 to an integer from a constant
int buttonState;
int prevButtonState;

int inpin=2; //switchpin (rolling of dice1)
int reading; // stocks switchpin

int inpin2=4; //switchpin (rolling of dice2)
int reading2; //stocks switchpin

int inpin3=6; //switchpin (rolling of dice3)
int reading3; //stocks switchpin

int ran; //stocks randoms for dice 1 
int ran2; //stocks random for dice 2
int ran3; // stocks random for dice 3

int outpin=LOW;
int LED1=13; //red LED

int outpin2=LOW;
int LED2=1; //blue LED

int outpin3=LOW;
int LED3=3; //green LED

int outpin4=LOW;
int BUZ=5; //piezio Buzzer

int del = 500; //buzzers delay time for inbetween LED flashes

void setup(){
   lcd.begin(16,2);
   pinMode (PIEZO, OUTPUT); //pin mapping
   pinMode(LED1,OUTPUT); //pin mapping
   pinMode(LED2,OUTPUT); //pin mapping
   pinMode(LED3,OUTPUT); //pin mapping
   pinMode (button, INPUT); //pin mapping
   pinMode (reading, INPUT); //pin mapping
   pinMode (reading2, INPUT); //pin mapping
   pinMode (reading3, INPUT); //pin mapping
}

void loop(){
    buttonState = digitalRead (button);
    if (buttonState != prevButtonState && buttonState == HIGH) //pushing the button
    {
    //reading of switch    
    reading=digitalRead(inpin);
    reading2=digitalRead(inpin2);
    reading3=digitalRead(inpin3);

    //random numbers generation    
    ran3=random(1,7); // #'s 1 through 6
    ran2=random(1,7); // #'s 1 through 6
    ran=random(1,7); // #'s 1 through 6   


    // Set the LCD screen for dice 1-3 label on the top.    
    lcd.setCursor(0,0);
    lcd.print("die");

    lcd.setCursor(5,0);
    lcd.print("die2");

    lcd.setCursor(11,0);
    lcd.print("die3");

    //display of random number.    
    if (reading==HIGH){
        lcd.setCursor(2,1);
        lcd.print(ran);
        delay(250); // not really clean but used as a debounce function.
    }

    if(reading2==HIGH){  
       lcd.setCursor(5,0);
       lcd.print("die2");
       lcd.setCursor(8,1);
       lcd.print(ran2);
      delay(250);
    }

    if(reading3==HIGH){
       lcd.setCursor(11,0);
       lcd.print("die3");
       lcd.setCursor(14,1);
       lcd.print(ran3);
       delay(250);
    }

    /*the logic that tells which LED to blink and when to sound the buzzer
      dependent upon which state the random numbers dispayed are in*/
    if (ran == ran2 && ran2 == ran3) // random number state (3 of a kind)
    {
      for (int c=0; c <= 2; c++) // variable defining how many times the green LED will blink and buzzer sound
      {
          digitalWrite(PIEZO, LOW); // buzzer off
          delay(del); // wait for .5 second
          digitalWrite(LED3, HIGH);   // Green - turn the LED on (HIGH is the voltage level)
          delay(100);               // wait for a second
          digitalWrite(LED3, LOW);    // turn the LED off by making the voltage LOW
          delay(100);               // wait for a second
          analogWrite(PIEZO, 128); //Buzzer on
          delay(del); // wait for .5 second
      }
      digitalWrite(PIEZO, LOW); //buzzer off
    }else{
        if(ran == ran2 || ran2 == ran3 || ran == ran3) // random number state (2 of a kind)
        {
            for (int b=0; b <= 2; b++) // variable defining how many times the blue LED will blink and buzzer sound
            {
                digitalWrite(PIEZO, LOW); // buzzer off
                delay(del); // wait for .5 second
                digitalWrite(LED2, HIGH);  //Blue - turn the LED on (HIGH is the voltage level)
                delay(100);   // wait for a second
                digitalWrite(LED2, LOW);  // turn the LED off by making the voltage LOw
                delay(100);   // wait for a second
                analogWrite(PIEZO, 128); //Buzzer on
                delay(del); // wait for .5 second
            }
            digitalWrite(PIEZO, LOW); //buzzer off
        }else{
            for (int a=0; a <= 10; a++) // variable defining how many times the red LED will blink.
            {
                digitalWrite(LED1, HIGH);   // Red - turn the LED on (HIGH is the voltage level)
                delay(100);               // wait for a second
                digitalWrite(LED1, LOW);    // turn the LED off by making the voltage LOW
                delay(100);               // wait for a second
            }
        }
    }

  }   
}
  • 2
    On behalf of the Stack exchange community, thank you for posting a follow-up message explaining the solution you found (voted.) That turns a newbie question into an asset that others can learn from. BTW, parts of your answer are missing code tags, making it harder to read. You might want to edit your answer and clean it up. – Duncan C Aug 5 '14 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.