I'm trying to made a program that turn on and turn off leds with an input and send the coordinates of joystick, but, in the loop, the arduino makes one at a time, and i want make this in the same time. How can i make that?

  • It isn't clear what you're asking. Can you describe what you've already tried? One processor can only do one thing at a time but it can do things in very a fast sequence, as long as the individual tasks have not (or only very short) delays. Have you tried to do this? – JRobert May 7 '15 at 11:46
  • I tried to sent an input to control the leds and send at the same time one joystick, but, the loop is something like this: loop() { controlleds(); joystickevent(); } but he runs, controlleds first, and joystickevent next, i want at the same time. – PRVS May 7 '15 at 12:07
  • My answer to question 9752 shows code to handle multiple actions independently. – James Waldby - jwpat7 May 7 '15 at 14:50

One way to do this is to use the millis() function.

millis() returns the current value of a timer that is incremented once per millisecond (hence the function name). You can compare the value of millis() at some past time with the current value to determine how many milliseconds have passed. By having multiple compare registers you can have multiple independent delays operating. (There is also micros() which increments at the 1 uS rate (with a "granularity" of 4uS on some processors).

This code is an example only. It MAY run but has not been tested and probably contains silly mistakes which do not affect its example purposes.

A function ToggleLED(n) is assumed which toggles the specified LED n from 1->0 or 0->1 as required.

LED1 will flash 200 mS on, 200 mS off, ...
LED2 will flash 333 mS on, 333 mS off, ...

The two loops are essentially independent.

unsigned long delay1; delay2;

void setup(){  

int FlashTime1 = 200; / LED 1 will have a 200 on 200 off cycle
int FlashTime2 = 333; / LED 2 will have a 333 on 333 off cycle

delay1 = millis() + FlashTime1;  / Init LED timers
delay2 = millis() + Flashtime2;  

void loop()  
  if (millis() == delay1){ / Not until delay expired. 

    delay1= millis() + FlashTime1; / Reinit flash timer}

  if (millis() == delay2){

       delay2= millis() + FlashTime2;}

| improve this answer | |

the loop is something like this:

loop() {

but he runs, controlleds first, and joystickevent next, i want at the same time.

There are typically two ways to read a simple potentiometer joystick: Connect it in a voltage divider and measure the analog voltage; and connect it in an R-C circuit and measure the time for the capacitor to charge.

The first way is faster and you should not be able to notice the time difference between controlling the LEDs and reading the joystick.

The second way takes longer and you might be able to notice. If this is how your joystick controller works, you would need to separately start the joystick reading, continue to poll the buttons control the LEDs, and check whether the joystick reading is ready yet. The joystick routine must not delay, waiting for the measurement to finish.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.