I have literally zero coding experience but have run into the need for it at my job -- all help appreciated. Basically, I am trying to accomplish many things at once and am not sure how to translate what i need into code. What I need: a tone and a light to simultaneously flash on for 500 milliseconds and then off again for 6 seconds; water reward to be delivered (by turning on/ off a solenoid) 2 seconds after the tone/LED have turned off, with the solenoid being open for 25 milliseconds; continuous collection of data from an input potentiometer. Since i need to monitor the input from the potentiometer at all times, i cannot use "delay()" because i cannot have the board stop reading the values from the potentiometer to preform a function.
This is a code i attempted -- however, the issue is the timing for the LED/tone and the solenoid. I need the solenoid to always open 2 seconds after the LED/tone turn off and it needs to open for 25 milliseconds and then shut back off. i have put hours into trying to figure out a way to do this --- as the code is now, the led/tone turn on and off fine, but the solenoid has a longer delay time (because i was trying to get it to come 2 seconds after the led/tone went off) and the excess time builds onto itself and makes the timing of the solenoid vary in its proximity to the LED/tone.
#include <elapsedMillis.h> //the timer.
//stopwatches
elapsedMillis timer;
int ledPin = 11;
int lick = 4;
int solenoid = 8;
int speaker = 2;
int ledOn = 500; //milliseconds
int ledOff = 6000;
int solenoidOn = 25;
int solenoidOff = 8475;
int LickState;
int playTime = 500; //how long speakers will play for
int freq = 8000; //frequency for left prize speakers in hz
int toneTime = 500;
unsigned long lms; //time from millis()
unsigned long lmsLast; //last time the LED changed state
unsigned long sms; //time from millis()
unsigned long smsLast;
unsigned int LedOn = 0;
unsigned int Reward = 0;
unsigned int Lick = 0;
boolean ledState; //current LED state
boolean solenoidState;
void setup(void){
Serial.begin(9600);
pinMode(lick, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(solenoid, OUTPUT);
pinMode(speaker, OUTPUT);
elapsedMillis timer = 0;
}
void loop(void){
int ledState = digitalRead(ledPin);
int lickstate = digitalRead(lick);
if (lickstate == HIGH){
// Serial.println(lickstate);
Serial.println(timer); }
lms = millis();
blinkLED();
sms = millis();
blinkLED();
} }
void blinkLED(void){
if (lms - lmsLast > (ledState ? ledOn : ledOff)) {
digitalWrite(ledPin, ledState = !ledState);
lmsLast = lms;
tone (speaker, freq, toneTime);
Serial.print("Led");
Serial.println(timer);
}
if (sms - smsLast > (solenoidState ? solenoidOn : solenoidOff)) {
digitalWrite(solenoid, solenoidState = !solenoidState);
smsLast = sms;
Serial.print("Reward");
Serial.println(timer);
}
}
I have tried using if then statements to say that if the LED state is HIGH, then wait 2 seconds and deliver the reward --- the issue is the "wait 2 seconds part". how do i get the arduino to add two seconds to the current time when the LED turns off and then give my 25 millisecond reward without messing up the on/off times of the LED? Very confused.