/* Analog input, analog output, serial output Reads an analog input pin, maps the result to a range from 0 to 255 and uses the result to set the pulsewidth modulation (PWM) of an output pin. Also prints the results to the serial monitor. The circuit: * potentiometer connected to analog pin 0. Center pin of the potentiometer goes to the analog pin. side pins of the potentiometer go to +5V and ground * LED connected from digital pin 9 to ground created 29 Dec. 2008 Modified 4 Sep 2010 by Tom Igoe This example code is in the public domain. */ #include Servo rodOutput; // Give names to pins const int rodInput = A0; // First potentiometer, used as input const int resetButton = 2; const int green[2] = { 6, 11 }; const int red[2] = { 5, 3 }; const int energyLED = 11; const int fuelLED = 5; int fuelLvl = 100; int energyLvl = 0; int sensorValue = 0; // value read from the pot //int outputValue = 0; // value output to the PWM (analog out) int rodPosition = 0; typedef enum {e_play=0,e_boom1,e_boom2,e_won1,e_won2} e_state; e_state state=e_play; int last=0; void ledOff() { analogWrite(red[0], 255); analogWrite(red[1], 255); analogWrite(green[0], 255); analogWrite(green[1], 255); } void reset() { Serial.print("RESET" ); state=e_play; fuelLvl = 255; energyLvl = 0; last=millis(); ledOff(); } void wonGame() { ledOff(); state=e_won1; } void lostGame() { ledOff(); state=e_boom1; } void setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); rodOutput.attach(10); pinMode(resetButton, INPUT); digitalWrite(resetButton, HIGH); reset(); } void loop() { int now; unsigned long power; // ROD POSITION AND BOOMING if (state!=e_play) { // It exploded: display it and check for reset if (LOW == digitalRead(resetButton)) { reset(); } else { delay(100); // It exploded, dude. Blink the LEDs switch (state) { case e_boom1: state=e_boom2; analogWrite(red[0], 255); analogWrite(red[1], 0); break; case e_boom2: state=e_boom1; analogWrite(red[0], 0); analogWrite(red[1], 255); break; case e_won1: state=e_won2; analogWrite(green[0], 255); analogWrite(green[1], 0); break; case e_won2: state=e_won1; analogWrite(green[0], 0); analogWrite(green[1], 255); break; } } } else { // COMMAND // read the analog in value: sensorValue = analogRead(rodInput); // Change rod position rodPosition = map(sensorValue,0,1023,0,180); rodOutput.write(rodPosition); // GAME LOGIC now = millis(); if (now-last>50) { power = (rodPosition/10)*(fuelLvl/10)*(now-last); // check whether it exploded yet, or update the energy level if still good if (power > 10000) lostGame(); else { energyLvl = map(power,0, 12000 ,0,255); if (energyLvl) { fuelLvl-=energyLvl/20; if (fuelLvl<20) wonGame(); } } // OUTPUT // print the results to the serial monitor: Serial.print("rodPosition = " ); Serial.print(rodPosition); Serial.print(" fuelLvl = " ); Serial.print(fuelLvl); Serial.print(" power = " ); Serial.print(power); Serial.print(" energyLvl = " ); Serial.print(energyLvl); Serial.print(" time = "); Serial.print(now-last); Serial.print(" state = "); Serial.println(state); // Show levels analogWrite(energyLED, 255-energyLvl); analogWrite(fuelLED,255-fuelLvl); last = now; } } }