Introduction: Magic 8 Ball Guess Game
Introduction:
Hello, I am Kay and this is my first Arduino project for school.
In this Instructable I will give the basics of making a guess game with sensors in Arduino.
For this you'll need:
- Arduino UNO
- Potentiometer
- FlexMeter (In my case the flexmeter from IEFSR.com, standard with the starterpack)
- 3x 10K Ohm Resistors.
- 2x 22. Ohm Resistors.
- A green Led.
- A red Led.
- A button.
If you want, you can add more sensors so it will be more challenging.
Let's get started!
Step 1: Assigning the Potentiometer
So in this step we are adding the potentiometer.
As you can see I added a resistor at the output of the potentiometer.
For some reason I got strange values from the potentiometer without the resistor.
In the next step we will assigning the Flexmeter.
Step 2: Assigning the Flexmeter
In this step we add the flexmeter.
I added a 10K resistor again to the output of the flexmeter.
Step 3: Assigning the Reset
in this step we just connect RESET with A3
Step 4: Assigning the Button
We connect the button with the digital 2 pin.
Make sure you have 10K ohm resistor for the ground
Step 5: Installing the LEDs
Now we connect the two LEDs (red and green). These are also connected to the digital pins.
don't forget to set the right resistors.
Step 6: Code: Lets Add Some Variables
In the first lines of your code add the pins used by the sensors.
So for the:
buttonPin = 2;
redLightPin = 3;
greenLightPin = 5;
Potentiometer = A0
flexsensor = A1
Reset = A3
After that add two value variables one for the potentiometer and one for the flexsensor.
Also add an empty r and buttonState integer, we'll need it later on.
Step 7: Setting Up the Setup
In the setup() function we first add the randomSeed() function which is assigned an analogRead from pin A5.
So you will get something like this:
randomSeed(analogRead(A5));
After that we define the pinmodes for buttonPin, redLightPin and greenLightPin:
pinMode(buttonPin, INPUT);
pinMode(redLightPin, OUTPUT);
pinMode(greenLightPin, OUTPUT);
Remember r which we defined with the variables? Well we are going to give it some values now:
r = random(0, 2);
after this we finish with the Serial.begin(9600);
Step 8: Adding the Loop
In the loop we don't add much because we are going to make some extra functions.
But we are going to put some essential code in this function.
First of all we are going to check the buttonstate:
buttonState = digitalRead(buttonPin);
after this we call a function called isOn():
isOn();
Step 9: Adding the IsOn Function
in this function we check if the button is pressed by checking the buttonState we assigned in the loop function:
void isOn(){
if(buttonState == 1){
Run();
}
}
Step 10: Adding the Run Function
The Run() function is going to have most of the code.
First of all we want to know which value of r the random assigned so we can debug the game and see which sensor the random chose:
Serial.println(r);
After that we check with an IF statement which one was chosen and add some code that is separate from the other sensor:
if(r == 0){
potValue = analogRead(potentiometerPin);
potValue = map(potValue, 0, 1024, 100, 200);
//Serial.println(potValue);
oldValue = potValue;
}
As you can see we read the pin the potentiometer is connected to and assign the value gained from that to potValue, next we map the value to a value between 100 and 200 in linear parallel to the 0 to 1024 gained from the pin itself.
the print is a debug line. so add if you want, but is useful for testing.
we also assign the potValue variable to an oldValue variable so we can compare if the player changed the sensor.
We do the same for the other sensor, you can reapeat this with as many sensors as you want.
else if(r == 1){
fsrValue = analogRead(fsrPin);
fsrValue = map(fsrValue, 0, 1024, 100, 200);
//Serial.println(fsrValue);
oldValue = fsrValue;
}
after this we add the delays and a countdown timer:
Serial.println("Time: 5");
delay(1000);
Serial.println("Time: 4");
delay(1000);
Serial.println("Time: 3");
delay(1000);
Serial.println("Time: 2");
delay(1000);
Serial.println("Time: 1");
delay(1000);
Serial.println("no time left");
delay(10);
After this delay we check each sensor for an extra time so we can next check each sensor.:
fsrValue = analogRead(fsrPin);
fsrValue = map(fsrValue, 0, 1024, 100, 200);
potValue = analogRead(potentiometerPin);
potValue = map(potValue, 0, 1024, 100, 200);
Serial.println(oldValue);
This println is also for debug.
Now we are going to check if the value of the sensors changed after 5 seconds, if not: we switch the red LED on, if correct: we switch the green LED on.
if(r == 0){
if(oldValue <= potValue + 10 and oldValue >= potValue - 10){
digitalWrite(redLightPin, HIGH);
}
else{
digitalWrite(greenLightPin, HIGH);
}
}
else if(r == 1){
if(oldValue <= fsrValue + 10 and oldValue >= fsrValue - 10){
digitalWrite(redLightPin, HIGH);
}
else{
digitalWrite(greenLightPin, HIGH);
}
}
so here we checked which of the 2 sensors was chosen by the random function after that we check if the value of the sensor changed from the value before the countdown.
Now we add some last things to reset the random and the whole script:
delay(1000);
r = random(0, 2);
analogWrite(resetPin, HIGH);
You will also need to declare the Run() function where the variables are:
void Run();
Step 11: Conclusion and Last Words
So you should now have the basis of a guess game with a few sensors/meters to begin playing with.
When you have a display you can let the chosen values be displayed at that display together with the amount of time left.
You can also add some more sensors to make it a bit harder to find the right sensor/meter.
I am very new to Arduino and the code and connections could probably be much easier or efficiently but this was my very first Arduino project (for school).
The whole code will be downloadable as a .ino file with this tutorial.
Kay