Hello,
I'm working to control and manipulate 16 LEDs daisychained through 2 74hc595 shift registers but I am starting with 8 LEDs and 1 shift register for now. I want to use analog data from an accelerometer to control which lights are on and off from left to right on something like a sliding scale dependent on tilt.
I've been combining scripts to get close to what I want but I'm not sure where to go from here. I'm having trouble figuring out specifically A. Sending and writing bits and figuring out their states within "if else" statements. Can anyone tell me what revisions I need to make the the script to make it work? Or is there a simpler way to script the rewrite the entire interaction?
Any information helps. Thanks.
The script:
const int Xdata = A0; // Analog input pin that accelerometer is attached to
// The steps to reach each LED
const int stepZero = 31;
const int stepOne = 62;
const int stepTwo = 93;
const int stepThree = 124;
const int stepFour = 155;
const int stepFive = 186;
const int stepSix = 217;
const int stepSeven = 248;
const int stepEight = 255;
int sensorValue0 = 0; // value read from accelerometer
int outputValue0 = 0; // value output from accelerometer
//Pin connected to latch pin (ST_CP) of 74HC595 #0
const int latchPin0 = 1;
//Pin connected to clock pin (SH_CP) of 74HC595 #0
const int clockPin0 = 2;
////Pin connected to Data in (DS) of 74HC595 #0
const int dataPin0 = 3;
void setup() {
Serial.begin(9600);
pinMode(latchPin0, OUTPUT);
pinMode(dataPin0, OUTPUT);
pinMode(clockPin0, OUTPUT);
Serial.begin(9600);
Serial.println("reset");
}
void loop() {
// read the analog in value:
sensorValue0 = analogRead(Xdata);
// map it to the range of the analog out based on accelerometer data:
outputValue0 = map(sensorValue0, 0, 525, 0, 654);
// print the results to the serial monitor:
Serial.print("sensor 0= " );
Serial.print(sensorValue0);
Serial.print("\t output 0= ");
Serial.println(outputValue0);
delay(100);
if (outputValue0 <= stepZero) {
int bittoset0 = 0;
// write to the shift register with the correct bit set high:
analogWrite(bittoset0, HIGH);
}
else if (outputValue0 <= stepOne) {
int bittoset0 = 1;
// write to the shift register with the correct bit set high:
analogWrite(bittoset0, HIGH);
}
else if (outputValue0 <= stepTwo) {
int bittoset0 = 2;
// write to the shift register with the correct bit set high:
analogWrite(bittoset0, HIGH);
}
else if (outputValue0 <= stepThree) {
int bittoset0 = 3;
// write to the shift register with the correct bit set high:
analogWrite(bittoset0, HIGH);
}
else if (outputValue0 <= stepFour) {
int bittoset0 = 4;
// write to the shift register with the correct bit set high:
analogWrite(bittoset0, HIGH);
}
else if (outputValue0 <= stepFive) {
int bittoset0 = 5;
// write to the shift register with the correct bit set high:
analogWrite(bittoset0, HIGH);
}
else if (outputValue0 <= stepSix) {
int bittoset0 = 6;
// write to the shift register with the correct bit set high:
analogWrite(bittoset0, HIGH);
}
else if (outputValue0 <= stepSeven) {
int bittoset0 = 6;
// write to the shift register with the correct bit set high:
analogWrite(bittoset0, HIGH);
}
else if (outputValue0 <= stepEight) {
int bittoset0 = 6;
// write to the shift register with the correct bit set high:
analogWrite(bittoset0, HIGH);
}
//This portion kills the script:
void analogWrite(bittoset0, LOW)
// the bits you want to send
byte bitsToSend0 = 0;
// turn off the output so the pins don't light up
// while you're shifting bits:
digitalWrite(latchPin0, LOW);
// turn on the next highest bit in bitsToSend:
bitWrite(bitsToSend0, latchPin0, HIGH);
// shift the bits out:
shiftOut(dataPin0, clockPin0, MSBFIRST, bitsToSend0);
// turn on the output so the LEDs can light up:
digitalWrite(latchPin0, HIGH);
}