Hi guys!
I'm trying to build a Skinner Box (an apparatus used to study behavior in animals) controlled by Arduino.
So, the system is as follows:
The animal press a lever for 2 seconds to light an LED randomly (left or right) for 1 second to indicate where the animal should go to get a reward. For this last point the animal should break an IR beam, to activate a solenoid valve in the side where the LED was lit.
Well, I´m new in Arduino, and the part of the IR beam doesn´t work :~
Can you help me please???
Thank you!
/*
One trial is iniciated when a switch (lever) is pressed for 2 seconds, then one of two LEDs is randomly turned on (cue) for 1 second
indicating the side where a rat must interrupt an IR beam in order to receive a drop of water (delivered with a solenoid valve). If the rat
interrupts the incorrect IR beam nothing happens until she press the switch again.
*/
//Declare variables:
unsigned long keyPrevMillis = 0;
const unsigned long keySampleIntervalMs = 25;
byte longKeyPressCountMax = 80; // 80 * 25 = 2000 ms
byte longKeyPressCount = 0;
byte prevKeyState = HIGH; // switch is active low
const byte keyPin = 8; // switch is connected to pin 8 and GND
int LED1 = 12;
int LED2 = 13;
int IR_1 = 4;
int IR_2 = 5;
int valvePin1 = 6;
int valvePin2 = 7;
int emitter1 = 1;
int emitter2 = 2;
const int cant_LED = 2; // number of LEDs I have
const int cant_emitters = 2; // number of IR LEDs I have
int LEDs [cant_LED] = {LED1, LED2};
int emitters [cant_emitters] = {emitter1, emitter2};
//Is called when switch is pressed:
void LED(){
digitalWrite (LEDs[random(cant_LED)], HIGH); //An LED is turned on randomly (LED1 or LED2)
delay(1000); //for 1 sec
for (int i=0; i < 2; i++) {
digitalWrite (LEDs[i], LOW); //then is turned off
}
}
//Is called when switch is pressed:
void IR(){
byte beam1 = digitalRead (IR_1);
byte beam2 = digitalRead (IR_2);
if ((LED1 == HIGH) && ( beam1 == LOW)){ //when LED1 was on and beam1 was interrupted
digitalWrite (valvePin1, HIGH); //turn the valve1 on
delay(1000); // for 1 sec
digitalWrite (valvePin1, LOW); // then turn it off
Serial.print(",");
Serial.println('delivered left');
}
else if ((LED2 == HIGH) && ( beam2 == LOW)){ //when LED2 was on and beam2 was interrupted
digitalWrite (valvePin2, HIGH); //turn the valve2 on
delay(1000); //for 1 sec
digitalWrite (valvePin2, LOW); //then turn it off
Serial.print(",");
Serial.println('delivered right');
}
}
// Is called when switch is kept pressed for less than 2 seconds
void shortKeyPress() {
Serial.println("short ----");
}
// called when switch is kept pressed for more than 2 seconds
void longKeyPress() {
Serial.println("long ----");
LED();
IR();
}
// Is called when key goes from not pressed to pressed
void keyPress() {
Serial.println("key press");
longKeyPressCount = 0;
}
// Is called when key goes from pressed to not pressed
void keyRelease() {
Serial.println("key release");
if (longKeyPressCount >= longKeyPressCountMax) { //if the switch is pressed for at least 2 seconds call "longKeyPress"
longKeyPress();
}
else { //otherwise "shortKeyPress" is called.
shortKeyPress();
}
}
void setup() {
Serial.begin(115200);
pinMode(keyPin, INPUT_PULLUP); // switch is an input
for (int i=0; i < cant_LED; i++) { // LEDs are outputs
pinMode(LEDs[i], OUTPUT);
}
for (int i=0; i < cant_emitters; i++) {
pinMode(emitters[i], OUTPUT); //
digitalWrite (emitters[i], HIGH); // both IR emitters are always on
pinMode (IR_1, INPUT); //
pinMode (IR_2, INPUT); // IR receptors are inputs
pinMode (valvePin1, OUTPUT);
pinMode (valvePin2, OUTPUT); // Valve pins are outputs
}
}
void loop() {
// key management section
if (millis() - keyPrevMillis >= keySampleIntervalMs) {
keyPrevMillis = millis();
byte currKeyState = digitalRead(keyPin); //currKeyState reads the state of the switch
if ((prevKeyState == HIGH) && (currKeyState == LOW)) { // if the switch is pressed
keyPress(); //call "keyPress"
}
else if ((prevKeyState == LOW) && (currKeyState == HIGH)) { //if the switch is released
keyRelease(); //call "keyRelease"
}
else if (currKeyState == LOW) {
longKeyPressCount++;
}
prevKeyState = currKeyState;
}}