Stap 3: De code uploaden naar je Arduino
De code:
<p>/*<br> * ===================================================================================== * * Filename: TargetPractice_photoresistor.ino * * Description: Uses a photoresistor to detect how long it takes a player * to hit a target with a laser pointer after a countdown. * http://www.ardusat.com * * This example uses many third-party libraries available from * Adafruit (https://github.com/adafruit). These libraries are * mostly under an Apache License, Version 2.0. * * http://www.apache.org/licenses/LICENSE-2.0 * * Version: 1.0 * Created: 2/24/2016 * Revision: none * Compiler: Arduino * * Author: Chris Hoffman (chris * Organization: Ardusat * * ===================================================================================== *</p><p>*----------------------------------------------------------------------------- * Includes *-----------------------------------------------------------------------------*/ #include <arduino.h> #include <wire.h></wire.h></arduino.h></p><p>/*----------------------------------------------------------------------------- * Constant Definitions *-----------------------------------------------------------------------------*/</p><p>long top = 38; boolean win = false; boolean gameOn = true; boolean Started = false; boolean timer = false; long startTime; long endTime; long randomTime; float elapsedTime; int ledPin2 = 2; int ledPin3 = 3; int ledPin4 = 4; int ledPin5 = 5; const int sensorPin = 0; int lightLevel, high = 0, low = 1023;</p><p>/* * === FUNCTION ====================================================================== * Name: setup * Description: This function runs when the Arduino first turns on/resets. This is * our chance to take care of all one-time configuration tasks to get * the program ready to begin the game. * ===================================================================================== */ void setup(void){ Serial.begin(9600); pinMode(ledPin2,OUTPUT); //red LED pinMode(ledPin3,OUTPUT); // yellow LED pinMode(ledPin4,OUTPUT); // green LED pinMode(ledPin5,OUTPUT); // "go" sign Serial.println("All systems go. Watch for the countdown."); }</p><p>/* * === FUNCTION ====================================================================== * Name: loop * Description: After setup runs, this loop function runs until the Arduino loses * power or resets. We go through and update each of the attached * sensors, write out the updated values in JSON format, then delay * before repeating the loop again. * ===================================================================================== */ void loop(void){ Serial.print("lightLevel: "); Serial.println(lightLevel);</p><p> lightLevel = analogRead(sensorPin); lightLevel = map(lightLevel, 0, 1023, 0, 255); lightLevel = constrain(lightLevel, 0, 255); manualTune(); // manually change the range from light to dark if (gameOn == true){ digitalWrite(ledPin2,HIGH); delay(2000); digitalWrite(ledPin2,LOW); digitalWrite(ledPin3,HIGH); delay(2000); digitalWrite(ledPin3,LOW); digitalWrite(ledPin4,HIGH); delay(2000); digitalWrite(ledPin4,LOW); Random(); digitalWrite(ledPin5,HIGH); Serial.println("Fire!"); gameOn = false; } if (lightLevel >= top){ win = true; } if (win == true){ Stop(); int i=0; while (i <= 9){ digitalWrite(ledPin2,HIGH); digitalWrite(ledPin3,HIGH); digitalWrite(ledPin4,HIGH); digitalWrite(ledPin5,HIGH); delay (200); digitalWrite(ledPin2,LOW); digitalWrite(ledPin3,LOW); digitalWrite(ledPin4,LOW); digitalWrite(ledPin5,LOW); delay (200); i++; } delay(3000); win = false; gameOn = true; }</p><p> delay(10); }</p><p>/* * === FUNCTION ====================================================================== * Name: Random * Description: Delays the time inbetween the initial countdown and detecting the * laser pointer. * ===================================================================================== */</p><p>void Random() { randomTime = random(4,10); randomTime = randomTime*1000; delay(randomTime); Start(); }</p><p>/* * === FUNCTION ====================================================================== * Name: Start * Description: Starts a timer so we can report to the player how quickly they * hit the light sensor with a laser pointer. Calculated in millis. * ===================================================================================== */</p><p>void Start(){ startTime = millis(); }</p><p>/* * === FUNCTION ====================================================================== * Name: Stop * Description: Stops the timer and reports the player's elapsed time between the * go light and hitting the sensor. * ===================================================================================== */ void Stop(){ endTime = millis(); elapsedTime = (endTime - startTime)+5; elapsedTime = elapsedTime/1000; Serial.println("Direct Hit!"); Serial.print("Time, Seconds: "); Serial.println(elapsedTime); }</p><p>void manualTune() { lightLevel = map(lightLevel, 0, 1023, 0, 255); lightLevel = constrain(lightLevel, 0, 255); }</p>
Volgende