Devices & Components
Arduino Uno Rev3
9V to Barrel Jack Connector
5 mm LED: Green
Photo resistor
9V battery (generic)
Breadboard (generic)
Jumper wires (generic)
SG90 Micro-servo motor
Resistor 220 ohm
Software & Tools
Arduino IDE
Project description
Code
Code
c_cpp
1// C++ code 2#include <Servo.h> 3byte phres[3]={A0,A1,A2}; 4 5int sensorValue = 0; 6int index=0; 7int score = 0; 8 9Servo servo_A3; 10Servo servo_A4; 11Servo servo_A5; 12Servo S[3]={servo_A3,servo_A4,servo_A5}; 13 14void setup() 15{ 16 pinMode(A0, INPUT); 17 pinMode(A1, INPUT); 18 pinMode(A2, INPUT); 19 servo_A3.attach(A3); 20 servo_A4.attach(A4); 21 servo_A5.attach(A5); 22 pinMode(13, OUTPUT); 23 S[0].write(0); 24 S[1].write(0); 25 S[2].write(0); 26 Serial.begin(9600); 27 randomSeed(analogRead(A3)); 28 index = random(0,3); 29 rand_up(index); 30} 31 32void rand_up(int index){ 33 S[index].write(90); 34} 35 36void rand_down(int index){ 37 S[index].write(0); 38} 39 40void loop() 41{ 42 sensorValue = analogRead(phres[index]); 43 if(sensorValue > 150){ 44 digitalWrite(13, HIGH); 45 rand_down(index); 46 delay(1000); 47 score = score + 1; 48 Serial.print(score); 49 digitalWrite(13, LOW); 50 index = random(0,3); 51 rand_up(index); 52 } 53 //Serial.print(sensorValue); <-- for checking Photoresistor input 54 delay(1000); // Wait for 1000 millisecond(s) 55} 56
Code
c_cpp
1// C++ code 2#include <Servo.h> 3byte phres[3]={A0,A1,A2}; 4 5int 6 sensorValue = 0; 7int index=0; 8int score = 0; 9 10Servo servo_A3; 11Servo 12 servo_A4; 13Servo servo_A5; 14Servo S[3]={servo_A3,servo_A4,servo_A5}; 15 16void 17 setup() 18{ 19 pinMode(A0, INPUT); 20 pinMode(A1, INPUT); 21 pinMode(A2, 22 INPUT); 23 servo_A3.attach(A3); 24 servo_A4.attach(A4); 25 servo_A5.attach(A5); 26 27 pinMode(13, OUTPUT); 28 S[0].write(0); 29 S[1].write(0); 30 S[2].write(0); 31 32 Serial.begin(9600); 33 randomSeed(analogRead(A3)); 34 index = random(0,3); 35 36 rand_up(index); 37} 38 39void rand_up(int index){ 40 S[index].write(90); 41} 42 43 44void rand_down(int index){ 45 S[index].write(0); 46} 47 48void loop() 49{ 50 51 sensorValue = analogRead(phres[index]); 52 if(sensorValue > 150){ 53 digitalWrite(13, 54 HIGH); 55 rand_down(index); 56 delay(1000); 57 score = score + 1; 58 59 Serial.print(score); 60 digitalWrite(13, LOW); 61 index = random(0,3); 62 63 rand_up(index); 64 } 65 //Serial.print(sensorValue); <-- for checking Photoresistor 66 input 67 delay(1000); // Wait for 1000 millisecond(s) 68} 69
Downloadable files
Circuit Diagram
Circuit Diagram

Comments
Only logged in users can leave comments