New to Arduino - Wanting to flash a led a few counts pause and few counts again

Go easy I'm new to the forum and the Arduino soI may have a lot of questions along the way.

I want to flash a led a number of times then pause then flash a few times again. Couldn't find a command to work with to do this, just need help!

Thanks,
Kevin

First. Welcome to the forum.

Second. In your IDE, select HELP and select REFERENCE . You will get a lots of info about the coding the Ardiuno and check http://www.ardiuno.cc for examples and how to connect it properly. Search on the net and Good luck.

I want to flash a led a number of times then pause then flash a few times again. Couldn't find a command to work with to do this, just need help!

You know about blink, right ? Well learn about for() function like :

This code is your answer of your question.

I use for() , turn on and off for 10 time, wait 2 seconds and doit again... The number in delay is in miliseconds.

for ( int i=0;0<10;i++)
{
   digitalWrite(13 HIGH);
   delay(250);
   digitalWrite(13 LOW);
   delay(250);
}

delay(2000);

for ( int i=0;0<10;i++)
{
   digitalWrite(13 HIGH);
   delay(250);
   digitalWrite(13 LOW);
   delay(250);
}

delay(2000);

Just an example code...

Pretty much the same thing, perhaps a little more modular and a few less magic numbers ...

const uint8_t       pinLED          = 13;
const uint8_t       LED_OFF         = LOW;
const uint8_t       LED_ON          = HIGH;

const unsigned long HALF_SECOND     = 500UL;
const unsigned long ONE_SECOND      = 1000UL;
const unsigned long TWO_SECONDS     = (2 * ONE_SECOND);

setLED(uint8_t state)
{
    digitalWrite(pinLED, state);
}

void blink(int count) 
{
    while ( count-- )
    {
        setLED(LED_ON);
        delay(HALF_SECOND);

        setLED(LED_OFF);
        delay(HALF_SECOND);
    }
}

void loop()
{
    blink(10);
    delay(TWO_SECONDS);

    // 'loop' will be recalled repeatedly upon exit
}

void setup()
{
    pinMode(pinLED, OUTPUT);
}

@lloyddean

That is a new way...

Just one question : const uint8_t pinLED = 13;

I understand : const = constant pinLED a variable , = 13 mean number 13, but what ? ---> uint8_t <---- ?

integer data 8 ? ..... ?

Good guess!

Note these declarations in "wiring.h":

void pinMode(uint8_t pin, uint8_t mode);
void digitalWrite(uint8_t pin, uint8_t val);
int digitalRead(uint8_t pin);

'digitalWrite' has two parameters both of which are expected to be an unsigned 8 bit value.

I bet the Op will have a hard time to understand...

unsigned 8 bit value.

Mean 0000 0000 to 1111 1111 or 0 to 255 or 0x00 to 0xFF , Right.

so const uint8_t pinLED = 13; It mean the contain of uint8_t = 0000 1101 = pinLED = 13 Got it !

@adigitaldj1

a function is : void blink( int count ) <--- a function and it looking for a variable name count , an integer from blink(10); < -- call the function. The 10 is the integer being use inside the blink function.

the while(count--) is when it is true ( 1 or ON or HIGH ) , do the loop. when the while(count--) is zero , 0, LOW, OFF, get out of the loop. count-- is count=count-1 In my code, i++ mean i=i+1.

Probably but having both versions can be educational. My personal opinion is that the version I posted takes a little more writing but is easier to read.

You may be interested in the various data types and the range of values that they can hold.

http://www.nongnu.org/avr-libc/user-manual/group__avr__stdint.html

Your right! A little above me on the 2nd code example. Thanks for the help guys!

Can I use 1/2 of the code example on first post if I am using loop?

I will need to see if I can put the code in without any problems because I am using void loop to detect a button press and activate the led. This is a project I am doing for a controller for my motorcycle turn signals, head light flasher, break light and maybe some custom show led lighting. I know the bike has a flasher but I am incorporating some safety ideas I have which may not be legal lighting but I think that will catch the attention of at least motorists behind me. I can always have a switch to normal operation if I get questioned by a vehicle inspector or police. :slight_smile:

I will definitely study the 2nd code example! I used to do basic in the old days and some visual basic. Hopefully this won't be to bad to catch onto.

I couldn't get the above link to work, would like to see what is there!

Thanks,
Kevin

Quote - "I couldn't get the above link to work, would like to see what is there!"

That could be because it's a URL not a link. Copy it and paste it into the your URL bar of your browser.

Sorry that is what I meant, I copied it and didn't work.

Tried it again and was fine!

@lloyddean

To put a URL press the "earth" icon, insert the link or type ---> [ url ] my link [ / url ] <-- just delete the space, so the reader of your post can just click the link.

@adgitaldj1

I will need to see if I can put the code in without any problems because I am using void loop to detect a button press and activate the led. This is a project I am doing for a controller for my motorcycle turn signals, head light flasher, break light and maybe some custom show led lighting. I know the bike has a flasher but I am incorporating some safety ideas I have which may not be legal lighting but I think that will catch the attention of at least motorists behind me. I can always have a switch to normal operation if I get questioned by a vehicle inspector or police.

For this project, your harware has to be propely wire, use of power transistor / MOSFET because of the high current of the light ( automotive light bulbs ) or automotive Led's array ( I say those in trucks, public buses - the brake light , back flasher ) and the arduino use 5 V , and automotive / motocycle use 12 V and some models 6 V. Be award of that.

Just one question ? Can you tell me where are you from ? You may check the MTO site ( Ministry of Transportation ) of your state / province ( USA / Canada ) or the country MTO site to check out the rules and regulation.

