NumberGuesser on Arduino UNO

243

3

1

Hi, first of all sorry if i make any error, if so please correct me.

Today i'm going to show you how to make a little game on the arduino, a pretty basic one. It consists in generating a random number, in my case from 0 to 9, and guess it using a little potentiometer and a button (or a little LDR, 'cause it looks cool).

I was inspired to make this from another instructable, its almost the same but written in python and with different difficulties. Here's the link: https://www.instructables.com/id/Python-Tutorials-A...

Let's start.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Things Needed.

Hardware:

-A bunch of wires.

-A button or a ldr (if you want to look cool).

-A seven segment display. If you want to add more difficulty you can add a dual one or so.

-A potentiometer.

-Two leds, one needs to represent that you fail and the other that you win.

-A 10k resistor

-An Arduino UNO, but it should work on any that have enough pins(~13 needed). I'm using a clone, a Sainsmart UNO R3.

Software:

-Arduino IDE

-SevenSeg library

That's it.

Step 2: Wiring the Display

This step really depends on your display (the model).

First, find what model is your display then google it and search for the datasheet. Once you have found the datasheet, locate the pinout part. If your display is dual and comes with all the pins, you need to connect the pins A1 to A2, B1 to B2 and so on.

Then, connect the pins to your arduino, pin A of the display to pin 2 on the arduino, pin B to pin 3, pin C to pin 4 and so on. If you want to connect the dot (normally displayed as DP on datasheets) connect it to the pin that follows the one for G, in our case the 9. Then connect the common pins of the display to the arduino on different pins. i.e. Common 1 to pin 11 and Common 2 to pin 12.

Once you get here, make sure you have installed the SevenSeg library on your Arduino IDE. If not, here's the link: http://playground.arduino.cc/Main/SevenSeg

NOTE: The photo is completely wrong, it is just to show the pins that i'm using on the arduino.

Step 3: Wiring the Rest of the Things.

Now we need to wire the potentiometer, the LDR (or button), and the LEDs.

The potentiometer is wired the same way as always (1 to 5V, 2 to A0 and 3 to GND).

The button (or ldr) is wired, again, the same way it should be wired, one pin to 5V and the other to A4 and GND (using the 10k resistor).

The leds need to be wired so they share the positive pole, the one that represents win needs to be connected from the negative side to A5 and the other (the one that represents fail) needs to be connected from ground to A3. The common positive goes to 3.3V.

And that's it.

Step 4: Programming Time :D

#include <SevenSeg.h>

int i;

int a;

int w;

SevenSeg disp(2,3,4,5,6,7,8); //change these numbers according to the format (A,B,C,D,E,F,G)

const int numOfDigits=2; //change this according to the number of digits of your display

int digitPins[numOfDigits]={11,12}; //common pin(s)

void setup() {

disp.setDigitPins(numOfDigits,digitPins);

pinMode(A5,OUTPUT);

pinMode(A3,OUTPUT);

pinMode(A0,INPUT);

pinMode(A4,INPUT);

//test for everything working

luzo();

luzr();

disp.write(88); //change the 88 to 8 if your display only have 1 digit

delay(250);

disp.write("");

luzo();

luzv();

delay(250);

luzo();

w = random(0,9); //makes a random number between 0 and 9, to make more difficult change the 9 to 99

}

void loop() {

i = map(analogRead(A0),0,1023,0,9); //to make more difficult change the 9 to 99

a = (10 * i) + i; //delete this if you changed the 9 to 99 or your display only have 1 digit

disp.write(a);

if(analogRead(A4) <= 100){ //change this to (digitalRead(A4) == HIGH) if you used a button

if(i == w){

luzv();

delay(1000);

asm volatile (" jmp 0"); //resets the program if you win

}

if(i != w){

luzr();

delay(500);

luzo();

}

}

}

void luzr(){

digitalWrite(A3,0);

}

void luzv(){

digitalWrite(A5,0);

}

void luzo(){

digitalWrite(A3,1);

digitalWrite(A5,1);

}

Upload it and have fun.

Step 5: Final Product

Move the potentiometer to change the number on the display and touch the button or the ldr to check, if you guessed it, win led turns on and resets the game, if not, fail led turns on for a second.