Re: Blink Without Delay - with extra button HOW DO THEY DO IT

Can anyone suggest a development of the "Blink Without Delay" sketch to include an additional function, the purpose of which is to demonstrate uninterrupted blinking, while the additional function operates? May I suggest two LEDs, one of which blinks, the second can be made to light (HIGH) by pressing a button? I am reading your original posting by means of Internet browser translation.

Like so?

/* Blink without Delay

 Turns on and off a light emitting diode (LED) connected to a digital

 pin, without using the delay() function. This means that other code

 can run at the same time without being interrupted by the LED code.

 The circuit:

 * LED attached from pin 13 to ground.

 * Note: on most Arduinos, there is already an LED on the board

 that's attached to pin 13, so no hardware is needed for this example.

 created 2005

 by David A. Mellis

 modified 8 Feb 2010

 by Paul Stoffregen

 modified 11 Nov 2013

 by Scott Fitzgerald
modified 20 Apr 2017 to show button press for 2nd LED using LED on 12 and button on 2

 This example code is in the public domain.

 [iurl=http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay]http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay[/iurl]

 */

 // constants won't change. Used here to set a pin number :

 const byte ledPin = 13; // the number of the LED pin

 // Variables will change :

 byte ledState = LOW; // ledState used to set the LED

 // Generally, you should use "unsigned long" for variables that hold time

 // The value will quickly become too large for an int to store

 unsigned long previousMillis = 0; // will store last time LED was updated

 // constants won't change :

 const long interval = 1000; // interval at which to blink (milliseconds)


 // added for new functionality 20 Apr 2017

 byte led2 = 12;

 byte button2 = 11;

 void setup() {

 // set the digital pin as output:

 pinMode(ledPin, OUTPUT);

 

// added for new functionality

 pinMode (led2, OUTPUT); // pin to LED anode, LED cathode to resistor, resistor to Gnd

 pinMode (button2, INPUT_PULLUP); // button connects pin to Gnd when pressed

 } // end setup

 void loop()

 {

 // here is where you'd put code that needs to be running all the time.

 // check to see if it's time to blink the LED; that is, if the

 // difference between the current time and last time you blinked

 // the LED is bigger than the interval at which you want to

 // blink the LED.

 unsigned long currentMillis = millis();

  if(currentMillis - previousMillis >= interval) {

  // save the last time you blinked the LED 

 previousMillis = currentMillis; 

 // if the LED is off turn it on and vice-versa:

 if (ledState == LOW){

 ledState = HIGH;
}

 else {

 ledState = LOW;
}

 // set the LED with the ledState of the variable:

 digitalWrite(ledPin, ledState);
} // end time check

 // added for new functionality

 if (digitalRead(button2) == LOW) {

 dgitalWrite (led2, HIGH);

 }

 else {

 digitalWrite (led2, LOW);

 }

 } // end loop

brianeo:
Can anyone suggest ...

Hi.

I don't know how well my post in the Dutch language was translated, but..

If you have to ask that question, and someone helps you by throwing you the code, the both of you didn't understand why i posted that message.
What you were asking, was exactly the point of my message and also the point of the blink without delay sketch as i see it.
There's a lot of English posts conveying the same message:

You're supposed to take the examples you got with your IDE, and study them.
Make small changes and see whether they do what you expected them to do, and then find out why not.
By finding out why it didn't work and correcting it, you're learning a great deal all by yourself.
But again, take small steps.
Blink without delay is no different from the other examples.

I'll tell you what i did during my first weekend playing with Arduino.
Previously (like 25 years before i started with Arduino) i had some experience copying Basic code from magazines (which was normal in the eighties), and that's all.

I took a peek at the examples before i received my Arduino, and soon found blink without delay.
The Arduino nano (clone) had it's on board LED blinking already when i first plugged it in.
So i added another LED with accompanying resistor of course.
I made them blink in different rhythms, took me a little while to do that.
Added another LED, and soon after that i added a potentiometer (that's another example you will find).
The 2nd day i had 6 LEDs, somewhat arranged in a circle and made the LEDs light up in clockwise and counter-clockwise.
And the 3rd day i had them do the same thing, but then not jumping from one to the next LED, but had each LED fade in and out (yet another example), in a rotation speed and direction controlled by the potentiometer.
Had a lot of fun (and not much sleep) that weekend.
And learned a great deal i wouldn't have learned when someone gave me the code to copy.

Anyone should have a look at the examples, and take plenty of time to play with them.
I handed you some ideas here, and CrossRoads showed you how you can expand your code.
CrossRoads does a great job explaining about Arduino and alike (he and his wife wrote a great book, introducing Arduino "to dummies").
Have a look at the examples and after studying, see if you can create another new idea by combining some of them.
You already have all you need for that once you have installed the IDE, all you need to do is to invest some time (really, take your time) in it.

Thank you, MAS3. Your original question is interesting. It is difficult to correspond by translation, but that is another interesting problem. I do not wish to "hijack" your thread, but may I please respond to the suggested sketch code modifications offered by Brattain?
Thank you, Brattain. I tried your sketch with changes. These are the error messages (please read additional comments, following):

Compiling sketch...
"F:\arduino-1.6.13\portable\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.3-arduino2/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10613 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR   "-IF:\arduino-1.6.13\portable\packages\arduino\hardware\avr\1.6.17\cores\arduino" "-IF:\arduino-1.6.13\portable\packages\arduino\hardware\avr\1.6.17\variants\standard" "C:\Users\brianh\AppData\Local\Temp\arduino_build_213613\sketch\sketch_apr21a_Brattain_mods.ino.cpp" -o "C:\Users\brianh\AppData\Local\Temp\arduino_build_213613\sketch\sketch_apr21a_Brattain_mods.ino.cpp.o"
sketch_apr21a_Brattain_mods:20: error: expected constructor, destructor, or type conversion before 'byte'

  byte led2 = 12;

  ^

F:\arduino-1.6.13\portable\sketchbook\sketch_apr21a_Brattain_mods\sketch_apr21a_Brattain_mods.ino: In function 'void setup()':

sketch_apr21a_Brattain_mods:30: error: 'led2' was not declared in this scope

  pinMode (led2, OUTPUT); // pin to LED anode, LED cathode to resistor, resistor to Gnd

           ^

F:\arduino-1.6.13\portable\sketchbook\sketch_apr21a_Brattain_mods\sketch_apr21a_Brattain_mods.ino: In function 'void loop()':

sketch_apr21a_Brattain_mods:79: error: 'led2' was not declared in this scope

  dgitalWrite (led2, HIGH);

               ^

sketch_apr21a_Brattain_mods:79: error: 'dgitalWrite' was not declared in this scope

  dgitalWrite (led2, HIGH);

                         ^

sketch_apr21a_Brattain_mods:85: error: 'led2' was not declared in this scope

  digitalWrite (led2, LOW);

                ^

exit status 1
expected constructor, destructor, or type conversion before 'byte'

The .ino file I am working with is attached for upload. Thanks to all readers for your patience. I have line numbering turned-on in my code window, if you care to offer corrections by line.
Beast Regards,
Brian

sketch_apr21a_Brattain_mods.ino (1.73 KB)

I do not wish to "hijack" your thread

And yet you did.

but may I please respond to the suggested sketch code modifications offered by Brattain?
Thank you, Brattain.

{facepalm}

The demo Several Things at a Time is an extended example of BWoD and includes a button and 3 LEDs. It may help with understanding the technique.

...R

Thanks for response. I always have to get my own mental confusion out of the way before I can think about anything. It's like a sneeze. Excuse me! Anyway, if you are not grossed out by that, I searched past forum posts and found what I was looking for, including your reference. On the Arduino tutorials page itself, blink-without-delay is example #2, right after blink with delay. It is an important concept. Before moving on, let me get some more stupidity out of the way. A single core processor can only do one thing at a time, thus the need for a clock. Using the clock function, it's not a matter of either/or, but of when.

brianeo:
A single core processor can only do one thing at a time, thus the need for a clock.

Every processor uses a clock - it is the fundamental thing that makes them work. The clock on an Uno works at 16MHz. The clock on a PC works at 1.6GHz or faster.

...R

Hi.

What stupidity do you see in analysing processes and challenges ?

A single core processor can only do one thing at a time, thus the need for a clock.

I don't necessarily agree with this, how did you jump to that conclusion ?
Didn't the other sketches use some kind of timing ?
And does that mean a multi core processor doesn't need a clock ?

Using the clock function, it's not a matter of either/or, but of when.

That's true, to an extend.
Because it'll still be either / or, but you won't have to wait too much for other processes to complete, because you'll be skipping the waiting (or rather wasting).

The example isn't just about timing.
It's also (and for a great deal) about keeping track of what you are, and have been doing.

As to your earlier reply:
I spend a lot of time carefully composing my posts.
Most of my posts won't tell you to do something to solve some problem, but they will help you solve your problem yourself.
Always expect multiple layers in my posts.
That means you'll probably have to re-read (parts of) my posts.
I expect anyone to be meticulous, and i'll do my best to do that too.
More important: the IDE won't forgive you if you're not meticulous.
So read answers to your questions multiple times to be sure you see all of the intended content.
And double check your own posts before actually posting them.
Brattain (click !) hasn't been around for a while, so i'm sure it wasn't him answering your questions (well, i'd say its a bit spooky if he would).

The original post in Dutch was made in Dutch because not every one is equally adept in the English language, hence the other sections of this forum.
Anybody is welcome to ask questions or ask for help.
But if that question isn't in Dutch, do not put it in the Dutch section.
That's why it was split from the original thread and moved here.

I am aware you're new to this forum.
So don't take these comments as condescending or be offended by them.
It's an other chance to learn.