Pages: [1]   Go Down
Author Topic: "until" programming question  (Read 1051 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

so I was wondering if there is some sort of an "until" function,
 I havnt come across something like it before. basically I have an analog input returning a number between 0 and 1024 and when the input reads above 600 I want it to write another pin high. I know how to do this but what I want in addition is to keep the pin high until the analog input reads something like 550. I assume this should be pretty easy I just dont know the command for it.
    any help would be greatly appreciated.
Logged

Chile
Offline Offline
Edison Member
*
Karma: 35
Posts: 1251
Arduino rocks?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You can use:
Code:
do { /* something */ } while( /* this is true */ );

Or
Code:
while( /* this is true */ ) { /* do this */ }
Logged

My website: http://ried.cl

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If the question is "where's until in C/C++", eried has already answered. Otherwise, you could just post a sample sketch to do what you described and in case it doesn't work we could try to help.
Logged

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 22
Posts: 1675
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If I understand correctly, you want to turn something on, lets say its a cooling fan if the input is above a threshold, and you then want to keep it on until the input falls to a level lower than the trigger threshold -

Quote
// put this before setup
#define TRIGGER_LEVEL 600
#define CANCEL_LEVEL 500

// put this inside loop
int nInput = analogRead(0); // or whatever your input is
if(nInput > TRIGGER_LEVEL)
{
 digitalWrite(OUTPUT_PIN,HIGH);
}
else if(nInput < CANCEL_LEVEL)
{
 digitalWrite(OUTPUT_PIN,LOW);
}

// this does not block like a while loop would, you can so something else with the rest of your loop function
//
//

Duane B

rcarduino.blogspot.com
Logged


Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

tsk tsk tsk.. Duane, you should know code goes inside tags  smiley-evil
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

DuaneB its like you read my mind, that's exactly what I'm doing with the code. thanks!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

so here is my code, im sure it could be made much cleaner but im not to great with the programming side of things so i kind of just pieced together various examples. I dont have any of the hardware yet so I cant test if it will work. (ie I don't know if it will turn a fan on when the value gets over 600 and off when it is below 580) so if someone could let me know if this seems right that would be great. as for the rest of the code I feel like there must be a better way to write it and I want to fit it onto an ATtiny24 and i might want to add some more so it would be nice if it could be written more efficiently.

Code:
int counter = 3;
int up = 9;
int down = 10;
int LEDR = 8;
int LEDB = 7;
int LEDG = 6;
int sensPin = A2;
int relayPin = 5;
int tempPin = A3;

void setup()
{
  pinMode(sensPin, INPUT);
  pinMode(up, INPUT);
  pinMode(down, INPUT);
  pinMode(LEDR, OUTPUT);
  pinMode(LEDG, OUTPUT);
  pinMode(LEDB, OUTPUT);
  pinMode(relayPin, OUTPUT);
  pinMode(tempPin, INPUT);
}

void loop() {
  int val = analogRead(tempPin);
  //this makes sure the system only runs if it is above freezing
  if(val > 260){
  int upval = digitalRead(up);
  //there will be a SPDT momentary toggle switch
  //which will scroll through the different temperature options
  int downval = digitalRead(down);
  if(upval == HIGH)
  {
    delay(200); 
    counter ++;
  if(downval == HIGH)
   {
   delay(200);
   counter --;
   } 
    if(counter == 6)
    {
      counter = 1;
    }
  }

  else
    switch (counter) {
    case 1:
      {analogWrite(LEDR, LOW);
      analogWrite(LEDB, HIGH);
      analogWrite(LEDG, LOW);
      int val = analogRead(sensPin);
      if(val > 600)
      {
      digitalWrite(relayPin, HIGH);
      }
      else if(val < 580);
      {
      digitalWrite(relayPin, LOW);
      }
      delay(100);
      break;}
    case 2:
      {analogWrite(LEDR, LOW);
      analogWrite(LEDG, 75);
      analogWrite(LEDB, HIGH);
      int val = analogRead(sensPin);
      if(val > 560)
      {
      digitalWrite(relayPin, HIGH);
      }   
      else if(val < 540)
      {
      digitalWrite(relayPin,LOW);
      }
      delay(100);
      break;}
    case 3:
      {analogWrite(LEDR, LOW);
      analogWrite(LEDG, HIGH);
      analogWrite(LEDB, LOW);
      int val = analogRead(sensPin);
      if(val > 500)
      {
      digitalWrite(relayPin, HIGH);
      }
      else if(val < 480);
      {
      digitalWrite(relayPin,LOW);
      }
      delay(100);
      break;}
    case 4:
      {analogWrite(LEDR, 75);
       analogWrite(LEDB, 75);
       analogWrite(LEDG, HIGH);
       int val = analogRead(sensPin);
       if(val > 470)
       {
       digitalWrite(relayPin, HIGH);
       }
       else if(val < 450);
       {
       digitalWrite(relayPin,LOW);
       }
       delay(100);
       break;}
    case 5:
      {analogWrite(LEDR, HIGH);
       analogWrite(LEDB, LOW);
       analogWrite(LEDG, LOW);
       int val = analogRead(sensPin);
       if(val > 440)
       {
       digitalWrite(relayPin, HIGH);
       }
       else if(val < 420);
       {
       digitalWrite(relayPin,LOW);
       }
       delay(100);
       break;}
       
    }
  }
 else{
 digitalWrite(relayPin, LOW);
 digitalWrite(LEDR, LOW);
 digitalWrite(LEDG, LOW);
 digitalWrite(LEDB, 75);
 delay(300);
 digitalWrite(LEDB, LOW);
 delay(2000);
 
 }
}
 
Logged

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

Why is the downval test inside the upval block? It seems to me that you want to increment or decrement counter independently.
Code:
  if(upval == HIGH)
  {
    delay(200); 
    counter ++;
  if(downval == HIGH)
   {
   delay(200);
   counter --;
   } 
    if(counter == 6)
    {
      counter = 1;
    }
  }
What happens if counter is decremented below 0? The three if statements need to be completely independent.

Increment, if appropriate.
Decrement, if appropriate.
Reset counter, if appropriate.
Operate on the value of counter.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

oh yeah you are right, when I originally wrote this I only had an up switch so I just completely forgot to add a line to stop the counter from going below zero
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
const int NUM_OPTIONS = 6;
int counter = 3;
int up = 9;
int down = 10;
int LEDR = 8;
int LEDB = 7;
int LEDG = 6;
int sensPin = A2;
int relayPin = 5;
int tempPin = A3;

void setup()
{
    pinMode(sensPin, INPUT);
    pinMode(up, INPUT);
    pinMode(down, INPUT);
    pinMode(LEDR, OUTPUT);
    pinMode(LEDG, OUTPUT);
    pinMode(LEDB, OUTPUT);
    pinMode(relayPin, OUTPUT);
    pinMode(tempPin, INPUT);
}

void loop() {
    int val = analogRead(tempPin);
    //this makes sure the system only runs if it is above freezing
    if(val >  260) {

        int upval = digitalRead(up);
        if(upval == HIGH) {
            counter++;
            if (counter > NUM_OPTIONS) {
                counter = 1;
            }
        }

        int downval = digitalRead(down);
        if(downval == HIGH) {
            counter--;
            if (counter <= 0) {
                counter = NUM_OPTIONS;
            }
        }


        switch (counter) {
        case 1:
            {
                analogWrite(LEDR, LOW);
                analogWrite(LEDB, HIGH);
                analogWrite(LEDG, LOW);
                int val = analogRead(sensPin);
                if(val > 600)
                {
                    digitalWrite(relayPin, HIGH);
                }
                else if(val < 580);
                {
                    digitalWrite(relayPin, LOW);
                }
                delay(100);
                break;
            }
        case 2:
            {
                analogWrite(LEDR, LOW);
                analogWrite(LEDG, 75);
                analogWrite(LEDB, HIGH);
                int val = analogRead(sensPin);
                if(val > 560)
                {
                    digitalWrite(relayPin, HIGH);
                }   
                else if(val < 540)
                {
                    digitalWrite(relayPin,LOW);
                }
                delay(100);
                break;
            }
        case 3:
            {
                analogWrite(LEDR, LOW);
                analogWrite(LEDG, HIGH);
                analogWrite(LEDB, LOW);
                int val = analogRead(sensPin);
                if(val > 500)
                {
                    digitalWrite(relayPin, HIGH);
                }
                else if(val < 480);
                {
                    digitalWrite(relayPin,LOW);
                }
                delay(100);
                break;
            }
        case 4:
            {
                analogWrite(LEDR, 75);
                analogWrite(LEDB, 75);
                analogWrite(LEDG, HIGH);
                int val = analogRead(sensPin);
                if(val > 470)
                {
                    digitalWrite(relayPin, HIGH);
                }
                else if(val < 450);
                {
                    digitalWrite(relayPin,LOW);
                }
                delay(100);
                break;
            }
        case 5:
            {
                analogWrite(LEDR, HIGH);
                analogWrite(LEDB, LOW);
                analogWrite(LEDG, LOW);
                int val = analogRead(sensPin);
                if(val > 440)
                {
                    digitalWrite(relayPin, HIGH);
                }
                else if(val < 420);
                {
                    digitalWrite(relayPin,LOW);
                }
                delay(100);
                break;
            }
        }
    }
    else {
        digitalWrite(relayPin, LOW);
        digitalWrite(LEDR, LOW);
        digitalWrite(LEDG, LOW);
        digitalWrite(LEDB, 75);
        delay(300);
        digitalWrite(LEDB, LOW);
        delay(2000);
    }
}


Some random tips:

Properly indent your code; CTRL+T is your friend.

Use consistent variable names and capitalization. upBtnPin and downBtnPin would be better names, since you already have variables like sensePin.

A variable name should describe its role in the program. Counter here is not a counter, but a temperature option, so a better name would be for example currTempOption.
If you'd make counter go from 0 to 5 instead of 1 to 6 you could use it as an index into one or more arrays which would hold the various led pwm levels and temperature threshold. You could then have only a few lines of code instead of a "big" select/case where each option has the same code flow but different data.
BTW, case 6 is missing.

I think you mixed analogWrite and digitalWrite...

HTH
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

these are all very good tips, and yeah the code is much more readable when it is properly indented like that. And I actually had digitalWrite but for some reason it didnt work so i switched it to analog.

in other news i just signed up for and arduino 101 and 201 class at a local hackerspace, so hopefully I can give advice instead of only getting it!
Logged

Pages: [1]   Go Up
Jump to: