Go Down

Topic: heating control (Read 4949 times) previous topic - next topic

Joes

ok just dun your code on it's on just tuning led's on and off think i see how it work but not the delay and i could not get it to delay ether

Arrch


ok just dun your code on it's on just tuning led's on and off think i see how it work but not the delay and i could not get it to delay ether


So using the code in this post:
http://arduino.cc/forum/index.php/topic,108263.msg846778.html#msg846778

with just LEDs does not work? What are you expecting it to do and what is it doing?

Joes

Quote


So using the code in this post:
http://arduino.cc/forum/index.php/topic,108263.msg846778.html#msg846778

with just LEDs does not work? What are you expecting it to do and what is it doing?


that is the code i am using


i can see the (  if (lastState == HIGH)   ) working, but the code in side that what i ges is doing the delay seem's to be doing navall
so putting the LED on as soon as the button go's low

Arrch


i can see the (  if (lastState == HIGH)   ) working, but the code in side that what i ges is doing the delay seem's to be doing navall
so putting the LED on as soon as the button go's low


I don't know what the bolded means.

That code is designed to turn the LED on as soon as the button goes LOW for the length of time the button was held HIGH up to thirty seconds. What are you trying to do?

Joes

Quote


That code is designed to turn the LED on as soon as the button goes LOW for the length of time the button was held HIGH up to thirty seconds. What are you trying to do?


thats what i want it to do. it pulls the led low when the button goes low and there is no delay

Arrch

Code: [Select]
unsigned long switchHIGHTime = 0;
unsigned long fanTurnOnTime;


Should also be declared as static.

Joes

this is the example i'm tring to get to work

Code: [Select]


#define THIRTY_SECONDS 30000
const int ledPin =  13;     
const int buttonPin = 2;     

void setup() {               
pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}
void loop() {
 
  static unsigned long startTime;
  static int lastState = HIGH;
  int currentState = digitalRead(buttonPin);
  unsigned long now = millis();
  unsigned long switchHIGHTime = buttonPin;
  unsigned long fanTurnOnTime;
 
  if (now - fanTurnOnTime > switchHIGHTime)
    // Turn the fan off
   
  if (currentState == HIGH) {
    digitalWrite(ledPin, HIGH);
    // Code goes here to occur whenver the button is HIGH
    if (lastState == LOW) {
      startTime = now; // Time when the switch went to HIGH
    }
  } else {
    // Code goes here to occur whenver the button is LOW
    if (lastState == HIGH) {
      switchHIGHTime = now - startTime; // determine how long it was HIGH for
      if (switchHIGHTime > THIRTY_SECONDS)
        switchHIGHTime = THIRTY_SECONDS;
     
      fanTurnOnTime = now;
      digitalWrite(ledPin, LOW);
      // Turn the fan on here
    }
  }
  lastState = currentState;
}

Arrch


this is the example i'm tring to get to work


Se the above post.

Joes

so how do we do that and where ?
or is it as sipal as this;
  static unsigned long switchHIGHTime = 0;
  static unsigned long fanTurnOnTime;

Arrch


so how do we do that and where ?
or is it as sipal as this;
  static unsigned long switchHIGHTime = 0;
  static unsigned long fanTurnOnTime;

Did you try it?

Joes

yes still no delay though
when the button went low the light goes out instantly but then if you put the button back high it seems to delay a little whle before it goes high it almost seems the delay is the wrong way round lol well thats the illusion it giving anyway

Arrch

Sounds like the HIGH/LOWs are mixed up.

Joes

ive tried switching high's and low's all over and still not working

johncc


Code: [Select]
unsigned long switchHIGHTime = 0;
unsigned long fanTurnOnTime;


Should also be declared as static.


Is there a benefit to having the in the loop and declared as static, over putting them outside the loop and not declaring them static?

Thanks,
John

Arrch

So I decided to actually get it working on my system. This is basics without a debounce or the thirty second max (easy to add). This will turn the LED on when you first press the button. When you release the button, it will determine how long the button was pressed for and after that same amount of time, turn the LED off. I have a very bouncy button, so it doesn't work most of the time, but otherwise, it should work fine:

Code: [Select]
const int ledPin =  13;     
const int buttonPin = 2;     

void setup() {               
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH);
 
  Serial.begin(115200);
}
void loop() {

  static unsigned long timePressedDown;
  static unsigned long timeReleased;
  static unsigned long pressTime;
  static int lastState = HIGH;
 
  int state = digitalRead(buttonPin);
  unsigned long now = millis();
 
  // Check for the falling edge, indicating the button was pushed down
  if ( (lastState==HIGH) && (state==LOW) ) {
    // Turn on our LED
    digitalWrite(ledPin, HIGH);
    // Record the time the button was pressed
    pressTime = now;
  }
 
  // Check for the rising edge, indicating the button was released
  if ( (lastState==LOW) && (state==HIGH) ) {
    // Determine how long the button was pressed down for
    timePressedDown = now - pressTime;
    // Record the time the button was released
    timeReleased = now;
  }
 
  /* If the button is not pressed down, determine how long it's been since
   * the button was released. If it's been at least the length that the button
   * was pressed, turn the LED off. */
  if ( (state == HIGH) && (now - timeReleased > timePressedDown)) {
    digitalWrite(ledPin, LOW);
  }
 
  lastState = state;
}



Is there a benefit to having the in the loop and declared as static, over putting them outside the loop and not declaring them static?


The benefit to using these as static is keeping it within the scope that you want to use it. If you only want it to change within a single function, then you can use them as static to help prevent logic errors if you mistakenly change the variable outside of the function you are using it within.

Go Up