Introduction to Arduino Programming 7: The Oracle Arduino

#Introduction to Arduino Programming
A series of based on New York University’s Introduction to Computer Programming class from the Courant Institute of Mathematical Sciences.

http://cims.nyu.edu/~kapp/common/CSCI-UA.0002/fall2013/

This is intended as a “gentle” introduction to computer programming and Arduino. The only prerequisite is that you’ve completed our Arduino Basics guide!

The Oracle Arduino :video_game:

Ever wonder if your secret crush is into you? If you’re going to strike it rich? Or if it’s going to rain today? Let the Arduino Oracle tell you your future.

By using the Arduino’s random number generator we can use the value it gives as a response to our burning questions with a YES, NO, and MAYBE state.

Use the code for this program as a base to create your own game as well!

###Decisions Decisions! ( a reminder )

Now onto the good stuff! So if you remember back in day when we first met in the getting started guide I mentioned that the Arduino has the ability to create games. There two reasons why:

  • Decisions: The Arduino programming language ( which is based off of the programming language called C ) has the ability to make decisions. But so what? When making a game we need determine as won or lost. The ability to make decisions helps us do that according to the rules of our game.

  • Memory: When we make variables, we’re basically making a named space in the Arduino’s memory. But so what? Well when determine if someone has one or lost we need to keep score. Variables can help us do that.

(Fun)ctions!

We’ve been using functions for years now. loop(), setup(), serial.print(), analogWrite(). But the Arduino can let us create our own. Functions are great for code reuse and readability. What means is that when our programs get bigger and bigger they can get harder to read.

Ok so what are we doing?

We’re going to create a program that will take the input value from a button then generate a random number. Based on that number we will turn the arm of a servo.

We also have graphic that we can use to communicate the answer from the Oracle Arduino to the player!

https://instagram.com/p/2_ZXBKHW0U/?taken-by=sssyed

Our Circuit:

The code (you’ll need to scroll)

// Made by Syed Salahuddin
// License: CC-BY-SA 3.0
// set pin numbers to represent our bits:
const int buttonPin = A0;
const int servoPin = 5;
const int i = 0;

int buttonState = 0;
int randomNumber = 0;
bool getRandomNumber = false;


