Arduino program problem

Hello,

I wrote this code (See below) that if I put an a in the serial a buzzer turns on for 0,1 sec off for 0,1 and repeats one more time after which a relay turns off for 1 hour.

Normally I would just put a relay but I’m planning on adding more code later that will run just before the relay turns off so I couldn’t use a delay. Instead I just count the milliseconds and time it, but the led won’t blink and I coudn’d figure it out why (Newbie) so I was hoping you could help.

Also sorry for the vague title I didn’t know a good way to describe the problem.

int incomingByte = 0;
int standby = 1;
const int beepin = 13;
const int relay = 12;
unsigned long previousMillis = 0;

void setup() {
  Serial.begin(9600);
  digitalWrite(beepin, HIGH);
  pinMode(beepin, OUTPUT);
  pinMode(relay, OUTPUT);
}

void loop() {


  if (Serial.available() > 0) {
    incomingByte = Serial.read();

    if (incomingByte == 97 || standby == 1 ) { //if serial input = a
      standby = 1; //sets a kinda reminder that there was an "a" recieved
      unsigned long currentMillis = millis(); 
      if (currentMillis >= 0 && currentMillis < 100) { //first 100 sec set pin 13 high
        digitalWrite(beepin, HIGH);
      };
      if (currentMillis >= 100 && currentMillis < 200) { //200 sec set pin 13 low
        digitalWrite(beepin, LOW);
      };
      if (currentMillis >= 200 && currentMillis < 300) { //300 sec set it high again
        digitalWrite(beepin, HIGH);
      };
      if (currentMillis >= 300 && currentMillis < 400) { //last 400 sec set it low
        digitalWrite(beepin, LOW);
        currentMillis = 0; //set timer to 0 for next time
        standby = 0; //turns of the reminder
        digitalWrite(relay, LOW); //turn off relay
        delay(3600000); //wait 1 hour
        digitalWrite(relay, HIGH); //turn relay back on
      };
    };
  };
};

All of your checks for the time are inside the if(Serial.available()) block. So they only get checked if there is new serial data available.

Make yourself another boolean variable that you can set to true in the serial part and test it to see if you need to run that timing code.

Thank you, I changed the code as you said

int incomingByte = 0;
int standby = 0;
int serial = 0;
const int beepin = 13;
const int relay = 12;
unsigned long previousMillis = 0;

void setup() {
  Serial.begin(9600);
  digitalWrite(beepin, HIGH);
  pinMode(beepin, OUTPUT);
  pinMode(relay, OUTPUT);
}

void loop() {


  if (Serial.available() > 0 || serial > 0) {
    incomingByte = Serial.read();
    serial = 1;

    if (incomingByte == 97 || standby == 1 ) { //if serial input = a
      standby = 1; //sets a kinda reminder that there was an "a" recieved
      unsigned long currentMillis = millis();
      if (currentMillis >= 0 && currentMillis < 100) { //first 100 sec set pin 13 high
        digitalWrite(beepin, HIGH);
      };
      if (currentMillis >= 100 && currentMillis < 200) { //200 sec set pin 13 low
        digitalWrite(beepin, LOW);
      };
      if (currentMillis >= 200 && currentMillis < 300) { //300 sec set it high again
        digitalWrite(beepin, HIGH);
      };
      if (currentMillis >= 300 && currentMillis < 400) { //last 400 sec set it low
        digitalWrite(beepin, LOW);
        currentMillis = 0; //set timer to 0 for next time
        standby = 0; //turns of the reminder
        digitalWrite(relay, LOW); //turn off relay
        delay(3600000); //wait 1 hour
        digitalWrite(relay, HIGH); //turn relay back on
      };
    };
  };
};

But it still doesn’t work (any more ideas?)

Thanks anyway! :slight_smile:

You still have the timing code inside the block that checks for serial data available.

Yes, but I thought that by adding:

  if (Serial.available() > 0 || serial > 0) {
    incomingByte = Serial.read();
    serial = 1;

It would set serial to 1 whenever a serial byte was recieved after which when the code ran again, it would see that serial is high and enter the if block since it says:

if (Serial.available() > 0 || serial > 0)

But that didn't work an if I remove the whole if (Serial.available) block and just leave it with

 if (incomingByte == 97 || standby == 1 ) { //if serial input = a
      standby = 1; //sets a kinda reminder that there was an "a" recieved

as start it still doesn't work? I'm probably missing something :P

Nope. Don't change those lines. You don't want to try to read serial if nothing is available. Move the timing code out of that block.

millis()

Description

Returns the number of milliseconds since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days.

currentMillis always equals millis(). Pretty sure it will usually be > 400.

You never do anything with previousMillis

Oh yeah, definitely need to keep up with the time you started and subtract. But it still shouldn't be buried in the serial code.

OP, go look at the Blink Without Delay example and see how to properly use millis.

Thank to both of you, I forgot that millis counts from the beginning. ( I thought that you could just start counting when you want )

But al works perfectly now (I put the code below if anybody was wonder)

int incomingByte = 0;
int standby1 = 0;
int enter = 0;
int Millis0 = 0;
int Millis1 = 0;
int Millis2 = 0;
int Millis3 = 0;
const int beepin = 13;
const int relay = 12;
unsigned long previousMillis = 0;

void setup() {
  Serial.begin(9600);
  digitalWrite(beepin, HIGH);
  pinMode(beepin, OUTPUT);
  pinMode(relay, OUTPUT);
}

void loop() {


  if (Serial.available() > 0 || enter == 1 ) {
    enter = 1;
    incomingByte = Serial.read();

     if (incomingByte == 97 || standby1 == 1 ) { 
        standby1 = 1;
         if (Millis0 == 0) {
          Millis1 = millis();
          Millis0 = 1; };
          Millis2 = millis();
          Millis3 = Millis2 - Millis1;
            Serial.print(Millis3);
            
            if (Millis3 >= 0 && Millis3 < 100) {
              digitalWrite(beepin, LOW); };
          
              if (Millis3 >= 100 && Millis3 < 200) {
                digitalWrite(beepin, HIGH); };
        
                if (Millis3 >= 200 && Millis3 < 300) {
                  digitalWrite(beepin, LOW); };
              
                  if (Millis3 >= 300 && Millis3 < 400) {
                    digitalWrite(beepin, HIGH);
                    
                    Millis0 = 0;
                    Millis1 = 0;
                    Millis2 = 0;
                    Millis3 = 0;
                    standby1 = 0;
                    enter = 0;
                    
                      digitalWrite(beepin, LOW);
                      delay(3600000);
                      digitalWrite(beepin, HIGH); 
                      };
                  };
             };
         };