Go Down

Topic: stop loop? (Read 457 times) previous topic - next topic

Robin2

that is basically the same thing i'm attempting
Always assume someone else has invented the wheel before you started worrying about it.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

kau_cinta_ku

this may be a dumb question, reading though this thread http://forum.arduino.cc/index.php?topic=261445.0

why don't you have to use the switch/case function? or in otherwords whats the diffrence between your way and useing them?

Robin2

this may be a dumb question, reading though this thread http://forum.arduino.cc/index.php?topic=261445.0
why don't you have to use the switch/case function? or in otherwords whats the diffrence between your way and useing them?
Planning and Implementing a Program is a long Thread. Which specific part are you referring to?

If you are just asking why I did not illustrate SWITCH/CASE in that Thread (and I can't remember if I did) I guess the answer is that it did not occur to me - it was not my purpose in writing the Thread to cover all possible code options. IF/ELSE is a more general form for decision making.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

kau_cinta_ku

If you are just asking why I did not illustrate SWITCH/CASE in that Thread (and I can't remember if I did) I guess the answer is that it did not occur to me - it was not my purpose in writing the Thread to cover all possible code options. IF/ELSE is a more general form for decision making.

...R
that answered my question, ty.

the way in that thread makes setting up a code easy to learn and I am thankful for your help on the board.

econjack

Relative to the switch/case statement block: I once saw a "professional" block of code that was used to do certain processes depending upon which day of the month it was. It contained 31 nested if statement blocks, like:

Code: [Select]

if (day == 1) {
   DoDay1();
} else {
   if (day == 2) {
      DoDay2();
   }
// ...and so on for 31 days...


This structure is also called a cascading if block. This code block begs for a switch/case:

Code: [Select]

switch (day) {
   case 1:
      DoDay1();
      break;

   case 2:
      DoDay2();
      break;

   // do through day 31

   default:
      Serial.print("I should never be here, day = ");
      Serial.println(day);
      break;
}


So, why the switch/case option? First, you can easily do some clearly-understood defensive coding via the default block. Second, I think it's easier to read, but that may or may not be true for everyone. Third, the compiler will create a jump table and jump to specific code blocks depending upon the value of day. This can be more efficient than a cascading if block. For example, if it is the last day of the month, the code from some compilers would do 30 unnecessary tests on day before finding the appropriate code block. (Truth-be-told, I think the gcc compiler would create a jump table for both, but I haven't checked.) A third option is to create an array of pointers-to-functions and use day to index into that array. You may want to investigate that if you're comfortable with pointers.

Robin2

An important difference between SWITCH/CASE and IF/ELSE is the fact that the CASE options must be known at compile time. Using IF/ELSE you can do comparisons on values that change while the code is running.

I presume that makes SWITCH/CASE more efficient for the situations where it can be used.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Vaclav

#21
Feb 03, 2016, 05:41 pm Last Edit: Feb 03, 2016, 06:40 pm by AWOL

that answered my question, ty.

the way in that thread makes setting up a code easy to learn and I am thankful for your help on the board.
if / else construct allows evaluation of any condition , any variable

switch   evaluates only one variable

if( today == Tuesday )
   go for a walk
else if( sun  != shining & today == Wednesday )
    stay home

switch ( day of week)
case Monday
    ...
case Tuesday
    ...


Moderator edit: tags corrected. Again. This is getting tedious Vaclav. What are we going to do about it?


"Thunder is good, thunder is impressive; but it is lightning that does all the work"
Mark Twain

I do not read nor respond to thunderous posts.

kau_cinta_ku

ok I know I am missing something here, I have LED's hooked up in place of the 2 silenoids to test, and I do have the float switches hooked up.

the porgram compiles and transfers just fine but I can't seem to get anything from the led's.

Code: [Select]

const byte lowFloat = 8;
const byte highFloat = 9;
byte lowFloatstate;
byte highFloatstate;
const byte roSolenoid = 10;
const byte diSolenoid = 11;

// states
#define STOPPED 0
#define FLUSHING 1
#define FILLING 2
#define FULL 3

byte state = STOPPED;

unsigned long startTime = 0;
unsigned long currentTime = millis();
int flushTime = 10000;


void setup()
{
  pinMode(roSolenoid, OUTPUT);
  pinMode(diSolenoid, OUTPUT);
  pinMode(lowFloat, INPUT_PULLUP);
  pinMode(highFloat, INPUT_PULLUP);

}

void loop(){
  checkFloats();


  switch (state) {
  case STOPPED:
    tankEmpty();
    break;
  case FLUSHING:
    flushMembrane();
    break;
  case FILLING:
    fillTank();
    break;
  case FULL:
    tankFull();
    break;
  }

}

void checkFloats(){
  lowFloatstate = digitalRead(lowFloat);
  highFloatstate = digitalRead(highFloat);
}

void tankEmpty(){
  if (lowFloat == LOW){
    flushMembrane();
  }
}

void flushMembrane(){
  if (startTime == 0){
    startTime = millis();
    digitalWrite(roSolenoid, HIGH);
    state = FLUSHING;
  }
  else{
    if(currentTime - startTime > flushTime){
      digitalWrite(roSolenoid, LOW);
      if(lowFloat == LOW){
        digitalWrite(diSolenoid, HIGH);
      }
      else{
        startTime = 0;
        state = FILLING;
      }
    }
  }
}

void fillTank(){
  if(lowFloat == HIGH && highFloat == LOW){
    digitalWrite(diSolenoid, HIGH);
  }
  else{
    if(lowFloat == HIGH && highFloat == HIGH){
      state = FULL;
    }
  }
}

void tankFull(){
  if (highFloat == HIGH){
    digitalWrite(diSolenoid, LOW);
  }
}


PaulMurrayCbr

#23
Feb 08, 2016, 03:21 am Last Edit: Feb 08, 2016, 03:24 am by PaulMurrayCbr
i'm trying to setup a system that has a high and low float valve and 2 solenids. here is what i'm trying to do.

lower float switch is activated
solenid 1 opens for 3 min
after 3 min, solenoid 1 closes and solenoid 2 opens
solenid 2 stays open until the higher float switch is activated.

then I don't want the system to start again until the lower float is activated again.
Here ya go:
Code: [Select]

const byte lowFloat = 8;
const byte highFloat = 9;
const byte roSolenoid = 10;
const byte diSolenoid = 11;

enum State {
  OK = 0,
  FLUSHING = 1,
  FILLING = 2
} state;

unsigned long startFlushMs;
const unsigned long FLUSHING_MS = 3L * 60L * 1000L; // three minutes

void setup()
{
  pinMode(roSolenoid, OUTPUT);
  pinMode(diSolenoid, OUTPUT);
  pinMode(lowFloat, INPUT_PULLUP);
  pinMode(highFloat, INPUT_PULLUP);

}


void loop() {
  boolean low = digitalRead(lowFloat) == LOW;
  boolean high = digitalRead(highFloat) == LOW;

  // high and low will never be both on (LOW),
  // this line is just here for safety.

  if (high) low = false;

  switch (state) {
    case OK:
      if (low) {
        digitalWrite(roSolenoid, HIGH);
        startFlushMs = millis();
        state = FLUSHING;
      }
      break;

    case FLUSHING:
      if (high) {
        // should never happen
        digitalWrite(roSolenoid, LOW);
        state = OK;
      }
      else if (millis() - startFlushMs >=  FLUSHING_MS) {
        digitalWrite(roSolenoid, LOW);
        digitalWrite(diSolenoid, HIGH);
        state = FILLING;
      }
      break;

    case FILLING:
      if (high) {
        digitalWrite(diSolenoid, LOW);
        state = OK;
      }
      break;
  }
}
WP personal, WP work-related, twitter

kau_cinta_ku

Here ya go:
Code: [Select]

const byte lowFloat = 8;
const byte highFloat = 9;
const byte roSolenoid = 10;
const byte diSolenoid = 11;

enum State {
  OK = 0,
  FLUSHING = 1,
  FILLING = 2
} state;

unsigned long startFlushMs;
const unsigned long FLUSHING_MS = 3L * 60L * 1000L; // three minutes

void setup()
{
  pinMode(roSolenoid, OUTPUT);
  pinMode(diSolenoid, OUTPUT);
  pinMode(lowFloat, INPUT_PULLUP);
  pinMode(highFloat, INPUT_PULLUP);

}


void loop() {
  boolean low = digitalRead(lowFloat) == LOW;
  boolean high = digitalRead(highFloat) == LOW;

  // high and low will never be both on (LOW),
  // this line is just here for safety.

  if (high) low = false;

  switch (state) {
    case OK:
      if (low) {
        digitalWrite(roSolenoid, HIGH);
        startFlushMs = millis();
        state = FLUSHING;
      }
      break;

    case FLUSHING:
      if (high) {
        // should never happen
        digitalWrite(roSolenoid, LOW);
        state = OK;
      }
      else if (millis() - startFlushMs >=  FLUSHING_MS) {
        digitalWrite(roSolenoid, LOW);
        digitalWrite(diSolenoid, HIGH);
        state = FILLING;
      }
      break;

    case FILLING:
      if (high) {
        digitalWrite(diSolenoid, LOW);
        state = OK;
      }
      break;
  }
}

ty for that, but not what i had in mind, that is still useing only 1 float switch and shuts off right when it goes high. both floats will have to be low to activate the unit. i will try to get a diagram or explain it better.

Robin2

#25
Feb 08, 2016, 05:19 pm Last Edit: Feb 08, 2016, 05:19 pm by Robin2
ty for that, but not what i had in mind, that is still useing only 1 float switch and shuts off right when it goes high. both floats will have to be low to activate the unit. i will try to get a diagram or explain it better.
This sounds like the same issue that I thought we had disposed of in Replies 1, 3 and 6

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

kau_cinta_ku

This sounds like the same issue that I thought we had disposed of in Replies 1, 3 and 6

...R
yeah it was. after some extra messing around i think I finally got it the way i need it though. I will also add a button to bypass the floats and have it fill that way also. i will have to make a test setup with water first, then get the 12v solenieds wired up. but i apperciate all the help that was given, it has helpped me out alot.

PaulMurrayCbr

This sounds like the same issue that I thought we had disposed of in Replies 1, 3 and 6
A sad and oft-told tale. A computer doesn't do what you want, it does what you tell it to.
WP personal, WP work-related, twitter

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