Arduino Excuse Generator

From SainSmart Wiki
Revision as of 07:27, 16 July 2013 by Admin (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Arduino Excuse Generator

Need an excuse in a hurry?

Look no further than the Excuse Generator.

File:A3.jpg

Step 1: What You Need / Wire it Up

A4.jpg

We are using:

       Arduino Uno
       LCD Module (HD44780 compatible)
       Potentiometer
       Tilt Switch
       220 Ohm Resistor (Red-Red-Black-Black)  / (Red-Red-Brown) or 470 Ohm or 560 Ohm is fine too


Wire up the LCD module to the Arduino as listed below and illustrated in the Diagrams. Don't forget to wire in the Pot and the Tilt Switch too as shown.


Wire up the LCD Module as follows:

   LCD pin (1)   / Vss      -> Gnd
   LCD pin (2)   / Vdd    -> 5V
   LCD pin (4)   / RS      -> 12
   LCD pin (5)   /  R/W  -> Gnd
   LCD pin (6)   / E         -> 11
   LCD pin (11)  / D4      -> 5
   LCD pin (12)  / D5      -> 4
   LCD pin (13)  / D6      -> 3
   LCD pin (14)  / D7      -> 2

In addition you need to wire 5V/Gnd to the A/K of the backlight LED (pins 15/16); and wire in a potentiometer to adjust the contrast (pin 3).

Without the contrast adjustment and the backlight the LCD will not Display anything visible.

Step 2: Here's the Code

A5.jpg

(Apparently Instructables now Require All Steps to have a picture. So please enjoy this picture of the code on my screen :-) )

Please double check the =5V/Gnd Wiring. You can PERMANENTLY DAMAGE your board if this is wrong.

Once wired up, try the Hello World! Example to check everything is OK. You may need to adjust the potentiometer to see the Hello World! message.

(File -> Examples -> LiquidCrystal -> HelloWorld)

OK, so let’s proceed.

We’ve attached a tilt sensor to Arduino pin d10 so whenever you tap the sensor it activates an Excuse to be generated.

Here’s the code:

/* Excuse Generator by Anthony Kelly

Adapted from Arduino Starter Kit example Project 11 - Crystal Ball

Parts required: Arduino UNO SainSmart LCD (HD44780 compatible) Tilt Sensor

http://arduino.cc/starterKit

This example code is part of the public domain

  • /

// include the library code:

  1. include <LiquidCrystal.h>
  2. include <string.h>

// initialize the library with the numbers of the interface pins LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// set up a constant for the tilt switchPin const int switchPin = 10;

// variable to hold the value of the switchPin int switchState = 0;

// variable to hold previous value of the switchpin int prevSwitchState = 0;

// a variable to choose which reply from the crystal ball int reply;

int i;

// Define your excuses char *start []= {"I'm sorry but ","Don't blame me ", "Not my fault ","Guess what happened "}; char *middle []= {"Godzilla ","Chuck Norris ", "Scrooge McDuck ","Soap McTavish"}; char *ends []= {"tried to kill me. ","ate my homework.","came after me.","stole my head."};

int length = 4;

void setup() {

 // set up the number of columns and rows on the LCD
 lcd.begin(16, 2);
 // Set the pullup on the switch pin
 pinMode(switchPin, INPUT);
 digitalWrite(switchPin, HIGH);

}

void loop() {

   // clean up the screen before printing a new reply
 lcd.clear();
   // Print a message to the LCD.
 lcd.print("Need an");
 // set the cursor to column 0, line 1
 // line 1 is the second row, since counting begins with 0
 lcd.setCursor(0, 1);
 // print to the second line
 lcd.print("Excuse?");
 for (i=0; i<4; i++) {
   // loop until the switch has changed from LOW to HIGH
   while (!(switchState ==  HIGH && prevSwitchState == LOW)) {
     // the switch did not change from LOW to HIGH last time
     // Remember the previous switch state from the last iteration
     prevSwitchState = switchState;
     // Read the present state
     switchState = digitalRead(switchPin);
     // delay as a simple debounce
     delay(100);
   }
   // the while loop exited wthout updating
   // the previous switch state so do it now
   prevSwitchState = switchState;
   if (switchState == HIGH) {
     // randomly choose a reply index
     reply = random(length);
     // clean up the screen before printing a new reply
     lcd.clear();
     // set the cursor to column 0, line 0
     lcd.setCursor(0, 0);
     // print some text
     // A different part of the excuse for each for-loop iteration
     switch(i) {
       case 0:lcd.print(start[reply]); break;
       case 1:lcd.print(middle[reply]); break;
       case 2:lcd.print(ends[reply]); break;
     }
   }
 }

}

Step 3: Enjoy

Every time you tap the tilt Sensor a part of an Excuse will be generated.

Don't blame me Chuck Norris stole my head

Running out of excuses?

Change these lines to add more:

// Define your excuses char *start []= {"I'm sorry but ","Don't blame me ", "Not my fault ","Guess what happened "}; char *middle []= {"Godzilla ","Chuck Norris ", "Scrooge McDuck ","Soap McTavish"}; char *ends []= {"tried to kill me. ","ate my homework.","came after me.","stole my head."};

int length = 4;