What I want to be able to do is read an output voltage for a specific amount of time while sanctimoniously running a motor for that same amount of time.
Here is my circuit in simulation where the blue led represents the voltage I am reading and red led represents the motor.
In the simulation both led's turn on but they stay on well past 10 seconds which is what I am trying to get them to turn off by.
In real life I have a different relay its a JZC-11. Also, I put a photo-resistor in place of the blue led and I put an led in parallel with the coil. When I run the code my photo-resistor works and collects data (past the time limit) and the led that's in parallel with the relays coil turns on but the 9v battery isn't running the motor which replaced the red led. My best guess is the relay was somehow faulty.
Any idea what I am doing wrong IRL, is it a faulty relay? Why doesn't the red led turn off after 10 seconds in the simulation?
Hi,
Your sketch is running correctly as you have written it.
It turns the LED on for 10Seconds.
Then turns the LED OFF for one loop. (very short time).
Then goes back and turns the LED on again for 10Seconds.
If you only want it to execute once then stop, put your loop program in the last bit of the setup.
Each reset will then restart it.
The loop is doing what it is supposed to, loop continuously through your sketch.
Load this modded version of your sketch to see, and open monitor.
I have added a 1000mS delay at LED off to show it is going OFF.
//#include "MegunoLink.h"
int relayPin = 11;
//TimePlot MyPlot;
void setup()
{
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
Serial.println("Analog Sensor Plotter");
}
void loop()
{
digitalWrite(relayPin, HIGH);
runL();
Serial.println(" LED OFF");
digitalWrite(relayPin, LOW);
delay(1000); // added to show off time
}
void runL()
{
for ( int x = 0; x < 10; x++)
{
//float SensorValue = analogRead(3);
//float voltage = SensorValue * (5.0 / 1023.0);
//MyPlot.SendData("My Sensor", voltage);
Serial.print(" x = ");
Serial.print(x);
Serial.println("LED ON");
delay(1000);
}
}
making a show of being morally superior to other people.
"what happened to all the sanctimonious talk about putting his family first?"
synonyms: self-righteous, holier-than-thou, churchy, pious, pietistic, moralizing, unctuous, smug, superior, priggish, mealy-mouthed, hypocritical, insincere, for form's sake, to keep up appearances; informalgoody-goody, pi;
rarereligiose, Pharisaic, Pharisaical, Tartuffian
"one tries to set a bit of an example, if that's not too sanctimonious"
I think I understand the problem here I guess my question is there a way for me not to include the loop function? I don't really have any need in my application for a continuous loop function.
vysero:
I think I understand the problem here I guess my question is there a way for me not to include the loop function? I don't really have any need in my application for a continuous loop function.
Put all your code in setup()
But, you can also put your loop() code in an infinite loop whenever you want.
.
looking at these tutorials it seems they all give great examples of how to get multiple operations to run at the same time the only problem is each individual operation runs forever. I need to be able to run for a specific amount of time and then stop running.
In other words I do not need to use a loop function, I do not need something to continuously loop forever.
Theoretical run of my circuit:
Code turns on a pump lets it run for a specified amount of time lets say 5 min. At the 5 min mark it then turns the pump off and starts collecting data and running a motor for say 10 min. Then after 10 min it turns off the data collection and turns of the motor and the run is over. In theory I would think the code should look something like this:
//#include "MegunoLink.h"
int relayPin = 11;
int relayPin2 = 12;
//TimePlot MyPlot;
void setup()
{
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
pinMode(relayPin2, OUTPUT);
//Serial.println("Analog Sensor Plotter");
// run pump for 5 seconds
for( int x = 0; x<5; x++){
digitalWrite(relayPin2, HIGH);
delay(1000);
}
digitalWrite(relayPin2, LOW)
//Since runL only runs for 10 seconds then the relay controlling the voltage to the motor should
//only run for 10 seconds.
digitalWrite(relayPin, HIGH);
runL();
digitalWrite(relayPin, LOW);
}
void runL()
{
// run sensor for 10 seconds
for( int x = 0; x<10; x++){
//float SensorValue = analogRead(3);
//float voltage = SensorValue * (5.0 / 1023.0);
//MyPlot.SendData("My Sensor", voltage);
delay(1000);
}
}
void loop(){}
However the simulator is not showing the led's (which are representing the motors of the pump and the DC motor respectively) as turning off at the correct intervals.
I do not need the pump to turn at the same time as the motor and the sensor in fact i would prefer running the pump first then turning it off and afterwords running the motor and the sensor.
vysero:
However the simulator is not showing the led's (which are representing the motors of the pump and the DC motor respectively) as turning off at the correct intervals.
The simulator is wrong. Which are you using?
I even just ran it in my simulator, (Virtronics Simulator for Arduino), and the pump LED, (relayPin2), was on for 5 seconds, then the motor LED, (relayPin), was on for 10 seconds, then everything stopped.
Your code should work, if you just change it from 5 seconds/10 seconds to 5 minutes/10 minutes as you mentioned in reply #12.
Just a quick point:-
In this, you don't need to turn the pump on inside the 'for' loop:-
// run pump for 5 seconds
for ( int x = 0; x < 5; x++)
{
digitalWrite(relayPin2, HIGH);
delay(1000);
}
digitalWrite(relayPin2, LOW)
You only need to write the pin HIGH once:-
// run pump for 5 seconds
digitalWrite(relayPin2, HIGH);
for ( int x = 0; x < 5; x++)
{
delay(1000);
}
digitalWrite(relayPin2, LOW)
or even:-
// run pump for 5 seconds
digitalWrite(relayPin2, HIGH);
delay(5000);
digitalWrite(relayPin2, LOW)
Excellent thank you. I was wondering if it wasn't my simulator. I think its hitting that final loop function and getting confused. Also, your definitely right I do not need a loop just a delay makes sense to me thanks.
vysero: looking at these tutorials it seems they all give great examples of how to get multiple operations to run at the same time the only problem is each individual operation runs forever. I need to be able to run for a specific amount of time and then stop running.
The usual way to deal with that is to have a variable (usually referred to as flag) which records whether the thing has been done. Something like this
boolean pumpDone = false;
void setup() {
// stuff
}
void loop() {
// stuff
runPump();
// more stuff
}
void runPump() {
if (pumpDone == false) {
pumpDone = true;
// run the pump
}
}
I think it's equally valid to just put the code in 'setup()' and leave 'loop()' empty as he's done, when the code only has to execute once. There's no need to over-complicate things for no reason.