Hello all I am hoping to create a laser maze. I am trying to get the lasers to hit a photoresistor and the arduino read the photoresistors. When the photoresistors are broken by someone blocking the path I want it to push to keep count. For the first time I want it to push to a relay, the second time push to a different relay and finally the thrid time push to a final relay. The inital code was written to push to a servo; however, I do not wish to do that. Hopefully this is clear enough to understand what I am wanting to do.
Thanks In Advance.
#include <Servo.h>
// servo will be used to flip a switch on the red beacon light
Servo myservo;
// constants to set up pins
int mySensors[] = {A0, A1, A2, A3, A4, A5}; // analog pins with photoresistors cells to detect laser beam
const int numSensors = 3; // total number of photoresistors cells used ###
// variables to handle analog values from CDS cells
int curValue; // stores the value of the photoresistor cell that is currently being read
int prevValue[numSensors]; // stores the previous value of each photoresistor cell
int baseValue[numSensors]; // stores a baseline value for each photoresistor cell when the laser beam is not broken
int shouldAlarm[numSensors];// flag to decide if alarm signal should be sent
int sensitivity = 35; // an offset from the baseValue to determine if laser beam is broken ###
int sendAlarm = 0; // determines if we should send the alarm signal to the COM port
// variables to control red beacon light
int alarmLength = 3000; // how long red light should remain on in milliseconds - synch with mp3 played by processing
unsigned long alarmTime;// time alarm was activated
int onPos = 50; // on position for servo
int offPos = 110; // off position for servo
void setup() {
// initialize the serial communication to PC
Serial.begin(9600);
// attach the servo to pin 9
myservo.attach(9);
myservo.write(offPos);
// set all of the photoresistor cell analog pins as inputs
for (int thisSensor = 0; thisSensor < numSensors; thisSensor++) {
pinMode(mySensors[thisSensor], INPUT);
}
calibrate();
}
void loop()
// check each photoresistor cell to see if the laser beam is broken
for (int thisSensor = 0; thisSensor < numSensors; thisSensor++) {
curValue = analogRead(mySensors[thisSensor]);
// A broken beam is detected if the current sensor value and
// the previous value are both less than the base value
// minus the sensitivity.
if (curValue < (baseValue[thisSensor] - sensitivity) && prevValue[thisSensor] < (baseValue[thisSensor] - sensitivity) && shouldAlarm[thisSensor] == 1) {
// laser beam has been broken, alarm should be sent
Serial.println("alarm");
shouldAlarm[thisSensor] = 0;
// activate servo to turn on red beacon light
myservo.write(onPos);
delay(20);
alarmTime = millis();
}
// laser beam not currently broken, so reset the alarm flag
else if (curValue > (baseValue[thisSensor] - sensitivity) && prevValue[thisSensor] > (baseValue[thisSensor] - sensitivity)) {
shouldAlarm[thisSensor] = 1;
}
prevValue[thisSensor] = curValue; // update the previous value
}
// deactivate the servo if it has been more than alarmLength
if (millis() - alarmTime > alarmLength) {
myservo.write(offPos);
delay(20);
}
}
void calibrate() {
// for each photoresistors cell, average three readings together to
// calculate base value when laser beam is not broken
for (int thisSensor = 0; thisSensor < numSensors; thisSensor++) {
baseValue[thisSensor] = analogRead(mySensors[thisSensor]); // first reading
delay(20);
baseValue[thisSensor] += analogRead(mySensors[thisSensor]); // plus second reading
delay(20);
baseValue[thisSensor] += analogRead(mySensors[thisSensor]); // plus third reading
baseValue[thisSensor] /= 3; // divided by 3 to find average
// Serial.println(thisSensor);
// Serial.println(baseValue[thisSensor]);
}
}