Skip to content

Example Arduino Code

zuk edited this page Nov 24, 2012 · 11 revisions

The following are some examples for how to write your Arduino code.

Some general notes:
  • void loop() is where all of your input happens. This function is continuously executed (several hundred times per second, depending on your Arduino hardware), so this is the place to put any sensor readings. The default example uses the Button library to make it easier to read input from simple buttons, but feel free to replace this to suit your needs. Just make sure that you have some code that signals the RPA that you are ready to battle (rpa.bow()) and that you have selected one of the three weapons (rpa.choose_ROCK(), rpa.choose_PAPER(), rpa.choose_SCISSORS()). This is the bare minimum for a playable RPA Arduino.

  • The event and state handlers are functions that get executed as the RPA finite state machine (FSM) transitions between different states. Event handlers get executed when events that cause transitions are triggered, and state handlers get executed when the state machine enters a new state. Consult RPA finite state machine UML for more info:

RPA Finite State Machine
RPA FSM

More info about finite state machines in general: http://en.wikipedia.org/wiki/Finite-state_machine

The T-Baum: Light Sensors & Tones

Full Code: https://github.com/miketissenbaum/rock_paper_awesome/blob/master/arduino/awesome/awesome.ino

In this version of RPA the standard buttons for choosing Rock, Paper, and Scissors have been replaced with light sensors, and the winning and losing alerts have been replaced with two different songs (similar to ring tones)

Setting up the light sensors

Note that we have to alter the loop() function to read the light sensors instead of taking input from Rock/Paper/Scissors buttons:

void loop() {
  lightReadingROCK = analogRead(PIN_ROCK);
  lightReadingPAPER = analogRead(PIN_PAPER);
  lightReadingSCISSORS = analogRead(PIN_SCISSORS);
  
  if (readyButton.uniquePress())
    rpa.bow();
  
  if (lightReadingROCK < 100){
    rpa.choose_ROCK();
  }
  
  if (lightReadingPAPER < 100){
    rpa.choose_PAPER();
  }

  if (lightReadingSCISSORS < 100){
    rpa.choose_SCISSORS();
  }

  rpa.check_input_from_serial();
}

We also need to add some code to the void setup in order to read the light sensors

void setup() {
  //checking light sensor
  pinMode(PIN_ROCK, INPUT);
  pinMode(PIN_PAPER, INPUT);
  pinMode(PIN_SCISSORS, INPUT);

And at the beginning of your code you need to redefine the pin inputs for PIN_ROCK, PIN_PAPER, PIN_SCISSORS and add a variable that stores the reading for each light sensor:

// pins that receive input for ROCK, PAPER, and SCISSOR - for this set up we need to use the Analog Pins to read the light sensors

#define PIN_ROCK A3 // Was 5
#define PIN_PAPER A4 // Was 6
#define PIN_SCISSORS A5 // Was 7

// Set up variable for light reading
int lightReadingROCK = 0;
int lightReadingPAPER = 0;
int lightReadingSCISSORS = 0;

Setting up the sounds

At the top of you code you need to include the "pitches.h" library (the library itself if already included in the rock_paper_awesome folder):

#include "pitches.h"

Then declare an arrays (for winning and losing), define the notes that will be played within each arrays and a second array for each that defines they durations of each note:

// ****** Sounds for winning a round ******
// notes for the win melody (for winning lab song):
int melodyWin[] = {
  NOTE_G4, NOTE_G4, NOTE_B4, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_E5, NOTE_D5, NOTE_CS5, NOTE_D5, NOTE_G4, 
NOTE_G4, NOTE_B4, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_E5, NOTE_D5, NOTE_B4, NOTE_G4};
  
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurationsWin[] = {
  8, 4, 4, 2, 8, 8, 8, 8, 8, 2, 8, 4, 4, 2, 8, 8, 8, 8, 8, 2};
// end of winning sounds

Finally you need to replace the Event Handler for 'void on_you_win()':

// when you've won
void on_you_win() {
  for (int thisNote = 0; thisNote < 20; thisNote++) {
    
    // to calculate the note duration, take one second 
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDurationWin = 1000/noteDurationsWin[thisNote];
    tone(13, melodyWin[thisNote],noteDurationWin);
    
    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDurationWin * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(13);
  }
} 

Clone this wiki locally