Go Down

Topic: New to Arduino - Wanting to flash a led a few counts pause and few counts again (Read 1 time) previous topic - next topic

adigitaldj1

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

PaulS

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

Absolutely!

adigitaldj1

@Paul,

Which one?

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

Thanks,
Kevin

PaulS

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.

adigitaldj1

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!  :)

lloyddean

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

adigitaldj1

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!

Techone

@adigitaldj1

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

Code: [Select]


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 :

Code: [Select]


/*
  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);




adigitaldj1

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!

Techone

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

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


lloyddean


adigitaldj1

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.

Code: [Select]
/*
 
 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); }
   
}

Techone

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 http://www.lulu.com/content/1108699a It got a free pdf booklet.

Go Up