0

TLC59711 Incorporating blink without delay / "Flasher" clas
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

TLC59711 Incorporating blink without delay / "Flasher" clas

by iainmac on Thu Aug 01, 2019 5:21 am

Hi,

I have been progressing with a project to create a bicycle turn signals board. I have the full functionality I want using singe LEDS directly connected to digital pins for the prototype stage. However when i try to create ways to recreate those blink without delay type functions on individual channels on the PWM LED driver board I am struggling to get to the next stage.

I am stuck trying to adopt the principles learned in the tutorial "multitasking the arduino" (creating a flasher class) and apply them to individual channels of the driver board. Ive looked at the code in the random blinky eyes project as i think that could be modified but just starting to go round in circles.

What I cant get to is basically blinking different channels of the board when required.

Code: Select all | TOGGLE FULL SIZE
   case 2:                   // indicating left
   digitalWrite(12,LOW);       // turn off right indicator
   turnLEFT.Update();          //Flasher turnLEFT(9, 500, 500);
   // how to replicate the above but on one of the Driver PWM channels so the indicator blinks ???       


Any help would be appreciated

https://learn.adafruit.com/multi-taskin ... y-solution


Code: Select all | TOGGLE FULL SIZE
// PWM Channel0 - Left indicator (also hazard light)
// PWM Channel2 - Right indicator (also hazard light)
// PWM Channel1 - Main front lights
// PWM Channel3 - rear Brake light 1
// PWM Channel5 - rear Brake light 2


//////////////////////////////////////////////////////////////////////////////////////////////////////

#include "Adafruit_TLC59711.h"                                          // 12-Channel 16-bit PWM LED Driver
#include <SPI.h>/                                                       // 12-Channel 16-bit PWM LED Driver
#define NUM_TLC59711 1                                                  // How many Driverboards do you have chained?
#define data   11                                                       // 12-Channel data
#define clock  13                                                       // 12-Channel clock
Adafruit_TLC59711 tlc = Adafruit_TLC59711(NUM_TLC59711, clock, data);   // 12-Channel 16-bit PWM LED Driver
//Adafruit_TLC59711 tlc = Adafruit_TLC59711(NUM_TLC59711);              // 12-Channel 16-bit PWM LED Driver

//////////////////////////////////////////////////

const int pinLEFT_switch = 5;                                          //left turn switch
const int pinRIGHT_switch = 7;                                         //right turn switch
const int pinHAZARD_switch = 6;                                        //hazards switch
const int pinMAINLIGHTS_switch = 4;                                    //main ights switch
int opState = 0;                                                       //define total variable used in switch case

//////////////////////////////////////////////////////////////////////////////////////////////////////

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
    }
  }
};
 
 
////////////////////////////////////debug leds for above///////////////////////////////////////////
Flasher turnLEFT(9, 500, 500);                                                        // (Pin Number, On time, Off time)
Flasher turnRIGHT(12, 500, 500);                                                      // (Pin Number, On time, Off time)
Flasher hazardLEFT(9, 300, 300);                                                      // (Pin Number, On time, Off time)
Flasher hazardRIGHT(12, 300, 300);                                                    // (Pin Number, On time, Off time)
Flasher breaking(8, 20, 20);                                                          // (Pin Number, On time, Off time)

                                                 
//////////////////////////////////////////////////////////////////////////////////////////////////////

void setup(){
    Serial.begin(9600);
    pinMode(pinLEFT_switch, INPUT_PULLUP);
    pinMode(pinRIGHT_switch, INPUT_PULLUP);
    pinMode(pinHAZARD_switch, INPUT_PULLUP);
    pinMode(pinMAINLIGHTS_switch, INPUT_PULLUP);
    tlc.begin();                                                                  // Start the 12-Channel 16-bit PWM LED Driver board
    tlc.setPWM(0, 30000);
    digitalWrite(9, HIGH);   
    tlc.setPWM(1, 30000);
    digitalWrite(10, HIGH);
    tlc.setPWM(2, 30000);
    digitalWrite(12, HIGH);
    tlc.setPWM(3, 30000);
    tlc.setPWM(5, 30000);
    digitalWrite(8, HIGH);
    tlc.write();
    delay (50);
    tlc.setPWM(0, 0);
    digitalWrite(9, LOW);   
    tlc.setPWM(1, 0);
    digitalWrite(10, LOW);
    tlc.setPWM(2, 0);
    digitalWrite(12, LOW);
    tlc.setPWM(3, 0);
    tlc.setPWM(5, 0);
    digitalWrite(8, LOW);
    tlc.write();
    delay (50);
    tlc.setPWM(0, 30000);
    digitalWrite(9, HIGH);   
    tlc.setPWM(1, 30000);
    digitalWrite(10, HIGH);
    tlc.setPWM(2, 30000);
    digitalWrite(12, HIGH);
    tlc.setPWM(3, 30000);
    tlc.setPWM(5, 30000);
    digitalWrite(8, HIGH);
    tlc.write();
    delay (50);
    tlc.setPWM(0, 0);
    digitalWrite(9, LOW);   
    tlc.setPWM(1, 0);
    digitalWrite(10, LOW);
    tlc.setPWM(2, 0);
    digitalWrite(12, LOW);
    tlc.setPWM(3, 0);
    tlc.setPWM(5, 0);
    digitalWrite(8, LOW);
    tlc.write();
    delay (2000);
   
    tlc.setPWM(0, 0);
    tlc.setPWM(2, 0);
    tlc.setPWM(3, 0);
    tlc.setPWM(5, 0);
    tlc.setPWM(1, 64000);
    digitalWrite(10 ,HIGH);
    tlc.write();
    delay (10);
   
           
}

//////////////////////////////////////////////////////////////////////////////////////////////////////

void loop(){
 
unsigned long currentMillis = millis();
 
 opState = 0;

 if (digitalRead(4) == LOW) {
    breaking.Update();
    tlc.setPWM(3, 64000);
    tlc.setPWM(5, 64000);
    tlc.write();
    delay (20);
    tlc.setPWM(3, 3000);
    tlc.setPWM(5, 3000);
    tlc.write();
    delay (20);
    Serial.print("Breaking....");
  }
 else {
    digitalWrite(8, HIGH);
    tlc.setPWM(3, 5000);
    tlc.setPWM(5, 5000);
    tlc.write();
   
 }
 
 if ((digitalRead(6) == HIGH) && (digitalRead(5) == HIGH) && (digitalRead(7) == HIGH)) {     //Hazard Off + Left Off + Right Off PINS are HIGH (off state)
      Serial.println("Hazard Off + Left Off + Right Off");
      opState =1;
 }     
 if ((digitalRead(6) == HIGH) && (digitalRead(5) == LOW) && (digitalRead(7) == HIGH)) {     //Hazard Off + Left On
      Serial.println("Hazard Off + Left On");
      opState =2;
 }
 if ((digitalRead(6) == HIGH) && (digitalRead(5) == HIGH) && (digitalRead(7) == LOW)) {     //Hazard Off + Right On
      Serial.println("Hazard Off + Right On");
      opState =3;
 }
 if ((digitalRead(6) == LOW) && (digitalRead(5) == HIGH) && (digitalRead(7) == HIGH)) {     //Hazard On + Left Off + Right Off
      Serial.println("Hazard On + Left Off + Right Off");
      opState =4;
 }
 if ((digitalRead(6) == LOW) && (digitalRead(5) == LOW) && (digitalRead(7) == HIGH)) {     //Hazard On + Left On
      Serial.println("Hazard On + Left On");                                           
      opState =4;
 }
 if ((digitalRead(6) == LOW) && (digitalRead(5) == HIGH) && (digitalRead(7) == LOW)) {     //Hazard On + Right On
      Serial.println("Hazard On + Right On");                                           
      opState =4;
 }
 
 switch (opState){

   case 1:                                                                                  // both switches are off so just turn the leds off, no need for timing
   digitalWrite(9, LOW);
   digitalWrite(12 ,LOW);
   Serial.print("opState =  ");
   Serial.print(opState);
   Serial.print(" ");
   break;
   
   case 2:
   digitalWrite(12,LOW);
   turnLEFT.Update();
   
   tlc.write();
   Serial.print("opState =  ");
   Serial.print(opState);
   Serial.print(" ");
   break;
 
   case 3:
   
   digitalWrite(9, LOW);
   turnRIGHT.Update();
   Serial.print("opState =  ");
   Serial.print(opState);
   Serial.print(" ");
   break;
   
   case 4:
   
   hazardLEFT.Update();
   hazardRIGHT.Update();
   Serial.print("opState =  ");
   Serial.print(opState);
   Serial.print(" ");
   break;
     
   case 5:                                           
 
   hazardLEFT.Update();
   hazardRIGHT.Update();
   Serial.print("opState =  ");
   Serial.print(opState);
   Serial.print(" ");
   break;
   
   case 6:                                           
   
   hazardLEFT.Update();
   hazardRIGHT.Update();
   Serial.print("opState =  ");
   Serial.print(opState);
   Serial.print(" ");
   break;
 
       
   default:
   Serial.print("default opState  ");
               
 }

}
////////////////////////////////////////////////////////////////////////////////////////////////////// 
Attachments
DSC_0794small.JPG
DSC_0794small.JPG (851.8 KiB) Viewed 42 times

iainmac
 
Posts: 2
Joined: Mon May 12, 2014 2:29 pm

Please be positive and constructive with your questions and comments.