Go Down

Topic: How do I create a confirmation and activation script? (Read 653 times) previous topic - next topic

benzure

For example, if the boolean is true it will flash a small LED using millis not delay.

Simple enough right? BUT there needs to be a time out, so if it's NOT TRUE AGAIN within 3 seconds and still under 4 successful times, it resets a value.

However, if the boolean is true 4 times within 3 seconds between them then it lights up the full light array.

Any ideas would be much appreciated! I think my brain has melted attempting this!

PaulS

Quote
Simple enough right?

Yes.

Quote
BUT there needs to be a time out, so if it's NOT TRUE AGAIN within 3 seconds and still under 4 successful times, it resets a value.

You need to define what causes the LED to think it needs to flash. You need to determine how long it has been since that event last occurred. You need to clear that flag if the time has been exceeded or if the number of failed attempts is excessive.

benzure

#2
Feb 26, 2013, 06:52 pm Last Edit: Feb 26, 2013, 06:55 pm by benzure Reason: 1
Am I on the right sort of tracks with this? It's all I can think of at the moment :smiley-red: I'm pretty new to this.

Code: [Select]
long startTime; // value from millis when boolean true
long duration;  // store the duration
int waitTime = 3000; // wait time
int globalCount = 0;
int counter = 0;
int ledState = LOW; // confirmation LED            
long previousMillis = 0;
long interval = 200;  
int conCounter = 0;

void setup() {
// led set up here
}

void triggerCon() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;  
if (ledState == LOW) {
ledState = HIGH;
conCounter++; }
else
ledState = LOW;
if (conCoutner < 4){
digitalWrite(ledPin, ledState); // only flash high 4 times?
}
if (conCounter > 4) {
conCounter = 0;  // flashes more than 4 times, reset to 0 and escape back to loop?
}
}
}

void loop() {

if (boolean = true && counter < 4) {
startTime = millis();
while(boolean = true); // wait
long duration = millis() - startTime;

   
if (duration >= waitTime) {
globalCount = 0;
// LARGE LED ARRAY ON
// DELAY
// LARGE LED ARRAY OFF
// DELAY
// WILL NOT ESCAPE UNTIL RESET
}
else if (duration < waitTime) {
globalCount++;
}
else {
triggerCon();
}



} // end of boolean if


} // end of loop

Arrch


Am I on the right sort of tracks with this?

Well, you're certainly not going to win any "Code Readability" Awards. There is an Auto Format tool in the Arduino's IDE that I recommend you get yourself acquainted with.

PaulS

Quote
There is an Auto Format tool in the Arduino's IDE that I recommend you get yourself acquainted with.

There is also the Verify icon that will attempt to compile and link, but not upload, your code. Posting code that at least compiles is considered polite, unless the question is "why won't my code compile?"

benzure

Apologies, I've come up with this code that uses a button rather than a boolean for now, just so that I don't get lost.

The obvious issue being the use of millis() as a stopwatch (which isn't going to work). So, is it best to use a 'stopwatch' library or is there an actual way of doing it without an additional library? I've only got around 3,100-3,800 bytes left to use.

Code: [Select]
int inPin = 2;         // the number of the input pin (switch)
int outPin = 3;       // the number of the output pin (LED)
long currentTime = 0;
long nextTime = 0;
long lastTime = 0;
int patCounter = 0;
int globalValue = 0;
int breakIn = 0;

void setup()
{
 pinMode(inPin, INPUT);
 pinMode(outPin, OUTPUT);
  Serial.begin(9600);
  Serial.println("GO");
}

void loop(){


if (digitalRead(inPin)==HIGH&&globalValue==0) { // true and only if 0
Serial.println("confirm"); // print confirmation
   Serial.println("CLEARED 1st + FLASH = X"); // cleared the counter
   delay (500); // flood prevention
   breakIn++;
   currentTime = 0; // flush? not going to work
   currentTime = millis(); // start counter
  if (currentTime<6000) { // less than 6s
    if (digitalRead(inPin)==HIGH&&breakIn==1) { // if high or true AGAIN within 6s
    Serial.println(currentTime); // print time, comment out
    Serial.println("CLEARED 2nd = 0 "); // was within 6s and cleared
     delay (500); // flood prevention
    patCounter++; // add alarm count value?? not needed?
   globalValue++; // increase global to diable loop flash
 }
}
if (currentTime>6000&&breakIn==1) { // if current time is greater than
      Serial.println("TIMEOUT 2nd STAGE"); // timeout, no button press or true
//reset
 breakIn = 0;
   patCounter = 0;
   globalValue = 0;
   currentTime = 0;
 }
}

// 3rd attempt
if (globalValue==1&&patCounter==1){
currentTime = 0; //flush, this wont work!
nextTime = millis(); // start a new counter or stopwatch and wait
if (nextTime<6000) { // if 3rd counter is less than 6s
if (digitalRead(inPin)==HIGH) { // and true
Serial.println("CLEARED 3rd = 1"); // cleared the stage
Serial.println(nextTime); // print time - fail
delay (500); // flood prevention for button
patCounter++;
} // if counter less
} // if true or high
if (nextTime>6000) {
        Serial.println("TIMEOUT 3rd STAGE"); // timeout
globalValue = 0;
patCounter = 0;
} // if more than
} // global master

// 4th attempt and LED array on
if (globalValue==1&&patCounter==2){ //  
currentTime = 0; // attempt to flush
lastTime = millis(); // start a new counter or stopwatch
if (lastTime<6000) { // if 3rd counter is less than 6s
if (digitalRead(inPin)==HIGH) { // and true or high
digitalWrite(outPin, HIGH); // LED on
Serial.println("CLEARED 4th latch"); // cleared the stage
Serial.println(lastTime); // print time
} // true or high
} // counter
if (lastTime>6000) {
      Serial.println("TIMEOUT 4th STAGE"); // cleared the counter
globalValue = 0;
patCounter = 0;
} // if more than
} // global


} // loop end  

Arrch


benzure

I've followed directions Arrch and the code above is verified and auto formatted...

PaulS

Quote
I've followed directions Arrch and the code above is verified and auto formatted...

I'm calling bullshit!
Code: [Select]
void loop(){


if (digitalRead(inPin)==HIGH&&globalValue==0) { // true and only if 0
Serial.println("confirm"); // print confirmation


That is NOT properly formatted.

Go Up