Pages: [1]   Go Down
Author Topic: Code question stopping a program after an output is high for a period of time  (Read 214 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am new to programming my arduino and am looking for a little code help. I have looked through a bunch of examples but nothing seems to be similar to what I'm looking to do.  

I am building a auger system that will move material from one bin to another. I want to shut everything off if the first bin runs out of material so it doesn't just sit there and run for no reason. If I activate a switch the program should reset. Below is the code I'm currently running and my project works great.  I now need to add the code to stop the program.  This is also a safety feature so my auger motor doesn't run for too long and burn out.

Code:
    int sensorPin = 1;
    int Pin12 = 12;
    void setup(){
      Serial.begin(9600);
      pinMode(Pin12, OUTPUT);
      pinMode (sensorPin, INPUT); 
    }

    void loop()
    {
      int val = analogRead(sensorPin);
      Serial.println(val);


    if (analogRead(sensorPin)< 100)
    {
    digitalWrite(Pin12, HIGH);
    }
    if (analogRead(sensorPin)> 244)
    {
    digitalWrite(Pin12, LOW);
    }
    delay(1000);
    }

I would like to turn pin12 off if it has been on longer then 5 min and stay off until pin14 reads high. I would also like to read pin15 high if sensorPin >244 for longer then 6 min.  pin15 will activate a light that lets me know that the system is empty.  In the future I would like to use send a text or an email instead of just turning on a light but one thing at a time.  Thank you for any help you can provide.

Moderator edit: [code] [/code] tags added.
« Last Edit: December 28, 2012, 10:15:25 pm by Coding Badly » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    int Pin12 = 12;
Do you really has a 12 connected to this pin? I seriously doubt that. The purpose of using names is so that they mean something. If you decide, in the future that you need an Ethernet shield, and can no longer use pin 12, then when you assign a new value, like 8 for the pin, then pin12 will have a value of 8, and look really stupid.

Give this variable a meaningful name, like augerPin.

Code:
      int val = analogRead(sensorPin);
      Serial.println(val);
Here's another case where a better name is needed. Two, in fact. First, it is unlikely that you have a sensor connected to the pin. In generic terms, yes, it is a sensor. But, it is an almost certainty that it did not come in a white box that said nothing more than "Sensor" on it.

Certainly, the VALUE read from the pin means something that deserves a better name.

Code:
    if (analogRead(sensorPin)< 100)
    {
    digitalWrite(Pin12, HIGH);
    }
    if (analogRead(sensorPin)> 244)
    {
    digitalWrite(Pin12, LOW);
    }
Why is it necessary to read the pin three times? You have the value read the first time in the variable misnamed val. Use that variable instead of reading the pin twice more.

Code:
      pinMode (sensorPin, INPUT); 
This function, because analog pins can not be output pins, knows nothing about analog pins. So, it sets the mode for digital pin 1 to INPUT. That pin is controlled by the Serial instance. You should NOT be diddling with it. Get rid of this call.

Quote
I would like to turn pin12 off if it has been on longer then 5 min
You need to record when you turned the pin on. Use millis().

You need to clear the time when you turn the pin off (set it to 0).

On each pass through loop, you compare the current time to the time you turned on, if it is on, to see if it has been on longer than is safe. If so, turn it off.

Quote
and stay off until pin14 reads high.
This requirement needs clarification. Should the pin not turn on again only if it was shut down for safety reasons? If so, it is easy to set a flag (bool shutOffForSafety) to true when the pin is shut off for safety reasons. If that flag is set, don't turn the pin on. Clear the flag when pin 14 reads HIGH.

Quote
pin15 will activate a light that lets me know that the system is empty.
No. A variable with a meaningful name, assigned the value 15, will be used in the digitalWrite() call to activate/deactivate the pin. That variable will NOT be called pin15, will it?

Quote
In the future I would like to use send a text or an email instead of just turning on a light but one thing at a time.
Which is precisely why pin12 is a lousy name.
Logged

Pages: [1]   Go Up
Jump to: