Hello,
I'm attempting to map analog data to 8 LEDs via a 7HC595 shift register to control the overall brightness of the lights. Is this possible? I'm specifically confused how to map the analog input data to the shift register and control the light illumination. I hope to achieve a PWM-eqsue effect based on the variation in the analog input.
These are three sketches I'm working to combine:
Sketch 1:
//Analog read pins
const int xPin = 0;
int minVal = 265;
int maxVal = 402;
//to hold the caculated values
double x;
void setup(){
Serial.begin(9600);
}
void loop(){
//read the analog values from the accelerometer
int xRead = analogRead(xPin);
//convert read values to degrees -90 to 90 - Needed for atan2
int xAng = map(xRead, minVal, maxVal, -90, 90);
x = RAD_TO_DEG * (atan2(-yAng, -zAng) + PI);
Serial.print("x: ");
Serial.print(x);
delay(100);
}
SKETCH 2:
//Pin connected to ST_CP of 74HC595
int latchPin = 2;
//Pin connected to SH_CP of 74HC595
int clockPin = 1;
////Pin connected to DS of 74HC595
int dataPin = 3;
int xRead = analogRead(A0);
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
//set pins to output so you can control the shift register
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
void loop() {
// read the analog in value:
sensorValue = analogRead(A0);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 525, 0, 654);
// change the analog out value:
analogWrite(latchPin, outputValue);
// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.println(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
delay(250);
}
SKETCH 3:
const int Xdata = A2;
const int greenPin1 = 3;
const int greenPin2 = 4;
const int greenPin3 = 5;
const int greenPin4 = 6;
const int greenPin5 = 7;
const int greenPin6 = 8;
const int greenPin7 = 9;
const int greenPin8 = 10;
int sensorValue = 0;
int outputValue = 0;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
// read the analog in value:
sensorValue = analogRead(Xdata);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 525, 0, 654);
// change the analog out value:
analogWrite(greenPin1, outputValue);
analogWrite(greenPin2, outputValue);
analogWrite(greenPin3, outputValue);
analogWrite(greenPin4, outputValue);
analogWrite(greenPin5, outputValue);
analogWrite(greenPin6, outputValue);
analogWrite(greenPin7, outputValue);
analogWrite(greenPin8, outputValue);
// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.println(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
delay(50);
}
(code tags added by moderator)
Any advice is greatly appreciated....