New here Help please

Hi All
I am new here and having VERY basic probem.
I have done blink on pin 13. Then tried to add pin 12 to blink at delay 100 but i get one long blink then one short blink over and over but am looking to flash 2 led's at the same time at different speeds. I tried to put the following. Can you please give me pointers as i don't seem to get the hang of doing two things at once.

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */

void setup() {                
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);     
}

void loop() {
  digitalWrite(13, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
  delay(1000);              // wait for a second
  digitalWrite(12, HIGH);
  delay (100);
  digitalWrite12, LOW);
  delay(100):
}

Regaeds
Bill

Hi,

I think the solution is to use the millis() function, this link will help you :

Hi Thanks rogerlette
I will have a good read Thanks for the pointer.
Regards
Bill

Hi
Sorry i might be missing something i still do not see how to control 2 led's at different speeds can you please put me a bit more on the right direction. Not looking for code just pointers so my head has to sort it. Or should i get it from this pointer?.
Regards
Bill

Think about how YOU would perform the task at hand. You have two lights, two switches, a watch and a pad of paper.

Turn a switch on, and record the time. Hum a tune, and periodically check the time. When the switch has been on long enough turn it off and record the time.

Hum a bit more, periodically checking the time. When the switch has been off long enough turn it on and record the time.

You can see that you can control more than one switch, recording different on and off times for each switch.

The millis() function takes the place of the watch. Variables in the sketch (global, usually) take the place of the pad of paper. The switches are the digital pins. The "periodically check the time" is every pass through loop.

Humming a tune? Well, strictly not necessary, and the Arduino is not particularly well suited for that, so you need to keep doing that yourself.

Thanks for reply

But Humming a tune? Well, strictly not necessary, and the Arduino is not particularly well suited for that, so you need to keep doing that yourself.
Are you saying arduino is the wrong way to go ??
Regards
Bill

Are you saying arduino is the wrong way to go ??

Do you need the LED controller to hum a tune? If so, then, yes, Arduino is probably not the right way to go. If it is not necessary for the LED controller to hum a tune, then the Arduino is perfectly suited to the task.

Here's your leg back. Sorry it came off when I pulled.

Thanks PaulS
I will keep trying then THANKS :slight_smile:
Regards
Bill

Hi Jack Christensen
You said you have a sketch could you please post it so i have something to work on. I have played with the tutorials and altered them to what i want and worked out fine. I do not seem to grasp it from start but if put in front of me i seem to be able to see what is happening and change to what i want.I have had a good look today but can not grasp what i need to do.
Regards
Bill

Think of it like this Bill:

prior to setup(), you define how long you want each event to last for

unsigned long duration1 = 4294967295UL;  // biggest # you can have, = 0xFFFFFFFF, 4 bytes long
unsigned long duration2 = 10000UL; // 10,000 milliseconds, or 10 seconds

in setup(), you capture the current time:

void setup(){
// anything = millis(); captures the amount of milliseconds since the last reset
start_time1 = millis();
start_time2 = millis();
}

then in void, you see how much time went by:

void loop(){
current_time = millis();
elapsed_time1 = current_time -start_time1;
elapsed_time2 = current_time - start_time2;

if (elapsed_time1 >= duration1){ // if enough time went by, do something
// start your action, or stop it if was started previously
if (action1_running == TRUE){
action1_running = FALSE; // was running, so stop it
}
else {
action1_running = TRUE;} // not running, so start it
}
// now check the other event, with its indepedent duration time
if (elapsed_time2 >= duration2){  // if enough time went by, do the other something
// start your action, or stop it if was started previously
if (action2_running == TRUE){
action2_running = FALSE; // was running, so stop it
}
else {
action2_running = TRUE;} // not running, so start it
}
} // end of void loop()

You need to declare all the variables used of course
all the time related events are type
unsigned long
so they're always a number from 0 to xxxx
Things like the 'flags' , action_running1 for example, can be type
byte
as they're just storing a 0 or 1 value: HIGH/LOW, 1/0, TRUE/FALSE
(got to watch the capitalizations on those too).

I think arduino would have no prblem humming a tune while turning those leds on and off with tone();
XD

Haha, winner10920, I'm sure you are correct!

Bill, my sketch is below. Note the technique of writing a different function for each task. This keeps the loop() function simple and keeps the logic for each task separate. Of course here the two tasks are essentially the same, so there is some repetitive coding. There are more elegant ways of doing it and avoiding the repetition, but that is a topic for another time. The point here is to be clear about doing "two things at once".

//blinkTwo
//Blinks two LEDs independently of each other at two different rates.

#define LED1 7            //the pin LED #1 is connected to
#define LED2 8            //the pin LED #2 is connected to
#define INTERVAL1 1000    //the number of milliseconds LED #1 turns on and off for
#define INTERVAL2 333     //the number of milliseconds LED #2 turns on and off for

unsigned long ms;         //the current millis() timer value

void setup(void) {
    pinMode(LED1, OUTPUT);
    pinMode(LED2, OUTPUT);
}

void loop(void) {
    ms = millis();
    blinkLED1();
    blinkLED2();
}

void blinkLED1(void) {
    static boolean ledState;        //current LED state (on or off)
    static unsigned long msLast;    //the last time the LED changed state
    
    if (ms - msLast >= INTERVAL1) {
        ledState = !ledState;
        digitalWrite(LED1, ledState);
        msLast = ms;
    }    
}

void blinkLED2(void) {
    static boolean ledState;        //current LED state (on or off)
    static unsigned long msLast;    //the last time the LED changed state
    
    if (ms - msLast >= INTERVAL2) {
        ledState = !ledState;
        digitalWrite(LED2, ledState);
        msLast = ms;
    }    
}

Hi I have put this on breadboard and run it but not had time to try to alter it (PLAY) but do think there will be a few questions after that looking at the code. Many thanks for the replies.
Regards
Bill