Hello there,
I'm trying to control 5 leds (pin(13,12,8,7,4)) with 5 piezos (A0,A1,A2,A3,A4). something like piezoA0 switch pinled1, piezo A1 switch pinled2 etc etc.
I've written down a simple program to switch 1 led on/off with 1 piezo.
int ledPin =13;
int piezoPin = 0;
int val = 0;
int ledStatus = LOW;
int threshold = 1;
void setup(){
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
val = analogRead(piezoPin);
if(val>= threshold){
ledStatus = !ledStatus;
digitalWrite(ledPin, ledStatus);
Serial.println(val);
delay(180);
}
delay(10);
}
and than i tryed with fail to array the piezo and the leds. like in the example below. I'm sure i'm doing wrong since here i can control 5 leds with 1 piezo only and this is not what i need:
const int analogPin = A0;
const int ledCount = 5;
int ledPins[] = {4,7,8,12,13};
int ledStatus = LOW;
void setup() {
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
}
void loop() {
int ledLevel = 1;
int sensorReading = analogRead(analogPin);
//map(sensorReading, 0, 1023, 0, ledCount);
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
if (sensorReading > ledLevel) {
ledStatus = !ledStatus;
digitalWrite(ledPins[thisLed], ledStatus);
Serial.println(ledPins[thisLed]);
delay(10);
}
}
}
const byte ledCount = 5;
byte ledPins[] = {4, 7, 8, 12, 13};
int sensorReading;
int ledLevel = 1; // why 1 for analog value ??
void setup()
{
for (int thisLed = 0; thisLed < ledCount; thisLed++) pinMode(ledPins[thisLed], OUTPUT);
}
void loop()
{
for (int thisLed = 0; thisLed < ledCount; thisLed++)
{
sensorReading = analogRead(thisLed); // runs: 0,1,2,3,4,5
if (sensorReading > ledLevel)
{
digitalWrite(ledPins[thisLed], !digitalRead(ledPins[thisLed])); // toggle led N
// Serial.println(ledPins[thisLed]); // why this ?? Take a look at the LED
// if you NEED it: Serial.println(digitalRead(ledPins[thisLed]);
delay(10); // why this here ??
}
}
}
No i 've never meet him
well, i'm not the best in programming. i'm trying to improve myself.
By the way the sketch is not working properly. the led lights up intermittently and when i press the piezo it stops. but i would like it to light on and off with no intermittence. this is cos of delay(10). if i remove it from the sketch it seems to work but not properly. the led is so fast to turn on and off that it seems to be on actually. so happen that when i press the piezo the led just select the state in that moment.
i don't know if i explane good myself but this is the problem (im sorry for my english im italian).
So post your corrected code so far and we will look at it. Your current code has changed too much, and we need to know if you have corrected he code and not introduce any more errors.
OK first of all you have no Serial.begin call so you will never see anything. Set the baud rate to the highest value possible and adjust the serial monitor to match.
Simply constantly inverts ( toggles ) the state of the LED while the analogue reading is above 1. There are two things wrong with this:-
A value of 1 is way too low. Try printing out the values you read from the sensor. I bet you will find a value of 100 is more like the point.
You don't want to toggle the state of the LED you want to turn it on. Only when the sensor reading drop below the LED level do you want to turn off the LED next time the LED level is exceeded.
So you need a booliean variable ( one for each sensor / LED so make an array of them ) to tell you if to turn the LED on or off in that wrong line you have.
Hi there Mike,
Actually the piezos are attached to 5 wine bottles so i need lower values cos i'm not going to touch the piezo directly and the values i get are pretty low when i play with the bottle.
btw i was trying to use no array and this is what i've done.
You don't need to set the piezo bits as inputs, when you use them as analogue reads that will set them to be analogue inputs.
This is your code converted to use arrays:-
Thank u Mike,
Now i understand
Btw i would like the led to keep light on when i press the piezo and switch off when i press it again. i tried something like this but is not working very well
You don't want to toggle the state of the LED you want to turn it on. Only when the sensor reading drop below the LED level do you want to turn off the LED next time the LED level is exceeded.
So you need a booliean variable ( one for each sensor / LED so make an array of them ) to tell you if to turn the LED on or off in that wrong line you have.