Hi community,
I am trying to create code that drives a laser to turn on for 250ms at 20HZ, then 250ms off entirely, repeated for as long as the state on one of the pins on my board remains high.
I've gotten the code to output (5 pulses, then turn off for 250ms), and it repeats these two things indefinitely. But I want it to just repeat this series for as long as my signal remains high, and then turn off entirely when the signal is low.
I only just started coding this morning in C++ and have never coded before so I'm sure there's some obvious logic I'm missing. Greatly appreciate any help!
// Define stimulation protocol here:
// Define one pulse cycle:
int PulseOn = 10; /*defines pulse width, time in ms*/
int PulseOff = 40; /*defines time between pulses, time in ms*/
// Define time intervals for cycles:
int StimOnInterval = 250; /*defines interval of time in which pulse cycles are repeating and LED is ON, time in ms */
int StimOffInterval = 250; /*defines interval of time in which pulse cycles are not repeating and LED is OFF, time in ms */
int StimOnTimes = 5; /*defines number of pulses per interval of LED on */
// Define pins on Arduino board:
int LEDPin = 11; /* PIN number for LED connected to BNC*/
int TTLPin = 3; /* PIN number for TTL input connected to BNC*/
void setup() {
// Put your setup code here, to run once:
pinMode(LEDPin, OUTPUT); /*set LED as output*/
pinMode(TTLPin, INPUT); /*set TTL as input*/
}
// Function created to recall one pulse cycle:
void StimCycle() {
digitalWrite(LEDPin,HIGH);
delay(PulseOn); /*turns on LED for duration of PulseOn*/
digitalWrite(LEDPin,LOW);
delay(PulseOff); /*turns off LED for duration of PulseOff*/
}
// Function created to drive a certain number of pulse cycles:
void StimContCycles() {
for (int i = 0; i < StimOnTimes; i++) {
StimCycle();
}
}
//Function created to tether continuous pulse cycles with a cycle-off period:
void StimTrain() {
StimContCycles();
digitalWrite(LEDPin,LOW);
delay(StimOffInterval); /* repeated pulse cycles for length of StimOnInterval followed by LED off period for the length of StimOffInterval */
}
//[b]this is the part i need help with[/b]
void loop() {
// Put your main code here, to run repeatedly:
int NewState = digitalRead(TTLPin);
if (NewState == HIGH) {
while (NewState == HIGH) {
StimTrain();
}
}
else {
digitalWrite(LEDPin,LOW);
}
}