Skip to content

Instantly share code, notes, and snippets.

@rsbohn
Created February 14, 2012 06:07
Show Gist options
  • Save rsbohn/1824162 to your computer and use it in GitHub Desktop.
Save rsbohn/1824162 to your computer and use it in GitHub Desktop.
Simple game on arduino
// wild_servo
// a game
// Your potentiometer controls the servo position
// But there is a region of instability in the center
// Keep the servo between 45 and 135 degrees
// Or the light flashes and you lose a point
// How long can you keep it going?
// Based on the servo>knob example code:
// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
// Feel free to modify this code, make it more challenging:
// -Make the unstable region more random
// -Make the unstable region wider
// -Make the safe zone narrower (60 to 120 degrees or even tighter)
#include <Servo.h>
#define LED 13
Servo myservo;
int potpin = 5; // analog pin used to connect the potentiometer
int val;
int spot = 90;
int inPlay = true;
int timeout;
void setup()
{
// attach the servo on pin 9 to the servo object
myservo.attach(9);
pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
}
void restart() {
digitalWrite(LED, HIGH);
spot = 90;
inPlay = true;
}
void loop()
{
if (inPlay) {
digitalWrite(LED, HIGH);
val = analogRead(potpin);
val = map(val, 0, 1023, 5, -5);
spot = spot + (val ? val : random(-6,6));
myservo.write(spot);
if (spot < 45 || spot > 135) {
inPlay = false;
timeout = 20;
}
delay(60);
}
else {
digitalWrite(LED, !digitalRead(LED));
delay(180);
if (timeout-- < 1) restart();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment