First post ever on the forums and still rather new to coding in Arduino IDE.
I'm trying to code a sketch so that as I turn the dial on the 10k pot, the LED's light up left to right in sequence. I mapped the pot values just fine "outputValue = map(potValue, 0, 1024, 0, 255);".
I have studied a lot of sites & info on an array for the 74HC595. What I'm trying to learn is how to use the value from the pot and apply that to the array to light up the appropriate # of LED's as I ramp up the dial on the pot. I've copied/pasted pieces of code from across the 'Net to put this sketch together. I'll include the code (so far) below. I'm hoping this is an easy fix for someone who REALLY knows how to code.
I want to thank up-front anyone who responds and appreciate any tips/ideas/inspiration to be a better coder!
// potentiometer controls
int potPin = A0; // pin for the pot
int potValue = 0; // incoming value from the pot
int outputValue = 0; // mapped value from pot to determine output levels
// shift register pins
int latchPin = 8; // Pin 12 on shift register
int clockPin = 12; // Pin 11 on shift register
int dataPin = 11; // Pin 14 on shift register
// holders for info going to shift register
byte data;
byte dataArray[9]; // 9 LED's; 3 green / 3 amber / 3 red
byte outputData; // translates pot value to array #
void setup()
{
pinMode(latchPin, OUTPUT); // shift register outputs
pinMode(clockPin, OUTPUT);
PinMode(dataPin, OUTPUT);
serial.begin(9600);
// binary notation as comment
dataArray[0] = 0xFF; // 0b11111111
dataArray[1] = 0xFE; // 0b11111110
dataArray[2] = 0xFC; // 0b11111100
dataArray[3] = 0xF8; // 0b11111000
dataArray[4] = 0xF0; // 0b11110000
dataArray[5] = 0xE0; // 0b11100000
dataArray[6] = 0xC0; // 0b11000000
dataArray[7] = 0x80; // 0b10000000
dataArray[8] = 0x00; // 0b00000000
}
void loop()
{
potValue = analogRead(potPin);
outputValue = map(potValue, 0, 1024, 0, 255);
// NOTE: this section is still being tinkered with. IDK if it's effective coding.
if (outputValue <= 28)
outputData = 0;
{
else if (outputValue >= 29 && <= 56)
outputData = 1;
else if (outputValue >= 57 && <= 84)
outputData = 2;
else if (outputValue >= 85 && <= 112)
outputData = 3;
else if (outputValue >= 113 && <= 140)
outputData = 4;
else if (outputValue >= 141 && <= 168)
outputData = 5;
else if (outputValue >= 169 && <= 196)
outputData = 6;
else if (outputValue >= 197 && <= 224)
outputData = 7;
else if (outputValue >= 225 && <= 255)
outputData = 8;
for (int j = 0; j < 9; j++)
{
data = dataArray[j]; // load the light sequence you want from array
digitalWrite(latchPin, 0); // ground latchPin and hold low for as long as you are transmitting
shiftOut(dataPin, clockPin, data); // move 'em out
digitalWrite(latchPin, 1); // return the latch pin high to signal chip that it
delay(300); // no longer needs to listen for information
}
}
// the heart of the program; outputValue lights corresponding LEDs
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);
Frankly, it's a mess. That's what happens when you copy & paste bits of code from different sources without understanding them.
You missed a part of the code at the bottom, is not complete.
You seem to have a function called shiftOut(). You don't need that because the Arduino language has such a function built-in. Much better to use that.
I don't understand why you map the 0..1023 value from the analog pin to 0..255 range, because you then use a long and complicated if-else construct to reduce that to a 0..8 range. Why not map to 0..8 in one step?
The code that sends the data to the shift register does not even use the result you calculate. It sends the contents of your array, one after the other.
Your array is not needed either. The values can be simply calculated on the fly.
Sorry to blast your attempt full of holes. But stick at it, you can do this. I think it must be harder than learning any human language. Your first utterances make very little sense, but other humans can usually piece together what you mean from context. Computers can't do that.
PaulRB:
I don't understand why you map the 0..1023 value from the analog pin to 0..255 range, because you then use a long and complicated if-else construct to reduce that to a 0..8 range. Why not map to 0..8 in one step?
I think this is what PaulRB means.
/*
potValue = analogRead(potPin);
outputValue = map(potValue, 0, 1024, 0, 255);
// NOTE: this section is still being tinkered with. IDK if it's effective coding.
if (outputValue <= 28)
outputData = 0;
{
else if (outputValue >= 29 && <= 56)
outputData = 1;
else if (outputValue >= 57 && <= 84)
outputData = 2;
else if (outputValue >= 85 && <= 112)
outputData = 3;
else if (outputValue >= 113 && <= 140)
outputData = 4;
else if (outputValue >= 141 && <= 168)
outputData = 5;
else if (outputValue >= 169 && <= 196)
outputData = 6;
else if (outputValue >= 197 && <= 224)
outputData = 7;
else if (outputValue >= 225 && <= 255)
outputData = 8;
*/
outputData = map(analogRead(potPin), 0, 1023, 0, 8); // this single line could replace that whole block, and the outputValue and potValue declarations can be deleted
Thanks for the feedback. I don't take offense at some harsher comments. I'm still learning.
I've really cleaned up the code and I think I've nearly achieved what I was aiming for.
I have a line of 8 LED's that I want to light up consecutively left-to-right as I turn the pot handle CW & then darken right-to-left as I turn the pot handle back CCW.
I found that I need to watch the amount of current drawing through the 74HC595 chip as it's limited to 70 mA continuous Vcc. I am aware of a high-power TPIC6B595 that can handle it. Any other ideas for shift register IC's that might work for me?
Here is what my code looks like after I cleaned it up:
int potPin = A0; // pin for the pot
byte latchPin = 8; // Pin 12 on shift register
byte clockPin = 12; // Pin 11 on shift register
byte dataPin = 11; // Pin 14 on shift register
byte leds = 0; // which LED is first in line to light up
int outputData; // takes pot value & translates to # of LEDs
void setup()
{
pinMode(latchPin, OUTPUT); // shift register outputs
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
void loop()
{
outputData = map(analogRead(potPin), 0, 1023, 0, 8); // takes pot-value & maps to 8 pins
bitSet(leds, outputData); // sets outputData to corresponding 595 pin
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds); // writes data to corresponding 595 pin
digitalWrite(latchPin, HIGH);
// leds = 0; // noticed that this doesn't do the job of darkening the LEDs as I turn the pot CCW
// I may still need assistance to figure this thing out. I'm very close to being done!!
}
Note: the ultimate goal is to use this function to control 5050 1W LED's on a robot-car I'm building.
int potPin = A0; // pin for the pot
byte latchPin = 8; // Pin 12 on shift register
byte clockPin = 12; // Pin 11 on shift register
byte dataPin = 11; // Pin 14 on shift register
byte leds = 0; // which LED is first in line to light up
int outputData; // takes pot value & translates to # of LEDs
void setup()
{
pinMode(latchPin, OUTPUT); // shift register outputs
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
void loop()
{
outputData = map(analogRead(potPin), 0, 1023, 0, 8); // takes pot-value & maps to 8 pins
if (outputData == 0) leds = 0; else leds = bit(outputData) - 1; // sets outputData to corresponding 595 pin
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds); // writes data to corresponding 595 pin
digitalWrite(latchPin, HIGH);
}
There are lots of other shift registers you could use for normal leds. But 1W leds will require constant-current drivers (and good heat-sinks). Assuming you are using 5V to power the circuit, a 1W led will draw around 200mA. I don't know of any constant-current led driver chips that can sink more than about 100-120mA. So you may have to use individual constant current drivers for each led. In that case, you might as well use 74hc595.