Hello, I'm trying to trigger a laser from another program using the arduino.
Essentially, the arduino receives a pulse from this other program (from a TTL converter) and the arduino then executes some code, which triggers a laser every 10 minutes for 1 second at 10hz. I'm trying to write non-blocking code so I'm not using the delay function. I'm, however, having trouble triggering the laser. Here's the code:
//COM4
int switchState_Cue = 0;
int cueState_Retrieval = 0;
int reset_Millis_Stop = 0;
int reset_start_Laser = 0;
int reset_startoffperiod = 0;
int reset_realLaser = 0;
long sessiontime;
long sessiontimestart;
long sessiontimestop;
long totalsessiontime;
long currentMillis;
long stopLaser;
long laserlimit;
long startoffperiod;
long endoffperiod;
long startLaser;
long endLaser;
long realLaser;
unsigned long Millis_start = 0;
unsigned long Millis_stop = 0;
void setup() {
// put your setup code here, to run once:
pinMode(3, OUTPUT); //Laser output
pinMode(2,OUTPUT); //LED output
pinMode(12,INPUT);//Cue input
pinMode(11,INPUT);//Second cue input from another program (TTL converter).
laserlimit = 1000; //laser will stop after 1 second
sessiontime = 4200000; // 1hour 10min
Serial.begin(9600);
digitalWrite(2, HIGH);
delay(2000);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
delay(0);
}
void loop() {
switchState_Cue = digitalRead(12); // First time cue goes off, that's when session begins.
reset_Millis_stop = reset_Millis_stop +1; //resetting millis.
if (switchState_Cue == HIGH) {
sessiontimestart = millis(); //
if (reset_Millis_stop> 0){
Millis_stop =millis();
x = x-1;
}
digitalWrite(2, HIGH); // this is just pulsing the LED, to make sure that the system is working still.
while (currentMillis - sessiontimestart < sessiontime){
if ((Millis_start - Millis_stop) >= 10000){ // Trying to pulse the laser every ten seconds.
reset_realLaser = reset_realLaser+1;
Millis_stop = Millis_start;
reset_start_Laser = reset_start_Laser+1;
reset_startoffperiod = reset_startoffperiod+1;
digitalWrite(2,LOW); //checking the code with LED.
if (reset_realLaser>0){
realLaser = millis();
reset_realLaser = reset_realLaser - 1;
}
while ((byeLaser - realLaser) <= laserlimit){ // Every ten seconds, the laser should go off for 1 second (from laserlimit function).
if (reset_start_Laser>0){
startLaser = millis();
reset_start_Laser = reset_start_Laser-1;
}
while ((endLaser - startLaser) <= 10){ // Trying to write the laser on HIGH for 10 milliseconds.
digitalWrite(2,HIGH);
digitalWrite(3, HIGH); // 10hz laser ON
endLaser = millis();
}
if (reset_startoffperiod>0){
startoffperiod = millis();
reset_startoffperiod = reset_startoffperiod-1;
}
while(endoffperiod-startoffperiod <= 90){ // Trying to write the laser on LOW for 90 milliseconds. Combined with the above, for 1 seconds, the laser should be turned on.
digitalWrite(2,LOW);
digitalWrite(3,LOW);
endoffperiod = millis();
}
byeLaser = millis();
}
}
Millis_start = millis();
}
}
if (currentMillis - sessiontimestart >= sessiontime){ // Checking to see if session time is up, in which case, session ends.
Stopsession();
}
cueState_Retrieval = digitalRead(11); // This is relevant for another part of the project, where I'm getting an input from a TTL converter. I need to know when that happens.
if (cueState_Retrieval == HIGH){
retrieved();
}
currentMillis = millis();
}
void retrieved() { // Again, just trying to write down when I'm getting a pulse from another program.
Serial.print("Retrieved at time...");
Serial.println((currentMillis+10000)-sessiontimestart);
}
void Stopsession() {
digitalWrite(2, LOW);
sessiontimestop = millis();
totalsessiontime = sessiontimestop - sessiontimestart;
Serial.print("Total Session Time...");
Serial.println(totalsessiontime);
Serial.end();
}