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

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

Could anyone tell me how 'C', being a subset of C++, is more versatile than its own superset 'C++'.

Ok how about this question to go with yours, why would anyone create subsets if the original is good enough. So there has to be a difference or else it wouldn't be there!

I have found Arduino is C/C++ AVR GCC

I guess I should have taken the post of topic!

@adigitaldj1

I am a scorpio and were naturally introverted

Well, I have that in common... Thank to let me know what state/Province. I hear Ohio State trouper are a "bit picky"...

About you problem in coding... C++ is a different ball game than BASIC.

I re-read this tread, It look like you want : press button, flash the light in the sequence you want, no press, no light flashing .... I am correct ? OK let assume that... Programing the Arduino, you have to know what you want to do.

Here is a concept code :

void setup()
{
  // your pin setup
}

void loop()
{

    read the switch
if swich is on = state = on
   while(state)
    {

      flash the light
    re-check the switch
if switch is on  = state on
if no on, state  = off 
}

I did a code simila to that. It my school bus light system. Just re-modify this code to fit your need.

Here the code :

/*
  size : 1692 bytes
    
  Version 2.1
  
  file name : schoolbussignal.pde
  
  School Bus Alternating Light Simulator
  
  It simulate the activation system to activate the alternating
  red light, stop sign and crossing gate of the school bus.

  Here the parts you need :
 
  4 - 330 ohms limiting resistors for the LED's
  2 - Red LED
  2 - Green KED
  3 - 10 K resistor pull-down resistor
  2 - SPDT or SPST switch or DIP switch
  1 - Push-on button switch
  
  How it work :
  
  1. To activate the system, turn on MASTER switch.
  2. Press the push button to activate the Red Lights ONLY.
  3. When the door is open, a swtich is close ( the door switch is ON )
     and activate the stop sign ( left side of the bus )
     and crossing ( front of the bus ) gate.
  4. Close the door ( turn the door off ) to turn off everythings.
  5. When open the door ( turn on/off door switch ) and 
     Master switch is OFF, nothing will happen.
  6. Even you press the push button and the Master switch is OFF, 
     nothing will happen.  
    
  Program by Serge J Desjardins aka techone
  
  Re-Modify with the help of AWOL
  
  Compile and Tested 
   
*/  

// input pin

const byte masterpin=12;
const byte doorpin=11;
const byte pushbuttonpin=10;

/*
   pin 12 : Master Switch
   pin 11 : Door Switch
   pin 10 : Push button to activate
*/

// output pin

const byte busoutpin[4]={9,8,7,6};

/*
   pin 7 : Alterning Light A
   pin 6 : Alterning Light B
   pin 5 : Front gard gate motor on/off
   pin 4 : Stop sign motor on/off
*/

byte master=0; // Master Switch
byte door=0;  // Door Switch
byte pushbutton=0; // Push Button

boolean buttonstate; // State Control
boolean doorstate;

void setup()
{ // init out & in pin
  pinMode(masterpin, INPUT);
  pinMode(doorpin, INPUT);
  pinMode(pushbutton, INPUT);
  for (int i=0;i<4;i++)
  {
    pinMode(busoutpin[i], OUTPUT);
  } 
   
  // turn the output pins off
  for (int i=0;i<4;i++)
  {
    digitalWrite(busoutpin[i], LOW);
  }  
  buttonstate=0;
  doorstate=0;
  turnoff(); 
}

void loop()
{  
  // check for the push button ON or door switch ON  
  while ((buttonstate==0) && (doorstate==0))
  {
    // check the master switch again
    master=digitalRead(masterpin);
    delay (100);
    // check push button again
    pushbutton=digitalRead(pushbuttonpin);
    delay (100);
    if ((pushbutton==1) && (master==1))
    {
      buttonstate=1;
    }
    // check the door switch again
    door=digitalRead(doorpin);
    delay (100);
    if ((door==1) && (master==1))
    {
      doorstate=1;
      buttonstate=1;
    }
    // when door is open and master switch is off
    if ((door==1) && (master==0))
    {
      doorstate=0;
      buttonstate=0;
    }   
  }
  // stay in loop when the master id ON AND the push button is press
  while ((master==1)  && (buttonstate==1))
  {
     // check the master switch again
     master=digitalRead(masterpin);
     delay (100);
     if ((master==1) && (buttonstate==1))
     {
       // push button is press AND master is ON
       alternating();
     }
    // check the door again   
     door=digitalRead(doorpin);
     delay(100);
     // the door is open with master on    
     if ((door==1) && (master==1))
     {
       doorstate=1;
       alternating();
       motorson();
       alternating();
     }
     // after the door was open and closing the door     
     if ((door==0) && (doorstate==1))
     {
       buttonstate=0;
     }  
   }
   // turn off everythings when master is off or door is close
   if ((master==0) || (door==0))
   {
    doorstate=0; 
    buttonstate=0; 
    motorsoff();
    turnoff();
   }
}   
// alternating red light routine
void alternating()
{
  digitalWrite(busoutpin[0], HIGH);
  digitalWrite(busoutpin[1], LOW);
  delay(500);
  digitalWrite(busoutpin[0], LOW);
  digitalWrite(busoutpin[1], HIGH);
  delay (500); 
}  
// turn off everythings routine
void turnoff()
{
  for (int i=0; i<4;i++)
   {
     digitalWrite(busoutpin[i], LOW);
   }
}
//deploy stop sign motor and crossing gate motor
void motorson()
{
   digitalWrite(busoutpin[2], HIGH);
   digitalWrite(busoutpin[3], HIGH);  
}
//re-track back stop sign and crossing gate 
void motorsoff()
{
   digitalWrite(busoutpin[2], LOW);
   digitalWrite(busoutpin[4], LOW);
}

Great, Thanks! Looks like some great switch logic here.

Yes if you get pulled over by a Ohio State Trooper it's a ticket no matter what!

I was able to put together some code with your help and lloyddean that works great!

Yes if you get pulled over by a Ohio State Trooper it's a ticket no matter what!

Oh, I agree with you, I hear truckers don't like Ohio. ( I did drive on I-75 in 2001 with a 10 wheeler truck, Man, did I follow the speed limit to the letter...

I was able to put together some code with your help and lloyddean that works great!

Great... Happy to hear it. And please show you code for the others members can see it and learn from it.

Is there a place to post the code like a repository etc.?

Here as this is where the discussion is.

Ok here is the code. I may not have properly put the brackets where most do but for my sanity I like them right with the code they represent. This is just for one pushbutton and one led. I am going to try and work on adding another button and led for what I will call the right side in the code. I also want to add headlight pulse and brake light flash and steady. Later after this all works good I want to add some special functions plus some rgb show lights when parked.

/*
  
  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;i<3;i++)         // flash 3 times & delay
  
    
  { digitalWrite(ledPin, HIGH);   // set the LED on
    delay(500);                   // wait
    
    digitalWrite(ledPin, LOW);    // set the LED off
    delay(500); }                 // wait
  
 
  
  delay(2000); }
    
  else 
  
  { // turn LED off:
    digitalWrite(ledPin, LOW); }
    
}

Look nice. A baby step ? Yes Code need some improvement ? Yes. Need more learning ? Yes. I hope you pla to buy a book call "Getting Started With Arduino". I have this book and it help me. Or this site at Page Not Found | Lulu It got a free pdf booklet.