Tuesday, 12 November 2013

PLBP for chipKit DP32 { Proverbial (4) Led Blink Program }





Ok I just could not resist, and had to post the Proverbial Led blink Program adapted for the DP32, that has 4 LED's on the board ( Digital Pins 11 to 14).
Will scroll them from right to left, speed varying on the variable resistor on board( trim pot).






/* Proverbial (4) Led Blink Program
 
 Scrolls 4 light emitting diodes(LED), by turning on and off connected to 4 different digital  
 pins, using the delay() function and the current millis alike :) . 
 
 The circuit:
 * 4 LED's on board - pins D11, D12, D13 and D14 
 ** Variable resistor/trimpot on A2
 
 *** Version done for the chipKit board DP32 **
 ****  http://dubworks.blogspot.co.uk/
 */

// constants won't change. Used here to 
// set pin numbers:
const int ledPin =  11;      // the number of the LED pin
const int ledPin1 =  12; 
const int ledPin2 =  13; 
const int ledPin3 =  14; 
int i;
// Variables will change:
int ledState = LOW;             // ledState used to set the LED
int ledState1 = LOW;
int ledState2 = LOW;
int ledState3 = LOW;
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
//long interval = 500;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(A2, INPUT);
  ledState = !ledState;  //Toggle the bits to start
  ledState1 = !ledState1;
  ledState2 = !ledState2;
  ledState3 = !ledState3;  
}

void loop()
{
  boolean b; 
  long interval = analogRead(A2); // interval at which to enter function.(milliseconds)
  interval= interval/3; //So it is not too slow
  digitalWrite(ledPin, ledState);
  digitalWrite(ledPin1, ledState1);
  digitalWrite(ledPin2, ledState2);
  digitalWrite(ledPin3, ledState3); 
  // 

  // check to see if it's time 
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) {
    // save the last time  
    previousMillis = currentMillis;   
    // 
    int var;
    var=interval-(currentMillis - previousMillis);
    // conditional depending on increment accumulator
    if(!b){
      switch (i) {

      case 0 :    
        ledState = !ledState;
        digitalWrite(ledPin, ledState); 
        delay((var*0.5));
        break;

      case 1 :    
        ledState1 = !ledState1;
        digitalWrite(ledPin1, ledState1); 
        delay((var));
        break;

      case 2 :    
        ledState2 = !ledState2;
        digitalWrite(ledPin2, ledState2); 
        delay((var*1.5));
        break;

      case 3 :    
        ledState3 = !ledState3;
        digitalWrite(ledPin3, ledState3); 
        delay((var*2));
        break;

      default:
        b=!b;
        break;
      }
    }
    else{
      switch (i) {

      case 0 :    
        ledState = !ledState;
        digitalWrite(ledPin, ledState); 
        delay((var*2.5));
        break;

      case 1 :    
        ledState1 = !ledState1;
        digitalWrite(ledPin1, ledState1); 
        delay((var*3));
        break;

      case 2 :    
        ledState2 = !ledState2;
        digitalWrite(ledPin2, ledState2); 
        delay((var*3.5));
        break;

      case 3 :    
        ledState3 = !ledState3;
        digitalWrite(ledPin3, ledState3); 
        delay((var*4));
        break;

      default:
        b=!b;
        break;
      }
    }
    i++; // Increment our accumulator
    i=i%5; 
    // Keep the value within  the parameters needed for our switch case
    // in this case 5, so we can have 4 + 1 
    // (0, 1, 2 and 3) + 1 for default toggling of boolean variable b. (aesthetic reasons ?!)  
  }
}




No comments:

Post a Comment

Feel free to contact me with any suggestions, doubts or requests.

Bless