Go Down

Topic: Limiting action time in ' if statement' (Read 345 times) previous topic - next topic

Boulton

Larry, below is what I've done so far. Not a lot really as its just based on what we've discussed so far.
Things to ignore are the threshold value as I have to do some testing to see what readings I'll get from the current sensor when its connected to the 32v circuit.
Also the voltage sensor as I'm yet to decide on what I'll use to do this.

From here, how to I get it to repeat the process to the maximum three attempts? Is it by putting in some sort of counter and switching the flag between true and false?

Code: [Select]
// These constants won't change:
const int currentPin = A0;    // pin that the current sensor is attached to
const int voltagePin = A1;   // pin that the voltage sensor is attached to
const int relayPin = 22;     // pin that the relay is attached to
const int threshold = 400;   // the current draw threshold level that's in the range of the analog input
unsigned long starTime = 0    // global variable
unsigned long interval = 5000     // 5 seconds

void setup() {
  // initialize the RELAY pin as an output:
  pinMode(relayPin, OUTPUT);
  // initialize serial communications:
  Serial.begin(9600);
}

void loop() {
  // read the value of the current sensor:
  int analogValue = analogRead(currentPin);

  // if the analog value is high enough, power the relay:
  if (analogValue > threshold) {
    digitalWrite(relayPin, HIGH);
  starTime = millis();    // start timer
  }
 }
if (millis() - starTime >= 5000UL) {   // current time - start time >= 5 seconds
   digitalWrite (pin,LOW);
 }


Cheers
Chris

LarryD

#16
Nov 07, 2015, 10:15 am Last Edit: Nov 07, 2015, 10:18 am by LarryD
This will turn on the relay a maximum of three times.
You should be able to modify the code as needed from this example:
Code: [Select]

// These constants won't change:
const int currentPin = A0;     // pin that the current sensor is attached to
const int voltagePin = A1;     // pin that the voltage sensor is attached to
const int relayPin = 22;       // pin that the relay is attached to
//const int relayPin = 2;       // pin that the relay is attached to
const int threshold = 400;     // the current draw threshold level that's in the range of the analog input
unsigned long starTime = 0;    // global variable
unsigned long interval = 5000; // 5 seconds
bool relayFlag = false;
byte attempts = 0;


void setup() {
  // initialize the RELAY pin as an output:
  pinMode(relayPin, OUTPUT);
  // initialize serial communications:
  Serial.begin(9600);
}

void loop() {
  // read the value of the current sensor:
  int analogValue = analogRead(currentPin);
  Serial.println(analogValue);
 
  // if the analog value is high enough, power the relay:
  if (relayFlag == false && attempts < 3 && analogValue > threshold)
  {
    digitalWrite(relayPin, HIGH);
    relayFlag = true;
    starTime = millis();    // start timer
  }

  if(relayFlag == true && millis() - starTime >= 5000UL)
  {   
    // current time - start time >= 5 seconds
    digitalWrite(relayPin,LOW);
    delay(100);  //this could be removed if needed
    relayFlag = false;
    attempts = attempts + 1;
  }

} //END of loop

.
No technical PMs.
The last thing you did is where you should start looking.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy