Go Down

Topic: Sweeping Indicator  (Read 1 time) previous topic - next topic

Madmonkeyrtu

Hi all,

I am looking at creating a sweeping indicator with the arduino mega controlling it. I have 15 LED's either side, looking for functionality of left indicator, right indicator and hazards. So three inputs.

I have started with

Code: [Select]

class Flasher
{
  // Class Member Variables
  // These are initialized at startup
  int ledPin;      // the number of the LED pin
  long OnTime;     // milliseconds of on-time
  long OffTime;    // milliseconds of off-time

  // These maintain the current state
  int ledState;                 // ledState used to set the LED
  unsigned long previousMillis;   // will store last time LED was updated

  // Constructor - creates a Flasher
  // and initializes the member variables and state
  public:
  Flasher(int pin, long on, long off)
  {
  ledPin = pin;
  pinMode(ledPin, OUTPUT);     
   
  OnTime = on;
  OffTime = off;
 
  ledState = LOW;
  previousMillis = 0;
  }

  void Update()
  {
    // check to see if it's time to change the state of the LED
    unsigned long currentMillis = millis();
     
    if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
    {
      ledState = LOW;  // Turn it off
      previousMillis = currentMillis;  // Remember the time
      digitalWrite(ledPin, ledState);  // Update the actual LED
    }
    else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
    {
      ledState = HIGH;  // turn it on
      previousMillis = currentMillis;   // Remember the time
      digitalWrite(ledPin, ledState);   // Update the actual LED
    }
  }
};


Flasher led1(22, 950, 750);
Flasher led2(23, 900, 800);
Flasher led3(24, 850, 850);
Flasher led4(25, 800, 900);
Flasher led5(26, 750, 950);
Flasher led6(27, 700, 1000);
Flasher led7(28, 650, 1050);
Flasher led8(29, 600, 1100);
Flasher led9(30, 550, 1150);
Flasher led10(31, 500, 1200);
Flasher led11(32, 450, 1250);
Flasher led12(33, 400, 1300);
Flasher led13(34, 350, 1350);
Flasher led14(35, 300, 1400);
Flasher led15(36, 250, 1450);

Flasher led16(37, 950, 750);
Flasher led17(38, 900, 800);
Flasher led18(39, 850, 850);
Flasher led19(40, 800, 900);
Flasher led20(41, 750, 950);
Flasher led21(42, 700, 1000);
Flasher led22(43, 650, 1050);
Flasher led23(44, 600, 1100);
Flasher led24(45, 550, 1150);
Flasher led25(46, 500, 1200);
Flasher led26(47, 450, 1250);
Flasher led27(48, 400, 1300);
Flasher led28(49, 350, 1350);
Flasher led29(51, 300, 1400);
Flasher led30(52, 250, 1450);

void setup()
{
}

void loop()

{
 
  if(digitalRead(2) == LOW)
 
{
  led1.Update();
  led2.Update();
  led3.Update();
  led4.Update();
  led5.Update();
  led6.Update();
  led7.Update();
  led8.Update();
  led9.Update();
  led10.Update();
  led11.Update();
  led12.Update();
  led13.Update();
  led14.Update();
  led15.Update();
 
  led16.Update();
  led17.Update();
  led18.Update();
  led19.Update();
  led20.Update();
  led21.Update();
  led22.Update();
  led23.Update();
  led24.Update();
  led25.Update();
  led26.Update();
  led27.Update();
  led28.Update();
  led29.Update();
  led30.Update();
}
}


And i was wondering if this was the correct way to achieve this? I have already run into some issues where the led's seem to get out of sync after a few minutes of sweeping.

Any advice much appreciated.
Thank you

patduino

Hi Mad,

I'm not completely sure what the sketch is trying to do. Can you explain it a little better? Also, what is pin 2 connected to and what is its purpose?

Your note says you have 3 sensors, but I only see one input.

Pat.
There are 10 types of people in the world, those who understand binary, and those that don't.

DrDiettrich

Your display freezes while pin 2 is high, this should be avoided. When it goes low, most probably all LEDs change their state.
Eventually also make the on/off time unsigned long.

Also respect the maximum current, drawable from all output pins together. No more than 100mA should be drawn from a port, not more than 200mA in total.

Madmonkeyrtu

Can you explain it a little better? Also, what is pin 2 connected to and what is its purpose?
Hi Pat. The purpose of this is to replicate sweeping style indicators which you can see on some cars nowadays. I have 15 high power LED's either side which are being switched on via transistors via the arduino.

Pin 2 was connected to a switch, i have actually currently removed it as i am switching it on by powering the arduino on and off, as it was only pausing the program. Not to sure how to set that up to actually switch the program on and off.


Eventually also make the on/off time unsigned long.

Also respect the maximum current, drawable from all output pins together. No more than 100mA should be drawn from a port, not more than 200mA in total.
Hi Doc,
so i should change
Code: [Select]
long OnTime;     // milliseconds of on-time
long OffTime;    // milliseconds of off-time


To
Code: [Select]
unsigned long OnTime;     // milliseconds of on-time
unsigned long OffTime;    // milliseconds of off-time


How would this help?

Also i am switching all the LED's with transistors, so that should takes care of the current limits.

Currently the program keeps on falling out of sync with itself.

TomGeorge

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?


Thanks.. Tom.. :)
Everything runs on smoke, let the smoke out, it stops running....

DrDiettrich

The LEDs can fall out of sync with your code, because you disable the timed processing depending on the button state. See #2.

A more stable approach would use a pattern array, and cycle through the array every 50ms. Then you can stop the cycle whenever and how long you want, and you can restart at any state.

Madmonkeyrtu

@Tom

I have attached a quick diagram of the circuit. It is just a repeat of resistor, transistor and LED for all 30 Lights.

Using ztx653 as the transistors.


A more stable approach would use a pattern array, and cycle through the array every 50ms. Then you can stop the cycle whenever and how long you want, and you can restart at any state.

Could you point me in the direction where i can find out more about pattern array, don't think i am finding the right thing when googling about it.

DrDiettrich

You can learn about arrays and bits in the Learning section. Then use a sufficiently large data type (uint32_t) to hold the state of all LEDs at a time (pattern), and an array of such patterns for sequential display. Use the binary number format to fill your array with the patterns. Show a pattern for e.g. 50ms, then advance to the next one. When you use shift registers to control the LEDs, a pattern can be shown using SPI or shiftOut(), with minimal coding efforts.

TomGeorge

#8
Apr 24, 2016, 05:15 am Last Edit: Apr 24, 2016, 05:16 am by TomGeorge
Hi,
Code: [Select]
void setup()
{
}

Why aren't you declaring you I/O here,  pinMode?

Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

Go Up