/* Stage 0 of the GoNoGo task, right nose-poke hole is active. The script will run for 20 min and then enter a timout period of 20 min. It needs a reset after every session. The timing of the session and timout can be changed by modifying the appropriate variables. */ #include int gatePin = 12; //controls feeder transistor int buzz = 10; //nose-poke buzzer int houseLight = 8; 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; //OFF timer variables unsigned long OFFtimerStart; unsigned long BUZZtimerStart; unsigned long currentMillis; const unsigned long OFFperiod = 10000; const unsigned long BUZZperiod = 250; void setup() { // initialize the LED 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); pinMode(GrLED1, OUTPUT); pinMode(GrLED2, OUTPUT); pinMode(houseLight, OUTPUT); pinMode(buzz, 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); delay (1200000); //Y } digitalWrite(houseLight, HIGH);//turn on house light digitalWrite(GrLED2, HIGH); digitalWrite(GrLED1, LOW); GOtrials = GOtrials + 1; Serial.print("GO trials"); Serial.print(","); Serial.println(GOtrials); while(digitalRead(GrLED2) == HIGH) { if (millis() >= 1200000) break; currentMillis = millis(); //record time at each iteration of loop in currentMillis var // read the state of the sensor: 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"); //port ID Serial.print (","); Serial.println (pokes2); //poke counter 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; OFFtimerStart = millis(); //record time when "while OFF" loop started in OFFtimerStart var while(digitalRead(GrLED2) == LOW) { if (millis() >= 1200000) break; 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 LED 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 } }