Loading...
Pages: [1] 2   Go Down
Author Topic: New to Arduino - Wanting to flash a led a few counts pause and few counts again  (Read 785 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: January 06, 2012, 10:31:24 pm by adigitaldj1 » Logged

Toronto, Canada
Offline Offline
Edison Member
*
Karma: 2
Posts: 1233
"Keep it R.E.I.L. - "Research, Experiment, Investigate and Learn"
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Quote
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.

Code:
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...
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 21
Posts: 697
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Code:
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);
}
Logged

Toronto, Canada
Offline Offline
Edison Member
*
Karma: 2
Posts: 1233
"Keep it R.E.I.L. - "Research, Experiment, Investigate and Learn"
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@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 ?   ..... ? 
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 21
Posts: 697
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good guess!

Note these declarations in "wiring.h":

Code:
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.
Logged

Toronto, Canada
Offline Offline
Edison Member
*
Karma: 2
Posts: 1233
"Keep it R.E.I.L. - "Research, Experiment, Investigate and Learn"
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Quote
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.
     
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 21
Posts: 697
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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>
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  smiley

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
« Last Edit: January 07, 2012, 01:36:24 pm by adigitaldj1 » Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 21
Posts: 697
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Tried it again and was fine!
Logged

Toronto, Canada
Offline Offline
Edison Member
*
Karma: 2
Posts: 1233
"Keep it R.E.I.L. - "Research, Experiment, Investigate and Learn"
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@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

Quote
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


Code:
/*
 
  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
« Last Edit: January 07, 2012, 05:06:29 pm by adigitaldj1 » Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 21
Posts: 697
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

adigitaldj1,

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

Code:
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:

Code:
    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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: January 07, 2012, 09:50:58 pm by adigitaldj1 » Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 21
Posts: 697
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 -

Code:
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
}
« Last Edit: January 08, 2012, 01:45:22 am by lloyddean » Logged

Pages: [1] 2   Go Up
Print
 
Jump to: