Go Down

Topic: My Code Share (Read 15 times) previous topic - next topic

ConfusionSaysASK

Potentiometer 10 Led Bar Graph Control using Multiplexer (Straight Forward Solution!)


//Example by: Piotr Drazek
//Multiplexer used in this example is 74HC595
//10 led bar graph where first 3 leds simultaneously are assigned to Q0 on the multiplexer
//If you are not using an LCD you can remove all sections of code related to LCD. It was used for troubleshooting. Second Potentiometer one right was used for LCD brightness control.

//shiftout function sourced from shiftout example found at: http://arduino.cc/en/tutorial/ShiftOut

#include <LiquidCrystal.h>

//LCD Control
LiquidCrystal lcd(12,11,5,4,3,2);
const int switchPin = 6;
//LCD Control


//Bar Graph related below
// these constants won't change:
const int analogPin = A0;   // the pin that the potentiometer is attached to
const int ledCount = 8;    // the number of LEDs in the bar graph (10) number of multiplexer outputs 8
//Bar Graph related Above

//Shift Out related below
//Pin connected to ST_CP of 74HC595 GREEN
int latchPin = 8;
//Pin connected to SH_CP of 74HC595 BLUE
int clockPin = 10;
////Pin connected to DS of 74HC595 YELLOW
int dataPin = 9;
//holder for information you're going to pass to shifting function
byte data = 0;
//Shift out related above

//---------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------

void setup() {
 
  //lcd related
  lcd.begin(16,2); //Define size of lcd, 16 char long, 2 rows
  pinMode(switchPin, INPUT);
  lcd.print("Keep");
  lcd.setCursor(0,1);
  lcd.print("Climbing!");
  //lcd related
   
//multiplexer related
   //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
//multiplexer related
}

//---------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------


void loop() {
 
   // read the potentiometer: SENSOR READING is a large number like 442 which is between 0 and 1023
   int sensorReading = analogRead(analogPin);
   
   //debugging
/*  lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("sensorReading");
    lcd.setCursor(0,1);
    lcd.print(sensorReading);
    delay(3000);
  */ 

   
   // map the result to a range from 0 to the number of LEDs:
   int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
 
 
   //debugging
/*  lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("ledLevel");
    lcd.setCursor(0,1);
    lcd.print(ledLevel);
    delay(3000);
*/

GraphBarStage(ledLevel);
   

} //END VOIDLOOP

//The below code is simple but inefficient. You can use this system to control a longer bar graph made up of multiples of this example.
//Create a formula for this based on the pattern below.
void GraphBarStage(int stopHere) {

if(stopHere == 0)
{
    digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, 0);
    digitalWrite(latchPin, 1);
   
}
if(stopHere == 1)
{
    digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, 1);
    digitalWrite(latchPin, 1);
   
}
if(stopHere == 2)
{
    digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, 3);
    digitalWrite(latchPin, 1);
   
}
if(stopHere == 3)
{
    digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, 7);
    digitalWrite(latchPin, 1);
   
}
if(stopHere == 4)
{
    digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, 15);
    digitalWrite(latchPin, 1);
   
}
if(stopHere == 5)
{
    digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, 31);
    digitalWrite(latchPin, 1);
   
}
if(stopHere == 6)
{
    digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, 63);
    digitalWrite(latchPin, 1);
   
}
if(stopHere == 7)
{
    digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, 127);
    digitalWrite(latchPin, 1);
   
}
if(stopHere == 8)
{
    digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, 255);
    digitalWrite(latchPin, 1);
   
}
}

//Literal Simplicity
//Note: on 10 led bar graph, i chose to control the first 3 with Q0 simultaneously.
//shiftout(dataPin, clockPin, 0) - turns them all off = 10 Led Bar Graph Dark
//shiftout(dataPin, clockPin, 1) - light led(s) connected to Q0 = 1st led lit only
//shiftout(dataPin, clockPin, 2) - light led(s) connected to Q1 = 2nd led lit only
//shiftout(dataPin, clockPin, 3) - light led(s) connected to Q0 + Q1 at the same time = 1st and 2nd led lit simultaneously
//shiftout(dataPin, clockPin, 7) - light led(s) connected to Q0 + Q1 + Q2 at the same time = 1st, 2nd, 3rd led lit simultaneously
//shiftout(dataPin, clockPin, 15) - light led(s) connected to Q0 + Q1 + Q2 + Q3 at the same time = 1st, 2nd, 3rd, 4th led lit simultaneously
//shiftout(dataPin, clockPin, 31) - light led(s) connected to Q0 + Q1 + Q2 + Q3 + Q4 at the same time = 1st, 2nd, 3rd, 4th, 5th led lit simultaneously
//shiftout(dataPin, clockPin, 63) - light led(s) connected to Q0 + Q1 + Q2 + Q3 + Q4 + Q5 at the same time = 1st, 2nd, 3rd, 4th, 5th, 6th led lit simultaneously
//shiftout(dataPin, clockPin, 127) - light led(s) connected to Q0 + Q1 + Q2 + Q3 + Q4 + Q5 + Q6 at the same time = 1st, 2nd, 3rd, 4th, 5th, 7th led lit simultaneously
//shiftout(dataPin, clockPin, 255) - light led(s) connected to Q0 to Q7 = 1st, 2nd, 3rd, 4th, 5th, 7th, 8th led lit simultaneously

// the heart of the program
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
  // This shifts 8 bits out MSB first,
  //on the rising edge of the clock,
  //clock idles low

  //internal function setup
  int i=0;
  int pinState;
  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, OUTPUT);

  //clear everything out just in case to
  //prepare shift register for bit shifting
  digitalWrite(myDataPin, 0);
  digitalWrite(myClockPin, 0);

  //for each bit in the byte myDataOut´┐Ż
  //NOTICE THAT WE ARE COUNTING DOWN in our for loop
  //This means that %00000001 or "1" will go through such
  //that it will be pin Q0 that lights.
  for (i=7; i>=0; i--)  {
    digitalWrite(myClockPin, 0);

    //if the value passed to myDataOut and a bitmask result
    // true then... so if we are at i=6 and our value is
    // %11010100 it would the code compares it to %01000000
    // and proceeds to set pinState to 1.
    if ( myDataOut & (1<<i) ) {
     
         /*    lcd.clear();
               lcd.setCursor(0,0);
               lcd.print("ifSO_myDataOut");
               lcd.setCursor(0,1);
               lcd.print(myDataOut);
               delay(3000);
      */
      pinState= 1;
    }
    else {   
      //LedLevel = 0 We get sent here loop a few times and nothing lights up.
       /*    lcd.clear();
             lcd.setCursor(0,0);
             lcd.print("elseSO_myDataOut");
             lcd.setCursor(0,1);
             lcd.print(myDataOut);
             delay(3000);
      */
      pinState= 0;
    }

    //Sets the pin to HIGH or LOW depending on pinState
    digitalWrite(myDataPin, pinState);
    //register shifts bits on upstroke of clock pin 
    digitalWrite(myClockPin, 1);
    //zero the data pin after shift to prevent bleed through
    digitalWrite(myDataPin, 0);
  }

  //stop shifting
  digitalWrite(myClockPin, 0);
}


Go Up