Go Down

Topic: Timer with no delay? (Read 578 times) previous topic - next topic

vmansmx5


:smiley-eek:well im having some problems with this what im trying to do is i have 2pots that are set to differnt values used for the delays. and a switch + 2 realys controling 2 leds.
so what im trying to do if the switch is turnnd on it reads the on delay it delays then the leds turn on.
if the switch gose off it reads the off delay it delays then turns the leds off
but if the switch is off its still reading that delay so if the switch gose on its doing the off delay then gose to the on delay so the delay is too long
:smiley-eek:
Code: [Select]

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,20,21);

int potPin = A6; // Potentiometer Pin
int potPin1 = A7; // Potentiometer Pin
int led1 = 5; // Relay 1
int led2 = 6; // Relay 2

int potValue = 0; // variable from the sensor
int potValue1 = 0;

const int buttonPin0 = A0;   
int buttonState0 = 0; 

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(buttonPin0, INPUT);
  Serial.begin(9600);
  lcd.init();                     
  lcd.backlight();
  lcd.begin(20, 4);
}

void loop(){
  buttonState0 = digitalRead(buttonPin0);
 
  potValue = analogRead(potPin);
  potValue = map(potValue, 0, 1023, 1000, 60000);
 
  potValue1 = analogRead(potPin1);
  potValue1 = map(potValue1, 0, 1023, 1000, 60000);
 
  lcd.setCursor(7, 0);
  lcd.print("TIMER");
  lcd.setCursor(0, 1);
  lcd.print("ON  DELAY:");
  lcd.setCursor(10, 1);
  lcd.print(potValue);
  lcd.print("   ");
  lcd.setCursor(0, 2);
  lcd.print("OFF DELAY:");
  lcd.setCursor(10, 2);
  lcd.print(potValue1);
  lcd.print("   ");
 
  if (buttonState0 == HIGH) {     
   
    Serial.println("1 ON"); //A0 = DIP 1
    Serial.println(potValue);
    delay(potValue);
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
   
  }
  else {
   
    Serial.println("1 OFF");
    Serial.println(potValue1);
    delay(potValue1);
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
   
  }
}

dhenry

Your writing is hard to follow but sounds like you want to read switch transitions rather than levels. If so, you will need to save the previous reads.

vmansmx5

its like when the switch is off its running this parts of the program so if the switch gose on its waiting for that delay to finish befor it dose the ON delay then turns on the LEDs
Code: [Select]

  }
  else {
   
    Serial.println("1 OFF");
    Serial.println(potValue1);
    delay(potValue1);
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
   
  }
}


Quote

want to read switch transitions rather than levels

? have you got example code for this

vmansmx5

Made some changes to the display so it tells me when the delays are on.
so it keeps looping in this part of the program when the switch is low
Code: [Select]

}
  else {
   
    Serial.println("1 OFF");
    Serial.println(potValue1);
    lcd.setCursor(3, 3);
    lcd.print("OFF");
    lcd.print("   ");
    lcd.setCursor(16, 3);
    lcd.print("OFF");
    lcd.print("   ");
    delay(potValue1);
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
   
  }
}

how can i stop this happing.
i only wont it to run this part if the switch gose from HIGH to LOW
But if the switch is on the programe keeps looping there aswell i only wont it to run it once then wait for a change in the switch.

Code: [Select]

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,20,21);

int potPin = A6; // Potentiometer Pin
int potPin1 = A7; // Potentiometer Pin
int led1 = 5; // Green
int led2 = 6; // Green

int potValue = 0; // variable from the sensor
int potValue1 = 0;

const int buttonPin0 = A0;   
int buttonState0 = 0; 

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(buttonPin0, INPUT);
  Serial.begin(9600);
  lcd.init();                     
  lcd.backlight();
  lcd.begin(20, 4);
}

void loop(){
  buttonState0 = digitalRead(buttonPin0);
 
  potValue = analogRead(potPin);
  potValue = map(potValue, 0, 1023, 1000, 60000);
 
  potValue1 = analogRead(potPin1);
  potValue1 = map(potValue1, 0, 1023, 1000, 60000);
 
  lcd.setCursor(7, 0);
  lcd.print("TIMER");
  lcd.setCursor(0, 1);
  lcd.print("ON  DELAY:");
  lcd.setCursor(10, 1);
  lcd.print(potValue);
  lcd.print("   ");
  lcd.setCursor(0, 2);
  lcd.print("OFF DELAY:");
  lcd.setCursor(10, 2);
  lcd.print(potValue1);
  lcd.print("   ");
  lcd.setCursor(0, 3);
  lcd.print("SW:");
  lcd.setCursor(10, 3);
  lcd.print("DELAY:");
  lcd.setCursor(16, 3);
  lcd.print("   ");
  delay(500);
 
 
  if (buttonState0 == HIGH) {     
   
    Serial.println("1 ON"); //A0 = DIP 1
    Serial.println(potValue);
    lcd.setCursor(3, 3);
    lcd.print("ON");
    lcd.print("   ");
    lcd.setCursor(16, 3);
    lcd.print("ON");
    lcd.print("   ");
    delay(potValue);
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
   

   
  }
  else {
   
    Serial.println("1 OFF");
    Serial.println(potValue1);
    lcd.setCursor(3, 3);
    lcd.print("OFF");
    lcd.print("   ");
    lcd.setCursor(16, 3);
    lcd.print("OFF");
    lcd.print("   ");
    delay(potValue1);
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
   
  }
}

firefoxik

What about to use attachInterrupt http://arduino.cc/en/Reference/AttachInterrupt
That can do the job for U, only think about debouncing switch, or small capacitor on input...

Go Up