Timer

Hi Guys, First of all I am Joshua, 15 years old, I am working on Arduino.
Please help me to Create Timer on Arduino, I want to do is I send a numbers and that is the delay.
I am going to use it in Statement.

Sorry for my English, I'm from philippines

Here's is my Code:

/* THE ATOMS COMPANY /
/
---------------------------------------*/
//Remake
const int A = 13;
const int B = 9;
const int C = 12;
int Byte;

void setup() {
Serial.begin(9600);
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
Serial.println("Please Enter 4 digits Password");
Serial.print("PASSWORD: ");
char buffer[] = {' ',' ',' ',' ',' ',' ',' '};
while (!Serial.available());
Serial.readBytesUntil('\n', buffer, 7);
int incomingValue = atoi(buffer);
if (incomingValue == 1234) {
Serial.println("Valid!");
delay(400);
Serial.println("Welcome to");
Serial.println(" THE ATOMS COMPANY ");
Serial.println("Now, What Can I Do For You?");
Serial.println("If You Don't Understand Please Send @");
loop();
} else {
Serial.println("Invalid =(");
setup();
}
}
void loop() {
//Serial Communication
if (Serial.available() > 0) {
Byte = Serial.read();

// Start in Here
if (Byte == 'A') {
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
Serial.println("Green light is On");
//delay(500);
//Serial.println("What's Next Boss?");
}
if (Byte == 'B') {
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
Serial.println("Yellow light is On");
//delay(500);
//Serial.println("What's Next Boss?");
}
if (Byte == 'C') {
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
Serial.println("Red Light is On");
//delay(500);
//Serial.println("What's Next Boss?");
}
if (Byte == 'O') {
digitalWrite(C, LOW);
Serial.println("All light is Off");
digitalWrite(B, LOW);
digitalWrite(A, LOW);
//delay(500);
//Serial.println("What's Next?");
}
if (Byte == 'L') {
digitalWrite(C, HIGH);
Serial.println("All light is On");
digitalWrite(B, HIGH);
digitalWrite(A, HIGH);
//delay(500);
//Serial.println("What's Next?");
}
if (Byte == '!') {
digitalWrite(C, LOW);
digitalWrite(B, LOW);
digitalWrite(A, LOW);
Serial.println("--THE ATOMS COMPANY--");
delay(300);
Serial.println("ideas by Kim Rosario");
delay(300);
Serial.println("created by Joshua Marc");
delay(300);
Serial.println("help by Junior Ahilon");
delay(300);
Serial.println("Remake 3.0a");
delay(300);
Serial.println("Project 3");
delay(300);
Serial.println("Code: Remaked");
}
if (Byte == '@') {
digitalWrite(C, LOW);
digitalWrite(B, LOW);
digitalWrite(A, LOW);
delay(250);
Serial.println("Here's What I Can Do For You");
delay(300);
Serial.println("Send this Digits to get...");
delay(300);
Serial.println("A - Light One or Green On");
delay(300);
Serial.println("B - Light Two or Yellow On");
delay(300);
Serial.println("C - Light Three or Red On");
delay(300);
Serial.println("L - All Lights On");
delay(300);
Serial.println("O - All lights Off");
delay(300);
Serial.println("! - Credits");
delay(300);
Serial.println("# - Status");
Serial.println("Send @ For Info");
delay(300);
Serial.println("Not So Much, Right?");
delay(300);
Serial.println("Now, What Can I Do For You?");
delay(1);
}
if (Byte == '#') {
Serial.println("Satus Update!");
Serial.println("--Status--");
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
int sensorValueA = analogRead(A0);
int sensorValueB = analogRead(A1);
int sensorValueC = analogRead(A2);
float voltageA = sensorValueA * (5.0 / 1023.0);
float voltageB = sensorValueB * (5.0 / 1023.0);
float voltageC = sensorValueC * (5.0 / 1023.0);
//A
Serial.print("Volts in A are: ");
Serial.println(voltageA);
Serial.print("Raw Data in A: ");
Serial.println(sensorValueA);
// B
Serial.print("Volts in B are: ");
Serial.println(voltageB);
Serial.print("Raw Data in B: ");
Serial.println(sensorValueB);
// C
Serial.print("Volts in C are: ");
Serial.println(voltageC);
Serial.print("Raw Data in C: ");
Serial.println(sensorValueC);
// Done
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
Serial.println("The Normal Volts is 4v up, 5v is max");
delay(1);
Serial.println("I Think We Are Okay.");
}
}
}

And then We will add a timer

Where will you add a timer and what exactly will it be timing? A more detailed specification is needed.

Steve

This will not work:

   if (incomingValue == 1234) {
    Serial.println("Valid!");
    delay(400);
    Serial.println("Welcome to");
    Serial.println(" THE ATOMS COMPANY ");
    Serial.println("Now, What Can I Do For You?");
    Serial.println("If You Don't Understand Please Send @");
    loop();
   } else {
    Serial.println("Invalid =(");
    setup();
   }

This is from the setup. The setup cannot call the loop or itself, you'll get quickly memory overflow and crash problem.

Read some basic tutorials about how a Arduino code should be organized.

Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

PS ... When posting code please use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor See How to use the Forum