Built-in project · Arduino Uno R3

Answer ball on an Arduino Uno

Ask a question and tip the tilt switch: the 16x2 LCD shows one of eight answers, picked with random(). The whole build, an Arduino Uno and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner Runs in your browser. Free, and no account needed.

Answer ball · Arduino Uno R3live0.000 s 0.00x
Hold the button
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Answer ball: ask a yes-or-no question, tip the tilt switch, and the LCD
// shows an answer picked at random.
//
//   LCD     RS -> pin 12, E -> pin 11, D4 to D7 -> pins 5, 4, 3, 2
//           VSS, RW, K -> GND; VDD, A -> 5V; V0 -> GND for full contrast
//   pin 6   the tilt switch, its other leg to GND (internal pull-up)
//
// random(8) picks a number from 0 to 7, and that number picks a line from
// the table. The numbers come from a generator that would start the same way
// every time, so setup() seeds it from analogRead of a pin with nothing on it,
// which wanders a little.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int TILT = 6;

const char *ANSWERS[8] = {
    "Yes", "Most likely", "Certainly", "Outlook good",
    "Unsure", "Ask again", "Doubtful", "No",
};

int lastTilt = LOW;

void setup() {
  pinMode(TILT, INPUT_PULLUP);
  lcd.begin(16, 2);
  lcd.print("Ask me a");
  lcd.setCursor(0, 1);
  lcd.print("question");
  randomSeed(analogRead(A5));
  Serial.begin(9600);
  Serial.println("Mokxi Uno: answer ball. Tip it to ask.");
  lastTilt = digitalRead(TILT);
}

void loop() {
  int tilt = digitalRead(TILT);
  // Tipped over: the switch opens and the pull-up reads HIGH.
  if (tilt != lastTilt && tilt == HIGH) {
    const char *answer = ANSWERS[random(8)];
    lcd.clear();
    lcd.print("The ball says:");
    lcd.setCursor(0, 1);
    lcd.print(answer);
    Serial.print("answer: ");
    Serial.println(answer);
  }
  lastTilt = tilt;
  delay(20);
}

Parts list

4 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

9 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Arduino Uno R3 pin 12; Character LCD, 16x2 pin RS
  • Arduino Uno R3 pin 11; Character LCD, 16x2 pin E
  • Arduino Uno R3 pin 6; Tilt switch pin 2
  • Arduino Uno R3 pin 5; Character LCD, 16x2 pin D4
  • Arduino Uno R3 pin 4; Character LCD, 16x2 pin D5
  • Arduino Uno R3 pin 3; Character LCD, 16x2 pin D6
  • Arduino Uno R3 pin 2; Character LCD, 16x2 pin D7
  • Arduino Uno R3 pin 5V; Character LCD, 16x2 pin VDD; Character LCD, 16x2 pin A
  • Ground: Arduino Uno R3 pin GND; Tilt switch pin 1; Character LCD, 16x2 pin VSS; Character LCD, 16x2 pin V0; Character LCD, 16x2 pin RW; Character LCD, 16x2 pin K

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.