void setup() {
  // initialize the Servo as an output:
  pinMode(servoPin, OUTPUT);

  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop(){

  if(getRandomNumber == false){
    attractMode();
  }

  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  if(getRandomNumber == false){
  	randomNumber = random(3);
    delay(100);
  }

  if (buttonState == HIGH) {
    Serial.print(randomNumber);
    getRandomNumber = true;

    if (randomNumber == 1){
      moveServo(1);  // moveServo to TRUE state
    } else if(randomNumber == 2){
      moveServo(2); // moveServo to MAYBE state
    } else {
      moveServo(0); // movesServo to FALSE state
    }
  }
}

void moveServo(int fortune){
  analogWrite(5, 0);
  analogWrite(5, 125);
  analogWrite(5, 230);

  if(fortune == 0){
    analogWrite(5, 0);
    delay(2000);
  } else if(fortune == 1) {
    //no
    analogWrite(5, 125);
    delay(2000);
    } else {
    //maybe so
    analogWrite(5, 230);
    delay(2000);
  }
  getRandomNumber = false;

}

void attractMode(){
    analogWrite(5, 0);
    delay(200);
    analogWrite(5, 128);
    delay(150);
    analogWrite(5, 230);
    delay(200);
}

###The Breakdown:

Our variables:

const int buttonPin = A0;
const int servoPin = 5;
const int i = 0;

int buttonState = 0;
int randomNumber = 0;
bool getRandomNumber = false;

The button and servo variables are pretty straight forward. You’ll notice that there is a new type, const int, it just means that this number will not change and will stay the same or constant.

We also have buttonState that will tell us if our button is pressed or not.

And randomNumber will store our random number when it’s been generated by our Arduino.

getRandomNumber will store a true / false value after we check if a button is pressed. We’re also coming across a totally new type called a bool, short for boolean. All you need to know about booleans is that they evaluate to true or false.

Setup:

void setup() {
  // initialize the Servo as an output:
  pinMode(servoPin, OUTPUT);

  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

Pretty straightforward, the servo is output and the button is input.

Function calls!

void loop(){

  if(getRandomNumber == false){
    attractMode();
  }

This is where things start heating up. This code means that if we don’t have a random number then call our function attractMode()

In games an attract mode entices players to play a game, in this case we will wiggle the arm until someone presses a button. This is a completely new function that we’ve defined (below)!

Here we will get the value of our button and check if we’ve gotten a randomNumber.

  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  if(getRandomNumber == false){
  	randomNumber = random(3);
    delay(100);
  }

random(3) function will generate a random number for us. It will be between 0 and 2 (3 integers)

Our button state:

if (buttonState == HIGH) {
    Serial.print(randomNumber);
    getRandomNumber = true;

if our button has been pressed we let getRandomNumber know that we need to get a random number, so we set it to true.

HIGH is a constant defined by the Arduino software.HIGH denotes a button press and LOW represents the opposite.

Now let’s use some decision structures to evaluate our different states. So if we get 1 that means we move the servo to “YES”, 2 is “MAYBE”, and 3 is “NO”. We also have yet another new function that we’re creating ourselves called moveServo()

    if (randomNumber == 1){
      moveServo(1);  // moveServo to YES state
    } else if(randomNumber == 2){
      moveServo(2); // moveServo to MAYBE state
    } else {
      moveServo(0); // movesServo to NO state
    }
  }

Our servo moving function!

void moveServo(int fortune){
  analogWrite(5, 0);
  analogWrite(5, 125);
  analogWrite(5, 230);

  if(fortune == 0){
    analogWrite(5, 0);
    delay(2000);
  } else if(fortune == 1) {
    //no
    analogWrite(5, 125);
    delay(2000);
    } else {
    //maybe so
    analogWrite(5, 230);
    delay(2000);
  }
  getRandomNumber = false;

}

Now I know this looks really scary but it’s not that complicated . We have a servo with an parameter that is our fortune value (0, 1, or 3). We do some more attractMode wiggling before we actually move the servo to it’s place using analogWrite. We give the decision 2 seconds and then set getRandomNumber to false. We do that because after we’ve gotten our fortune we want to reset our Oracle and go back to our attract mode.

Our attract mode function!

void attractMode(){
analogWrite(5, 0);
delay(200);
analogWrite(5, 128);
delay(150);
analogWrite(5, 230);
delay(200);
}

So as you might have observed, defining our function is as easy as setup() or loop(). Our type will be void meaning that it’s just moving our servo around not much else ( there is some delay added so the servo doesn’t just jitter )

Quick overview

There’s also this really quick screen cast that covers the programming parts!

That’s it!

That’s first taste of slightly more complex coding concepts! Nice work!

HAVE FUN

Troubleshooting

On the physical side, if you’re having trouble with the servo at all try switching to the p3 power, the extra juice will keep you from draining your batteries!


And checkout our troubleshooting guide!

Looking for more inspiration?

Check out some rad project’s from Dave Saunder’s game design class!

“House of Horrors boardgame that included flying snake heads, a deadly buzzsaw, falling cars, and a level boss who had to be defeated before a player could claim victory.”
Found out more about David’s process and his here: https://medium.com/@DesignSaunders/maker-night-game-design-construction-4446d4a2ef9c

How can you make a game with littleBits?

  1. First think of some rules for your game. Use familiar games like tag or hot potato as a start
  2. How do you win or lose in your game? Will you use points or time to determine the end of your game?
  3. Now remix your game so you can make it harder or easier to play. Try to create a game that starts out in easy mode and progresses to medium and hard modes. Does your game have a story or a character that’s on a quest?
1 Like

Bridget here from ITP. Mind if I share this on Twitter with @ITPalumni? :bitstar:

Go for it! If you don’t mind I’m cleaning up some of the code. I’ll let you know when it’s totally ready to go :slight_smile:

Also feel free to @tigoe I think he’d love to see it! I’m twitter as @sssyed and of course littleBits is @littleBits.

Standing by for clean code… Did you all have fun with NY Creative Mornings playtime? Maybe they’re still there? http://creativemornings.com/talks/MEjEj7nxxPDX-qRQLbp91A==

Yeah it was awesome!

Ok feel free to post away!

for some reason my arduino is failling to understand what the attract mode function is