-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathARD30Sec.cpp
More file actions
68 lines (56 loc) · 2.42 KB
/
ARD30Sec.cpp
File metadata and controls
68 lines (56 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <Stepper.h>
const int stepsPerRevolution = 516; // change this to fit the number of steps per revolution for your stepper motor
const int ledPin = 13; // the pin that the trial indicator LED is attached to
const int irPin = 2; // the pin that the IR Phototransistor is attached to
unsigned long previousTime = 0;
unsigned long trialStartTime = 0;
const unsigned long trialInterval = 30000; // 30 seconds interval between trials
const unsigned long trialDelay = 10000; // 10 seconds delay between trials
bool startTrial = false; // Flag to start a trial
// initialize the stepper library on pins 9 through 12:
Stepper myStepper(stepsPerRevolution, 9, 10, 11, 12);
void setup() {
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output
pinMode(irPin, INPUT); // initialize the IR Phototransistor pin as an input
Serial.begin(9600); // initialize serial communication at 9600 bits per second
trialStartTime = millis() + trialInterval; // Initialize the first trial start time
}
void loop() {
unsigned long currentTime = millis();
// read the state of the IR Phototransistor
int irState = digitalRead(irPin);
// Check if it's time to start a new trial
if (currentTime >= trialStartTime) {
startTrial = true;
trialStartTime = currentTime + trialInterval; // Set the next trial start time
}
// check if there is any incoming serial data
if (Serial.available() > 0) {
// read the incoming byte
byte incomingByte = Serial.read();
// if the incoming byte is 't', start a trial immediately
if (incomingByte == 't') {
startTrial = true;
}
}
// Start a trial if the flag is set
if (startTrial) {
Serial.print("Trial started at: ");
Serial.println(currentTime);
digitalWrite(ledPin, HIGH); // turn on the LED
myStepper.setSpeed(25); // set the speed of the stepper motor
myStepper.step(30); // move the stepper motor 30 degrees
previousTime = currentTime;
delay(500); // wait for half a second
// activate the camera for 30 seconds
Serial.write("c"); // send 'c' to trigger the camera
delay(50); // wait for the camera to initialize
Serial.write("r"); // send 'r' to start recording
// Wait for 1 second before turning off the LED
delay(1000);
digitalWrite(ledPin, LOW); // turn off the LED
// Reset the flag
startTrial = false;
}
// You can add other code here to handle the IR Phototransistor if needed.
}