Mission 15 - Guess a Number - Rotary Encoder
 DuinoKit Essentials - View Only. See new "Mission Manual" forum for manual and code   Started by Daniel Alich   2017-06-27 21:27:39 -04:00   Comments: 1    Viewed: 974

  1. Daniel Alich
    Daniel Alich Member Staff Member
    252_1511883500_Mission 15 – guess a nu

    #include <LiquidCrystal.h>
    /*
    This program was adapted from electronicsbug.wordpress.com
    I could not find the Random number library as suggested in this code
    so I utilized the Arduino C++ Random function in its place. Also added in was
    Rotary Encoder and LCD modules
    DuinoKit.com
    */

    #define ENC_A 8
    #define ENC_B 9
    #define ENC_PORT PINB
    LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
    // (RS,E, D4, D5, D6, D7)
    int computerNumber;
    int lastGuess;
    boolean inGame;

    void setup()
    {
    Serial.begin(9600);
    lcd.begin(16, 2);
    pinMode(ENC_A, INPUT);
    digitalWrite(ENC_A, HIGH);
    pinMode(ENC_B, INPUT);
    digitalWrite(ENC_B, HIGH);
    inGame = false;
    digitalWrite(6,HIGH); // Set pin 6 HIGH for press of rotary encoder
    }

    void loop ()
    {
    while(!inGame)
    {
    Serial.println("Guessing Game");
    Serial.println("The computer has picked a number between 1 and 100");
    lcd.setCursor(0, 0);
    lcd.print("The computer has picked a number between 1 and 100");
    Serial.println("Send anything to start the game");
    lcd.setCursor(0, 1);
    lcd.print("Send anything to start the game");
    randomSeed(analogRead(0));
    while(digitalRead(6) == HIGH) {
    } // Wait for user to enter somehting in serial
    computerNumber = random(0, 99)+1 ; // Get random number here
    delay(300);
    inGame = true;
    }
    while(inGame)
    {
    while(Serial.available() > 0)
    {
    delay(100);
    Serial.read (); // read value from serial
    }
    Serial.println("Please enter a guess...");
    while(digitalRead(6) == HIGH) {
    static uint8_t counter = 0; //this variable will be changed by encoder input
    int8_t tmpdata;
    tmpdata = read_encoder();
    int MapValue;
    if( tmpdata ) {
    // Serial.print("Counter value: ");
    // Serial.print(counter, DEC);
    // Serial.print(" value: ");
    lastGuess=map(counter,0,150,0,100);
    Serial.print(lastGuess, DEC);
    Serial.print(" ");
    counter += tmpdata;
    }
    }
    delay(300);
    // lastGuess = Serial.read()-48;
    // if(Serial.available() >0)
    // {
    // lastGuess=lastGuess *10 + Serial.read()-48;
    // delay(100);
    // if(Serial.available() >0)
    // {
    // lastGuess = lastGuess * 10 + Serial.read()-48;
    // }
    // }
    if(lastGuess <1 || lastGuess > 100)
    {
    Serial.println("Invalid entry, pleast try again");
    return;
    }
    Serial.println("");
    Serial.print(lastGuess);
    if(lastGuess == computerNumber)
    {
    Serial.println(" was correct. You Win!");
    inGame=false ;
    }
    else if (lastGuess < computerNumber)
    {
    Serial.println (" is too low");
    }
    else if (lastGuess > computerNumber)
    {
    Serial.println(" is too high") ;
    }
    }
    }

    /* returns change in encoder state (-1,0,1) */
    int8_t read_encoder()
    {
    static int8_t enc_states[] = {
    0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0 };
    static uint8_t old_AB = 0;
    old_AB <<=2 ; //remember previous state
    old_AB |= ( ENC_PORT & 0x03 ); //add current state
    return ( enc_states[( old_AB & 0x0f )]);
    }

    Daniel Alich, 2017-06-27 21:27:39 -04:00
  2. (You must log in or sign up to post here)

Report Post

Write your reason

Log in | Sign up
Terms & Conditions!
Help!