Concurrent

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Concurrent

Postby camacho » Tue Feb 14, 2012 11:07 am

Hello,

So I have a series of 6 Light Boxes. I'm using 5V - Solid State Relays and Arduino Mega to control 15 Amps worth of Bulbs. And of course, some boxes have NO rhythm or pattern. And while I can program for that, I'm having problems with being able to run each box AT THE SAME TIME. I created functions I labeled BOX1() or even BOX2BLUE() and BOX2PURPLE() for really intricate ON/OFF states, but they run sequentially. How can I call upon all functions to run at the same time?

Any help would be great. I have a feeling it's easy, I just can't find the solution. Is it somewhere in millis()?

Thank,
Michael
camacho
 
Posts: 3
Joined: Tue Feb 14, 2012 10:57 am

Re: Concurrent

Postby philba » Tue Feb 14, 2012 12:38 pm

The simplest approach is not use delay() but rather millis(). Create a delay list. This involves setting up the timing information to be relative to the start time. When you start, you figure out when the next event has to happen and look for that in the loop. If the time hasn't been reached, do nothing. A simple example:
Code: Select all
long offTime, onTime;
void setup(){
   onTime = millis();
   offTime = millis()+1000;
}

loop(){
   if(millis() > offTime) {
      offEvent();
      offTime = millis()+2000;
   }
   if(millis() > onTime){
      onEvent();
      onTime = millis()+2000;
   }
}

This gives a 2 second cycle with one second on and one second off. It will fail after 50 or so days of uptime, though.

A better solution is to use interrupts so you can do other things in your loop rather than fiddling with times
philba
 
Posts: 387
Joined: Mon Dec 19, 2011 5:59 pm

Re: Concurrent

Postby adafruit_support_bill » Tue Feb 14, 2012 4:10 pm

If you check over at the Arduino Playground site, there are some timer libraries that might be helpful to you.
User avatar
adafruit_support_bill
 
Posts: 16633
Joined: Sat Feb 07, 2009 9:11 am

LED sequencing and timing

Postby camacho » Thu Feb 16, 2012 11:56 am

So I used the millis() and arrays to create a list of ON/OFF times. But I want to reset the states of led1State and led2State to their respective initial state (ON for led1State and OFF for led2State) because both LED sequences end in the OFF state.

Code: Select all
int led1Delays[40] = {2000,2000,2000,2000,2000,2000,2000,2000,6000,4000};
int led1Count = 10;
int led1Index = 0;
bool led1State = 1;    // ledState used to set the LED
long led1PreviousMillis = 0;  // will store last time LED was updated

int led2Delays[40] = {2000,2000,2000,2000,2000,2000,2000,2000,8000,2000};
int led2Count = 10;
int led2Index = 0;
bool led2State = 0; // ledState used to set the LED
long led2PreviousMillis = 0;  // will store last time LED was updated

void loop() {
  if(currentMillis - led1PreviousMillis > led1Delays[led1Index])
   {
    led1PreviousMillis = currentMillis;   
    led1State = ! led1State;
    digitalWrite(pinArray[0], led1State);
    led1Index = (led1Index+1) % led1Count;
  }


   if(currentMillis - led2PreviousMillis > led2Delays[led1Index])
    {
    led2PreviousMillis = currentMillis;   
    led2State = ! led2State;
    digitalWrite(pinArray[1], led2State);
    led2Index = (led2Index+1) % led2Count;
    box1CycleCount = box1CycleCount + 1;
   }
   if (box1CycleCount == 10)
   {
   led1State = 1;
   led2State = 0;
}


Do I count the number of iterations and then when count == 10 set led1State and led2State to their initial states??? My current sketch doesn't seem to do this properly. What am I missing?
camacho
 
Posts: 3
Joined: Tue Feb 14, 2012 10:57 am

Re: Concurrent

Postby philba » Thu Feb 16, 2012 7:48 pm

I don't see currentMillis defined. If it's a variable, you need to set it to something. Did you mean to use millis(), prehaps?
philba
 
Posts: 387
Joined: Mon Dec 19, 2011 5:59 pm


Return to Arduino

Who is online

Users browsing this forum: ArunShanbhag, mibignistinly, xEDWARDSx and 12 guests

Stuff to buy from the Adafruit store and links to product documentation!


New Products [114]

Raspberry Pi[82]
 
FLORA[24]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[12]
Arduino[60]
 
NETduino[14]
 
BeagleBone[23]
 
Android[6]
 
XBee[10]
More Dev Boards[30]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[39]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[9]


 
Breakout Boards[35]
LCDs & Displays[49]
Components & Parts[70]
Batteries & Power[54]
EL Wire/Tape/Panel[52]
LEDs[112]
 
Wireless[16]
Cables[66]
 
Lasers[6]
Sensors/Parts[147]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[70]
 
iDevices[13]
Tools[71]
 
Wearables[41]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[25]


 
Stickers[41]
 
Skill badges[55]
 
Books[26]
 
Circuit Playground[7]
 
Gift Certificates[4]