I live in Columbus Ohio.

Yes thanks I am aware of the electrical aspects of the project. I have worked in electronics since I was a kid. I work as a Biomedical Engineer now for a hospital and work on hospital and research equipment. My specialty is EEG, EMG, LTM and Microscopy. When I took electronics in the 70's and early 80's is when digital became big. I used to play with Commodore 64's etc. back then in the mid 80's.

I built my own Jukebox a few years ago and all of the lighting. I have a website even though not recently updated but you see a couple of projects I have done. http://www.freewebs.com/artdecojukebox/

The microprocessors are new to me, I started with the Parallax SX28 and when I was learning all the basics they quit selling it. I then thought about the pic and decided on the Arduino because of open source and support. I can also use the ide in Linux, I hate Windows!

I like wood working, worked as a machinist for a while, big music nut(had a DJ Business for 25 years) , electronics and love motorcycles. When I put some of that together I create some cool projects.

OK enough about me, I usually didn't say much about myself to others probably because I am a scorpio and were naturally introverted.

Here is what I have but it's not working, not sure if I am to use Void Loop and For together. I thought they were both somewhat loops with exception the for is a count.

/*
  
  Button Turns on and off a light emitting diode(LED) connected to digital  
   pin 13, when pressing a pushbutton attached to pin 2. 
 
 
 CIRCUIT:
 * LED attached from pin 13 to ground(on board led) 
 * pushbutton attached to pin 2 from ground
 * 10K resistor attached to pin 2 from positive
 
 */

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPinLF = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPinLF, INPUT);     
}

  void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPinLF);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is Low:
  
  if (buttonState == LOW) {     
    // turn LED on:
   
    for (int i=0;0<10;i++);
    
    digitalWrite(ledPin, HIGH);   // set the LED on
    delay(500);                   // wait for a second
    digitalWrite(ledPin, LOW);    // set the LED off
    delay(500);                   // wait for a second
  
  delay(2000);
  
    digitalWrite(ledPin, HIGH);   // set the LED on
    delay(500);                   // wait for a second
    digitalWrite(ledPin, LOW);    // set the LED off
    delay(500);                   // wait for a second
    
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }
}

Thanks,
Kevin

adigitaldj1,

Something you're NOT seeing is behind the scene (provided by the Wiring framework) driving your code. And that is:

int main(void)
{
    init();
    
    setup();
    for ( ; ; )
    {
        loop();
    }
}

As is normal for C++ programs the board turns over control to the function 'main' which is provided (as above), by the Wiring framework, leaving YOU to provide both the 'setup' and 'loop' functions. 'setup' give you the opportunity to initialize your part of the software and 'loop' will be called repeatedly till either a reset occurs or power is turn off.

As you can see the code you provide in 'loop' will be immediately run again upon returning from 'loop'.

Your 'loop' functions contains a 'for' loop that does 10 x nothing:

    for (int i = 0; 0 < 10; i++)
    {
        ;
    }

turns the LED 'ON' for half a second, turns the LED 'OFF', waits for two seconds, turns the LED 'ON' for half a second, turns the LED 'OFF' and exits 'loop' which is then immediately reentered.

Is that what you wanted to happen.

I want to set it for maybe 3 flashes pause then 3 again, or set it for any value I want.

I am a little confused, not sure what your saying.

I liked the old days when maybe it was as simple as setting up a variable having it count up or down either compare it to another number then within if/then check it and do something. I guess I will be fine if I don't compare it to old Commodore Basic or Visual Basic programming.

Can you maybe shed a little more light on what I am missing? I am understanding a little more of your first example from above then the one I am trying after looking over it more.

Would it help me to understand what is going on if there is a web page that would explain more of Arduino and C++.

Thanks,
Kevin

You said blink an LED a few time wait some an do it again ..., so I removed your button references modified your 'for' loop code leaving -

const int ledPin = 13;

void setup()
{
    pinMode(ledPin, OUTPUT);      
}

void loop()
{
    for ( int i = 0; 0 < 3 ; i++ )  // loop thrice
    {       
        digitalWrite(ledPin, HIGH); // set the LED on
        delay(1000);                // wait for a second <- one second = 1000 milliseconds

        digitalWrite(ledPin, LOW);  // set the LED off
        delay(1000);                // wait for a second <- one second = 1000 milliseconds
    }

    delay(2000);                    // wait for two second <- two second = 2000 milliseconds
    
    // we're leaving 'loop' and will return to 'main' as told of above
}

Great, Thanks!

I did want the button portion but what I did was look over your code and my code. I made modifications to mine and also found that I was not using the brackets{ } correctly. I read that this is one of the things beginners have a hard time with so I don't feel to bad. C++ is really different from basic!

I did have to take the for ( int=0; 0<3; i++ ) and change it to ( int=0; i<3; i++ ) to get the counts to work in your code and mine. I have a better understanding of how this all works and will look at your other example more.

Is C++ the best way to go with Arduino or is C?

Thanks,
Kevin

Is C++ the best way to go with Arduino or is C?

Absolutely!

@Paul,

Which one?

Sorry didn't know which one you were referring to.

Thanks,
Kevin

C++ is a superset of C. The Arduino is programmed in C++. But, you are not required to use any of the superset of C that is C++, if you don't want to. So, see, the question was kind of silly, like my answer.

From my understanding from a Nuts & Volt magazine article C is much more versatile and not as limited as C++ for the Arduino.

There are no silly questions, your just silly if you don't ask! :slight_smile: