/* Sketch for stage 1 GoNoGo, RIGHT active nosepoke hole. Session ends after 20 min and delays for 20 min, turnign off active nose poke LED */ #include int gatePin = 12; //controls feeder transistor int houseLight = 8; int buzz = 10; //nose-poke buzzer int scope = 9; int GrLED1 = 6; //green LEDs above ports int GrLED2 = 7; int pokes1 = 0; //nosepoke counters int pokes2 = 0; int rewards = 0; // reward counter int GOtrials = 0; //GO trial counter #define SENSORPIN_A 4 #define SENSORPIN_B 5 // variables will change: int sensorState_A = 0, lastState_A = 0; int sensorState_B = 0, lastState_B = 0; bool scopeTrigger = true; //used to trigger scope recording //OFF timer variables unsigned long OFFtimerStart; unsigned long ONtimerStart; unsigned long BUZZtimerStart; unsigned long currentMillis; const unsigned long OFFperiod = 10000; const unsigned long ONperiod = 9000; const unsigned long BUZZperiod = 250; void setup() { // initialize the gate pin as an output: pinMode(gatePin, OUTPUT); // initialize the sensor pins as an input and enable internal pullup: pinMode(SENSORPIN_A, INPUT_PULLUP); pinMode(SENSORPIN_B, INPUT_PULLUP); //green LEDs above nosepoke holes: pinMode(GrLED1, OUTPUT); pinMode(GrLED2, OUTPUT); pinMode(houseLight, OUTPUT); pinMode(buzz, OUTPUT); pinMode(scope, OUTPUT); Serial.begin(9600); } void loop() { // timer for session of X milliseconds, stops arduino for Y milliseconds if (millis() >= 1200000) //X { Serial.println ("--------------------------------------------"); Serial.println ("session end"); Serial.println ("--------------------------------------------"); Serial.print("GO trials"); Serial.print(","); Serial.println(GOtrials); Serial.print ("rewards"); Serial.print (","); Serial.println (rewards); Serial.print ("active"); Serial.print (","); Serial.println (pokes2); Serial.print ("inactive"); //port ID Serial.print (","); Serial.println (pokes1); digitalWrite (GrLED2, LOW); digitalWrite (houseLight, LOW); digitalWrite (scope, LOW); delay (1200000); //Y } if (scopeTrigger == true) { digitalWrite(scope, HIGH); Serial.println("scope on"); scopeTrigger = false; } digitalWrite(houseLight, HIGH);//turn on house light digitalWrite(GrLED2, HIGH); //turn on LED1 over left nosepoke digitalWrite(GrLED1, LOW); GOtrials = GOtrials + 1; Serial.print("GO trials"); Serial.print(","); Serial.println(GOtrials); ONtimerStart = millis(); while(digitalRead(GrLED2) == HIGH) { currentMillis = millis(); //record time at each iteration of loop in currentMillis var if (currentMillis - BUZZtimerStart >= BUZZperiod) digitalWrite(buzz, LOW);//turn off buzzer if (currentMillis - ONtimerStart >= ONperiod) digitalWrite(GrLED2, LOW);//if the OFF period has passed break out of "while OFF" loop // read the state of the sensors: sensorState_A = digitalRead(SENSORPIN_A); sensorState_B = digitalRead(SENSORPIN_B); if (sensorState_B && !lastState_B) { digitalWrite(gatePin, LOW); } if (!sensorState_B && lastState_B) { pokes2 = pokes2 + 1; rewards = rewards + 1; Serial.print ("active"); Serial.print (","); Serial.println (pokes2); Serial.print ("rewards"); Serial.print (","); Serial.println (rewards); BUZZtimerStart = millis(); digitalWrite(buzz, HIGH); digitalWrite(gatePin, HIGH); delay(75); digitalWrite(gatePin, LOW); digitalWrite(GrLED2, LOW); //switch LED1 off after nosepoke } lastState_B = sensorState_B; if (sensorState_A && !lastState_A) { digitalWrite(gatePin, LOW); } if (!sensorState_A && lastState_A) { pokes1 = pokes1 + 1; Serial.print ("inactive"); //port ID Serial.print (","); Serial.println (pokes1); //poke counter } lastState_A = sensorState_A; delay(10); // so we don't flood the serial port } lastState_A = sensorState_A; //reset sensor states lastState_B = sensorState_B; //removed 500 ms delay here OFFtimerStart = millis(); //record time when "while OFF" loop started in OFFtimerStart var while(digitalRead(GrLED2) == LOW) { currentMillis = millis(); //record time at each iteration of loop in currentMillis var if (currentMillis - BUZZtimerStart >= BUZZperiod) digitalWrite(buzz, LOW);//turn off buzzer if (currentMillis - OFFtimerStart >= OFFperiod) break; //if the OFF period has passed break out of "while OFF" loop // read the state of the sensors: sensorState_A = digitalRead(SENSORPIN_A); sensorState_B = digitalRead(SENSORPIN_B); if (sensorState_B && !lastState_B) { digitalWrite(gatePin, LOW); } if (!sensorState_B && lastState_B) { pokes2 = pokes2 + 1; //only pokes counted, no rewards since no pellet dispensed Serial.print ("active"); Serial.print (","); Serial.println (pokes2); // no gatePIN trigger since non-rewarded poke } lastState_B = sensorState_B; if (sensorState_A && !lastState_A) { digitalWrite(gatePin, LOW); } if (!sensorState_A && lastState_A) { pokes1 = pokes1 + 1; Serial.print ("inactive"); //port ID Serial.print (","); Serial.println (pokes1); //poke counter } lastState_A = sensorState_A; delay(10); // so we don't flood the serial port } }