Adding optional times frames to the classic blink sketch.

Hello everyone. I'm new to the world or arduino and am playing around with the blink sketch.
I am for now only using the on board LED on the UNO. I can not get this sketch to compile. What am I doing wrong or leaving out?

I feel like it should work. Is this one of the problems caused by using delay in place of unsigned long?

Thank you in advance for any help you can provide.

This is my modified code:

/*
Blink

Turns an LED on for one second, then off for one second, repeatedly.

Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
the correct LED pin independent of which board is used.
If you want to know what pin the on-board LED is connected to on your Arduino
model, check the Technical Specs of your board at:

modified 8 May 2014
by Scott Fitzgerald
modified 2 Sep 2016
by Arturo Guadalupi
modified 8 Sep 2016
by Colby Newman

This example code is in the public domain.

*/

#define TIME_SCALE_ONE_ENABLE_PIN 2 // set this pin high to enable time scale one second on/five seconds off
#define TIME_SCALE_TWO_ENABLE_PIN 3 // Set this pin high to enable time scale five seconds on/one second off
#define TIME_SCALE_THREE_ENABLE_PIN 4 // set this pin high to enable time scale one minute on/off
#define TIME_SCALE_FOUR_ENABLE_PIN 5 // set this pin high to enable time scale one minute on and five minutes off
#define TIME_SCALE_FIVE_ENABLE_PIN 6 // set this pin high to enable time sclae five minutes on and one minute off
int val1 = 1000; // cycle time of one second on
int val2 = 5000; // cycle time of five seconds off
int val3 = 5000; // cycle time of five seconds on
int val4 = 1000; // cycle time of one second off
int val5 = 60000; // cycle time of one minute on
int val6 = 60000; // cycle time of one minute off
int val7 = 60000; // cycle time of one minute on
int val8 = 300000; // cycle time of five minutes off
int val9 = 300000; // cycle time of five minutes on
int val10 = 60000; // cycle time of one minute off

// the setup function runs once when you press reset or power the board

void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
pinMode(TIME_SCALE_ONE_ENABLE_PIN, INPUT);
pinMode(TIME_SCALE_TWO_ENABLE_PIN, INPUT);
pinMode(TIME_SCALE_THREE_ENABLE_PIN, INPUT);
pinMode(TIME_SCALE_FOUR_ENABLE_PIN, INPUT);
pinMode(TIME_SCALE_FIVE_ENABLE_PIN, INPUT);
}

// the loop function runs over and over again forever

void loop() {
if (digitalRead(2) == HIGH){
delay = val1;
delay = val2;
}
else if (digitalRead(3) == HIGH){
delay = val3;
delay = val4;
}
else if (digitalRead(4) == HIGH){
delay = val5;
delay = val6;
}
else if (digitalRead(5) == HIGH){
delay = val7;
delay = val8;
}
else if (digitalRead(6) == HIGH){
delay = val9;
delay = val10;
}
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(val1); // Adjust on time as needed. One second is equal to one thousand
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(val2); // wait for a second
}

Hey!,

It will help us help you if you show what error messages you are getting.

Your delay variable is using the name of a function (delay()). Use a different name for that variable. Brackets must match. You need a } for every {. If you use the autoformat tool (ctrl-t) you may see the mismatch. You are missing a } to close the loop() function.

warning: overflow in implicit constant conversion [-Woverflow]

      int val9 = 300000;

The int data type can hold values from -32768 to 32767. 300000 is a bit more than an int can hold. See Arduino data types.
Variables used for timing (delay, millis()) should be unsigned long data type.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

int val5 = 6000;                        //  cycle time of one minute on

That and the other inaccurate comments are not worth much.

I was clued into the fact that many of my times were simply too long for the delay function. Because of this, I just deleted a few zeros to see if it would compile. Sorry to be so loose on the forum. I'm just figuring things out.

Hi,
These all these statements for delay;

delay = val1;
delay = val2;

