light timer help

i am not new to arduino but i do need help now and again. i have made a light timer but would like to not use delay. i have also made an app for my phone and it all works by bluetooth. i just would like to be able to still control my lights while the timer is going. here is my code and help and advice would be great thank you.

#include <Servo.h> 
#include <IRremote.h>
#include <IRremoteInt.h>

Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0; 

char val;

IRsend irsend;

void setup()
{
  pinMode(13,OUTPUT);
  Serial.begin(9600);
  myservo.attach(9);
}
 
void loop() {
  if( Serial.available() )
  {;}
    val = Serial.read();
 
  if(val=='u')
  {
    digitalWrite(13,HIGH);
    delay(50);
    digitalWrite(13,LOW);
    delay(50);  
    myservo.write(15);
    delay(420);
    myservo.write(90);
  }
    if(val=='i')
  {
    digitalWrite(13,HIGH);
    delay(100);
    digitalWrite(13,LOW);
    delay(50);  
    myservo.write(145);
    delay(420);
    myservo.write(90);
  }
      if(val=='o')
  {
    digitalWrite(13,HIGH);
    delay(100);
    digitalWrite(13,LOW);
    delay(600000);  //10 min
    myservo.write(145);
    delay(420);
    myservo.write(90);
  }
      if(val=='p')
  {
    digitalWrite(13,HIGH);
    delay(100);
    digitalWrite(13,LOW);
    delay(1800000);  //30 min
    myservo.write(145);
    delay(420);
    myservo.write(90);
  }
      if(val=='h')    //led lights
  {
    digitalWrite(13,HIGH);
    delay(100);
    digitalWrite(13,LOW);
    delay(18); 
    irsend.sendNEC(0xF7C03F, 32);  //on
  }
      if(val=='j')    //led lights
  {
    digitalWrite(13,HIGH);
    delay(100);
    digitalWrite(13,LOW);
    delay(18); 
    irsend.sendNEC(0xF740BF, 32);  //off
  }
      if(val=='k')    //led lights
  {
    digitalWrite(13,HIGH);
    delay(100);
    digitalWrite(13,LOW);
    delay(600000); //10 minutes 
    irsend.sendNEC(0xF740BF, 32);
  }
      if(val=='l')    //led lights
  {
    digitalWrite(13,HIGH);
    delay(100);
    digitalWrite(13,LOW);
    delay(1800000); //30 minutes 
    irsend.sendNEC(0xF740BF, 32);
  }
}

light_conrtoller_BT.ino (1.77 KB)

Look at the very first entry in this forum "Demonstration code......"

It should answer your question.

Weedpharma

Yes I saw the first post and I am only asking for help because I don't quite understand how to implement it. I get a little lost I am not as good as i once was sadly.

Bump! Can anyone help I just Wana get rid of the 10 min and the 30 min delay again any help would be greatly appreciated thank you

Please look at Blink Without Delay. It's not that hard. And a good idea is to first make a simple sketch where you use it and later try to implement it in this sketch.

And if you want more help around here, place your code in code tages ([code ] //code[/code ]). It's completely unreadable now.

you should at least fix this

if( Serial.available() )
  {;}
    val = Serial.read();

to

if( Serial.available() > 0)
  {
    val = Serial.read();
}
else val = 0;

Ps991:
you should at least fix this

if( Serial.available() )

{;}
    val = Serial.read();




to



if( Serial.available() > 0)
  {
    val = Serial.read();
}
else val = 0;

the whole code works perfectly fine i just cant do anything till the delay ends ill keep working on it and post back

septillion:
Please look at Blink Without Delay. It's not that hard. And a good idea is to first make a simple sketch where you use it and later try to implement it in this sketch.

And if you want more help around here, place your code in code tages ([code ] //code[/code ]). It's completely unreadable now.

Thank you I did not know that about the code part

Bump

oldsmarracin:
Bump

You need more than that to let us know where your problem is after reading the examples.

Weedpharma

i finally figured it out and it works flawlessly

#include <Servo.h> 
#include <IRremote.h>
#include <IRremoteInt.h>

Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0; 
int B = false;
char val;
long previousMillis = 0;
long intervala = 600000;

IRsend irsend;

void setup()
{
  pinMode(13,OUTPUT);
  pinMode(12,OUTPUT);
  Serial.begin(9600);
  myservo.attach(9);
}
 
void loop() {
  if( Serial.available() )
  {;}
    val = Serial.read();
    
  unsigned long currentMillis = millis(); 
 
      if(val=='o')
  { 
      B = true;
      digitalWrite(13,HIGH);
      delay(100);
      digitalWrite(13,LOW);
  } 
  
    if((currentMillis - previousMillis > intervala)&&(B == true))
  {
      previousMillis = currentMillis;  
      myservo.write(145);
      delay(420);
      myservo.write(90); 
      B=false;
  }
}

this is just the one part but all together it works i can still control my lights while the timer is running

if( Serial.available() )
  {;}
    val = Serial.read();

I repeat...CHANGE THIS! This is poor poor poor programming even after you have been advised to change it.

You dont seem to understand that "if( Serial.available() ) {;}" does LITERALLY NOTHING! All you are doing is checking if there is data available, then doing nothing, you are better off just deleting it. The only reason your program is working is because if you try to read data from serial when there is nothing, it returns a -1. And even then you are trying to store a -1 in an 8 bit unsigned variable which underflows to 255. (Arduino uses a signed byte for char...)

Now read my previous post and change it to that...