i all i am currently programming multiple sensors... 1 analogue controlling a bar chart of LEDs and then 2 more that only light up LEDs in certain ranges. I also have 2 digital switched but they are a completely different story....
My Code that works currently is for the 1 analogue controlling the bar chart and currently stands at:
// these constants won't change:
const int analogPinVol = 0; // the pin that the volume is attached to
const int ledCount = 6; // the number of LEDs in the bar graph
const int analogPinPlay = 1; // the pin that the play/pause is attached to
const int analogPinSource = 2; // the pin that the source is attached to
const int buttonPin = 2; // the pin that the skip sensor 1 is attached to
const int sensorRightPin = 1; // the pin that the skip sensor 2 is attached to
const int ledPinPlay = 6; // the pin that the LED for Play/pause is attached to
const int ledPinSource = 5; // the pin that the LED for source is attached to
const int ledPinPrevious = 4; // the pin that the LED for Previous track is attached to
const int ledPinNext = 3; // the pin that the LED for Next track is attached to
// Variables will change:
int ledPins[] = {
7, 9, 10, 11, 12, 13 }; // an array of pin numbers to which LEDs are attached
void setup() {
// loop over the pin array and set them all to output:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
pinMode(ledPinPlay, OUTPUT); // declare the ledPinplay as an OUTPUT
pinMode(ledPinSource, OUTPUT); // declare the ledPinplay as an OUTPUT
pinMode(ledPinPrevious, OUTPUT); // declare the ledPinplay as an OUTPUT
pinMode(ledPinNext, OUTPUT); // declare the ledPinplay as an OUTPUT
pinMode(buttonPin, INPUT); // declare the left sensor as an INPUT
}
}
void loop() {
// read the volume:
int sensorReading = analogRead(analogPinVol);
// map the result to a range from 0 to the number of LEDs:
int ledLevel = map(sensorReading, 120, 600, 0, ledCount);
// loop over the LED array:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// if the array element's index is less than ledLevel,
// turn the pin for this element on:
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
}
// turn off all pins higher than the ledLevel:
else {
digitalWrite(ledPins[thisLed], LOW);
}
}
}
Ignore most of the int as they arent programmed yet... But i am trying to combine this with an analogue lighting an LED which is currently:
// these constants won't change:
const int analogPinVol = 0; // the pin that the volume is attached to
const int ledCount = 6; // the number of LEDs in the bar graph
const int analogPinPlay = 1; // the pin that the play/pause is attached to
const int analogPinSource = 2; // the pin that the source is attached to
const int buttonPin = 2; // the pin that the skip sensor 1 is attached to
const int sensorRightPin = 1; // the pin that the skip sensor 2 is attached to
const int ledPinPlay = 6; // the pin that the LED for Play/pause is attached to
const int ledPinSource = 5; // the pin that the LED for source is attached to
const int ledPinPrevious = 4; // the pin that the LED for Previous track is attached to
const int ledPinNext = 3; // the pin that the LED for Next track is attached to
// Variables will change:
int ledPins[] = {
7, 9, 10, 11, 12, 13 }; // an array of pin numbers to which LEDs are attached
int val = 0;
void setup() {
// loop over the pin array and set them all to output:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
pinMode(ledPinPlay, OUTPUT); // declare the ledPinplay as an OUTPUT
pinMode(ledPinSource, OUTPUT); // declare the ledPinplay as an OUTPUT
pinMode(ledPinPrevious, OUTPUT); // declare the ledPinplay as an OUTPUT
pinMode(ledPinNext, OUTPUT); // declare the ledPinplay as an OUTPUT
pinMode(buttonPin, INPUT); // declare the left sensor as an INPUT
}
}
void loop() {
val = analogRead(analogPinPlay);
if (val > 160) {
digitalWrite(ledPinPlay, HIGH);
}
else {
//
digitalWrite(ledPinPlay, LOW);
}
}
Again... ignore the other int as they arent used yet.
I am just very unsure on how to combine these if statements and the times i have tried it with:
void loop() {
val = analogRead(analogPinPlay);
// read the volume:
int sensorReading = analogRead(analogPinVol);
// map the result to a range from 0 to the number of LEDs:
int ledLevel = map(sensorReading, 120, 600, 0, ledCount);
// loop over the LED array:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// if the array element's index is less than ledLevel,
// turn the pin for this element on:
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
if (val > 160) {
digitalWrite(ledPinPlay, HIGH);
}
// turn off all pins higher than the ledLevel:
else {
digitalWrite(ledPins[thisLed], LOW);
digitalWrite(ledPinPlay, LOW);
}
}
}
I get the error: a function -definition is not allowed here before { token.
Can anyone help please....
I also want to do the same with 'analogPinSource' do i just write a new value area and double the if commands???
I am so confused.