Dear all,
I’ve bought recently the Arduino Starter kit and I am currently working around the project No. 6 which explains the tone() function.
I wanted to connect my piezo either to one pin with analogWrite() or to another pin with Tone().
I could switch on/off these pins with a push button and I could have heard the difference.
Moreover, just to play around with the LED, I wanted a LED to be connected to another pin with analogWrite() all the time.
So I created the attached “Arduino Uno V1” sketch.
But strangely, when the piezo receives the signal the Tone() (Pin 2), the analogWrite () from the pin 11 doesn’t work, when paradoxically it works when the piezo receives the signal from analogWrite() (pin 3).
I tried then to replace LEDs, etc… to check if the problem was not related to the the resistor or the blue LED but when I changed pin 11 to pin 5, everything works perfectly, whatever is the signal going to the piezo. (See attached file “Arduino Uno V2” sketch).
Do you have any idea why it does that with pin 11?
Below you can find my code.
Thanks in advance for your help.
Eric
CODE:
const int redLedPin = 10; // Pin definitions
const int blueLedPin = 11;
const int blueLedSparePin = 5;
const int blueLedTestPin = 12; //Green cable
const int greenLedPin = 7;
const int yellowLedPin = 6;
const int photoResPin = A0;
const int pushButtonPin = 4;
const int pushButtonTestPin = 8;
const int buzzerTonePin = 2;
const int buzzerPWMPin = 3;
const int initTime = 10000; // Time for initialization phase
int sensorValue; // Variables definition
int sensorLow = 1023; // Variables for calibration
int sensorHigh = 0;
int buttonPreviousSatus;
int buttonCurrentStatus;
int buzzerStatus;
int buzzerTest;
void setup() {
buttonPreviousSatus = LOW;
buttonCurrentStatus = LOW;
buzzerTest = LOW;
buzzerStatus = buzzerTonePin;
pinMode(redLedPin, OUTPUT);
pinMode(blueLedPin, OUTPUT);
pinMode(blueLedTestPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(buz// May be incorrectzerTonePin, OUTPUT);
pinMode(buzzerPWMPin, OUTPUT);
pinMode(pushButtonPin, INPUT);
pinMode(pushButtonTestPin, INPUT);
digitalWrite(blueLedPin, LOW);
digitalWrite(blueLedTestPin, LOW);
digitalWrite(yellowLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
digitalWrite(redLedPin, HIGH);
digitalWrite(buzzerPWMPin, LOW);
noTone(buzzerTonePin);
Serial.begin(9600);
Serial.println("Calibration's started:");
Serial.println();
// Initialization loop
while (millis()<initTime){
sensorValue = analogRead(photoResPin);
Serial.print("Sensor LOW: ");
Serial.print(sensorLow);
Serial.print(", Sensor HIGH: ");
Serial.println(sensorHigh);
if (sensorValue > sensorHigh){
sensorHigh = sensorValue;
}
if (sensorValue < sensorLow){
sensorLow = sensorValue;
}
}
digitalWrite(redLedPin, LOW); //Switch Off Red LED when calibration is done
digitalWrite(blueLedTestPin, HIGH); //Switch On the reference Blue LED
}
void loop() {
sensorValue = analogRead(photoResPin);
// Calibration check
if (sensorValue > sensorHigh){
sensorHigh = sensorValue;
}
if (sensorValue < sensorLow){
sensorLow = sensorValue;
}
//Frequency for tone function
int pitch = map(sensorValue, sensorLow, sensorHigh, 50, 4000);
//PWM for blueLedPin / buzzerPWMPin
int blueLedValue = map(sensorValue, sensorLow, sensorHigh, 0, 255);
//Tension for blueLedPin
float blueLedU = blueLedValue/255.0*5.0;
Serial.print("pitch: ");
Serial.print(pitch);
Serial.print(" Hz, Blue LED / Yellow Buzzer Value: ");
Serial.print(blueLedValue);
Serial.print(", Blue LED Tension Value: ");
Serial.println(blueLedU);
buttonCurrentStatus = digitalRead(pushButtonPin);
while(buttonCurrentStatus == HIGH){
buttonPreviousSatus = HIGH;
buttonCurrentStatus = digitalRead(pushButtonPin);
}
if(buttonPreviousSatus == HIGH){
buttonPreviousSatus = LOW;
if(buzzerStatus == buzzerTonePin){
buzzerStatus = buzzerPWMPin;
}
else{
buzzerStatus = buzzerTonePin;
}
}
// Blue LED swtiched on
analogWrite(blueLedPin, blueLedValue);
analogWrite(blueLedSparePin, blueLedValue);
analogWrite(redLedPin, blueLedValue);
if(buzzerStatus == buzzerTonePin){
digitalWrite(buzzerPWMPin, LOW);
digitalWrite(yellowLedPin, LOW);
digitalWrite(greenLedPin, HIGH);
tone(buzzerTonePin, pitch);
}
else{
noTone(buzzerTonePin);
digitalWrite(greenLedPin, LOW);
digitalWrite(yellowLedPin, HIGH);
analogWrite(buzzerPWMPin, blueLedValue);
}
delay(10);
buzzerTest = digitalRead(pushButtonTestPin);
while(buzzerTest == HIGH){
buzzerTest = digitalRead(pushButtonTestPin);
noTone(buzzerTonePin);
digitalWrite(greenLedPin, LOW);
digitalWrite(yellowLedPin, HIGH);
analogWrite(buzzerPWMPin, 255);
analogWrite(blueLedPin, 255);
}
buzzerTest = LOW;
delay(10);
}