I know this is going to be a really simple, stupid mistake I've made, but for the life of me, I cannot figure it out.
I am trying to make a simple bargraph using a shift register to control the LEDs (yes, I know there's probably a simpler, easier way to do this, but I'm trying to learn the Arduino, electronics and programming, all at the same time, so please be patient with me, OK ) using the following code
int dataPin = 8;
int clockPin = 9;
int latchPin = 10;
int inputPin = 0;
int readVal = 0;
int outputVal = 0;
void setup()
{
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
readVal = analogRead(inputPin);
readVal = int(readVal / 114); //converts analogRead's 0-1023 range to a 0-8 range
outputVal = pow(2, readVal) - 1; //should convert readVal's 0-8 range to 0, 1, 3, 7, 15 etc.
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, outputVal);
digitalWrite(latchPin, HIGH);
}
But when I start to turn the pot, LED 1 lights, then it goes out while the other LEDs light up (so the output to the shift register goes 0, 1, 2, 6, 14 etc. and not 0, 1, 3, 7, 15 etc.). I have used a simple Serial.print to confirm that readVal is being calculated correctly and have tried switching from int arithmetic to float to see if that makes any difference, but it doesn't. If I write my own function to calculate the powers, it works fine and writing a simple loop
for(x = 0; x < 10; x++) { Serial.println(pow(2, x));}
also works fine.
So, where am I going wrong?
Many thanks,
David Shaw