Arduino Projects

A Glance into the Future

arduino Project look to the future cover

To admit it right from the start: You won’t predict the next lottery numbers with this project. But you will learn how to control a servo motor and let it “look into the future ” with the help of a few random numbers.

For this Arduino Project you need:

  • Arduino Board
  • Breadboard
  • Servo motor
  • Capacitor
  • 10k Ohm Resistor
  • Switch
  • Wires

Simple set up – with a little difficulty

Arduino Project Looking to the future with random numbers

Building this project on the breadboard is easy. Place the servo motor in the middle of the breadboard and fix it e.g. with a wire as shown in the photo below. This is not the most stable solution, but it is enough to keep it in place.

Plug the motor connector on the left side of the breadboard and connect plus (red) and minus (black) to the power bar. In the middle – usually the white wire – you put a connection to the digital pin 2 on the Arduino.

If you use an electrolytic capacitor (like me in this project), you have to be careful now. Put the capacitor on the board and connect the short end to the negative pin. The long one you plug two holes further to the positive pin.

Important: Make sure to connect the capacitor the right way.

Make sure again that the capacitor is installed the right way – also by the labeling on the capacitor itself. Installing it the wrong way round can be dangerous. Because then it can explode. How this looks like and further basics about capacitors you will learn in the following video.

YouTube

Mit dem Laden des Videos akzeptieren Sie die Datenschutzerkl├Ąrung von YouTube.
Mehr erfahren

Video laden

Now you need a switch that you press after you have asked your question and that starts the prophecy.

You set up the switch over the bridge of your breadboard, so that the two upper pins on one side and two lower pins on the lower side are plugged in. Connect the pin on the upper right side with plus. The pin in the upper left corner is connected by a wire to the digital input 3 of your Arduino. Additionally you connect it with a 10k Ohm resistor to minus. When you are done, it should look something like this:

Switch on the Arduino

Now you are done setting up this project. The only thing missing is a display for the answers.

The simplest of all displays

I simply cut out a piece of paper so that it fits around the Servo-Moto. On the left side is the No, on the right side the Yes and in the middle the Maybe.

Sure you are more creative and do something prettier. ­čÖé But to make sure the code below matches the display, take care to distribute the answers as mentioned above.

Generate random numbers

At the core of your project are random numbers. Your Arduino should answer your questions with either yes, no or maybe. This means that you have to generate 3 numbers, each representing one of the 3 answers.

This is quite simple. All you need is the function random().

For this project, we would simply want the numbers 0, 1, or 2. For this, we enter the number 3 between the brackets of the random function. Confusing? At first sight, yes, but you have to keep in mind – as so often when programming – that the last number is exclusive. That means it is the outer limit and is not added. As if you were writing: < 3.

Learn more about random numbers on the Arduino.

The finished code

Now all you need is the sketch that makes your fortune teller work. We have already looked at the generation of the random numbers. Also important is the control of the servo motor.

Load the following code onto your Arduino and you’re ready to go

#include <Servo.h> //Servo-Bibliothek einbinden
Servo myServo; //Servo-Objekt "myServo" erstellen

int angle; //Position, die der Servo anfahren soll
int switchState = 0; //Status des Schalters
int reply; //Generierte Zufallszahl

void setup() {

pinMode(3, INPUT);
  
myServo.attach(2); //Der Servo liegt an Pin 2
myServo.write(90); //1. Position des Servos - 90┬░

Serial.begin(9600);
}

void loop() {

switchState = digitalRead(3); //Lesen, ob der Schalter gedr├╝ckt wurde

  if(switchState == 1){
    reply = random(3); //Zufallszahl von 0 bis 2 generieren
    Serial.print(reply);
    
    switch(reply){
      
      case 0:
      myServo.write(0); //Falls 0, Servo nach links fahren - 0┬░
      delay(2000);
      break;

      case 1:
      myServo.write(90); //Falls 1, Servo nach oben fahren - 90┬░
      delay(2000);
      break;

      case 2:
      myServo.write(180); //Falls 2, Servo nach rechts fahren - 180┬░
      delay(2000);
      break;
      }
    }
}

What’s next?

You now have a simple framework for your fortune teller. Be creative and come up with some improvements! For example a display of the answers on a LCD screen. Or an LED that lights up when the switch is pressed.

You may also like

Comments are closed.