Need to be;

delay(val1);
delay(val2);

Tom... :slight_smile:

Or possibly ??

delay(val1 + val2);

Or use millis timing and get away from delays altogether?

delay() takes an unsigned long, not an int.

Better to use "const byte" to name pins.

You can't have two variables named "delay", especially if you want to be able to use the 'delay()' function.

/*
  Blink

  Turns an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino
  model, check the Technical Specs of your board at:
  https://www.arduino.cc/en/Main/Products

  modified 8 May 2014
  by Scott Fitzgerald
  modified 2 Sep 2016
  by Arturo Guadalupi
  modified 8 Sep 2016
  by Colby Newman

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Blink
*/

const byte  TIME_SCALE_ONE_ENABLE_PIN    = 2;  //  set this pin high to enable time scale one second on/five seconds off
const byte  TIME_SCALE_TWO_ENABLE_PIN    = 3;  //  Set this pin high to enable time scale five seconds on/one second off
const byte  TIME_SCALE_THREE_ENABLE_PIN  = 4;  //  set this pin high to enable time scale one minute on/off
const byte  TIME_SCALE_FOUR_ENABLE_PIN   = 5;  //  set this pin high to enable time scale one minute on and five minutes off
const byte  TIME_SCALE_FIVE_ENABLE_PIN   = 6;  //  set this pin high to enable time sclae five minutes on and one minute off
const unsigned long  onTime1 = 1000;                         //  cycle time of one second on
const unsigned long  offTime1 = 5000;                         //  cycle time of five seconds off
const unsigned long  onTime2 = 5000;                         //  cycle time of five seconds on
const unsigned long  offTime2 = 1000;                         //  cycle time of one second off
const unsigned long  onTime3 = 60000;                        //  cycle time of one minute on
const unsigned long  offTime3 = 60000;                        //  cycle time of one minute off
const unsigned long  onTime4 = 60000;                        //  cycle time of one minute on
const unsigned long  offTime4 = 300000;                       //  cycle time of five minutes off
const unsigned long  onTime5 = 300000;                       //  cycle time of five minutes on
const unsigned long  offTime5 = 60000;                       //  cycle time of one minute off

// the setup function runs once when you press reset or power the board

void setup()
{
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(TIME_SCALE_ONE_ENABLE_PIN, INPUT);
  pinMode(TIME_SCALE_TWO_ENABLE_PIN, INPUT);
  pinMode(TIME_SCALE_THREE_ENABLE_PIN, INPUT);
  pinMode(TIME_SCALE_FOUR_ENABLE_PIN, INPUT);
  pinMode(TIME_SCALE_FIVE_ENABLE_PIN, INPUT);
}

// the loop function runs over and over again forever

void loop()
{
  // default to 500/500 if no pin is HIGH
  unsigned long onTime = 500;
  unsigned long offTime = 500;

  if (digitalRead(TIME_SCALE_ONE_ENABLE_PIN) == HIGH)
  {
    onTime = onTime1;
    offTime = offTime1;
  }
  else if (digitalRead(TIME_SCALE_TWO_ENABLE_PIN) == HIGH)
  {
    onTime = onTime2;
    offTime = offTime2;
  }
  else if (digitalRead(TIME_SCALE_THREE_ENABLE_PIN) == HIGH)
  {
    onTime = onTime3;
    offTime = offTime3;
  }
  else if (digitalRead(TIME_SCALE_FOUR_ENABLE_PIN) == HIGH)
  {
    onTime = onTime4;
    offTime = offTime4;
  }
  else if (digitalRead(TIME_SCALE_FIVE_ENABLE_PIN) == HIGH)
  {
    onTime = onTime5;
    offTime = offTime5;
  }

  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(onTime);                       // Adjust on time as needed. One second is equal to one thousand
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(offTime);                       // wait for a second
}

As a general
Point ....Better too to do this in small stages, add a couple of times and get that working - easier to de bug short pieces of code .