LED Strip Buzzer break in void loop - Page 1
Forum section: electronics / beginners

author: dleebell
author_rank: Contributor
timestamp: May 13, 2015, 02:57:36 pm
content: HiI have a set up of an RBG LED strip with a 3 RBG buttons connected with a piezo buzzer to my arduino.I wanting to simply make a break in the void loop for when I press the Red button, the Red LED's come on and the buzzer makes one single tone and then stops, but the LED's remain on.This is the code that I have so far.Code:if (outputValueR == 0) {digitalWrite(outputR, HIGH);noTone(6); // tone 1// play a note on pin 6 for 200 ms:tone(6, 220, 165);delay (200);break;I have attempted to put in a break; but I haven't managed to make this work.Thanks

author: electr_peter
author_rank: Unknown
timestamp: May 13, 2015, 03:55:04 pm
content: Did you turn off LED in main loop? Like thisCode:digitalWrite(outputR, LOW);

author: mikerj
author_rank: Super Contributor
timestamp: May 13, 2015, 05:15:46 pm
content: That can't be all the code you have written so far, and without the rest of it it's impossible to say what you are doing wrong.

author: dleebell
author_rank: Contributor
timestamp: May 13, 2015, 06:49:26 pm
content: HiYes sorry that isn't everything. Here's everything, I have tried to put in some kind of flag to have the buzzer only buzz once and LED to remain on.Code:/*Analog input, analog output, serial outputReads an analog input pin, maps the result to a range from 0 to 255and uses the result to set the pulsewidth modulation (PWM) of an output pin.Also prints the results to the serial monitor.The circuit:* potentiometer connected to analog pin 0.Center pin of the potentiometer goes to the analog pin.side pins of the potentiometer go to +5V and ground* LED connected from digital pin 9 to groundcreated 29 Dec. 2008modified 9 Apr 2012by Tom IgoeThis example code is in the public domain.*/// These constants won't change. They're used to give names// to the pins used:const int analogInPinR = A0; // Analog input pin that the potentiometer is attached toconst int analogInPinG = A1; // Analog input pin that the potentiometer is attached toconst int analogInPinB = A2; // Analog input pin that the potentiometer is attached toint sensorValueR = 0; // value read from the potint sensorValueG = 0; // value read from the potint sensorValueB = 0; // value read from the potint outputValueR = 0; // value output to the PWM (analog out)int outputValueG = 0; // value output to the PWM (analog out)int outputValueB = 0; // value output to the PWM (analog out)int outputR = 10;int outputG = 11;int outputB = 12;int flagR = 1;int countR = 0;int toneR = 0;void setup() {// initialize serial communications at 9600 bps:Serial.begin(9600);pinMode(outputR, OUTPUT);pinMode(outputG, OUTPUT);pinMode(outputB, OUTPUT);}void loop() {// read the analog in value:sensorValueR = analogRead(analogInPinR);sensorValueG = analogRead(analogInPinG);sensorValueB = analogRead(analogInPinB);// map it to the range of the analog out:outputValueR = map(sensorValueR, 0, 1023, 0, 255);outputValueG = map(sensorValueG, 0, 1023, 0, 255);outputValueB = map(sensorValueB, 0, 1023, 0, 255);if (outputValueR == 0 && flagR == 1) { //if red is pressed and tone red gone flag is updigitalWrite(outputR, HIGH); //trun on readflagR = 0; // reset flag R to offtoneR = 1; // set tone falg on}else{digitalWrite(outputR, LOW);}if(toneR ==1){ // make a tone for a set amount of time//Red tone 1// play a note on pin 6 for 200 ms:if(countR <= 1000){ //if Red counter is below 1000tone(6, 220); // play tone}else{ // else if red counter is above 1000noTone(6); //stop tonecountR = 0; // and reset the Red counter to off}countR++; // increment read counter}if (outputValueG == 0) {digitalWrite(outputG, HIGH);}else {digitalWrite(outputG, LOW);}if (outputValueB == 0) {digitalWrite(outputB, HIGH);}else {digitalWrite(outputB, LOW);}// change the analog out value:// analogWrite(analogOutPin, outputValue);// print the results to the serial monitor:Serial.print("sensorR = " );Serial.print(sensorValueR);Serial.print("\t outputR = ");Serial.print(outputValueR);Serial.print("\t sensorG = " );Serial.print(sensorValueG);Serial.print("\t outputG = ");Serial.print(outputValueG);Serial.print("\t sensorB = " );Serial.print(sensorValueB);Serial.print("\t outputB = ");Serial.println(outputValueB);}Although it compiled ok, it still either doesn't stop the buzzer or it doesn't work at all along with the LED.Many Thanks for you help

author: dleebell
author_rank: Contributor
timestamp: May 14, 2015, 06:00:42 pm
content: HiI have managed to add the flag in correctly to stop the buzzer from repeatedly beeping and keeping the LED's on. I have done with with all RGB colours. I have also added a tone (ToneY) to play when two Red and Green colours show, but now have the problem of when I alternate between the individual colours Green and Red, it plays (ToneY). Basically theres the problem in the serial read where it over laps between sensorR and sensorG.Whole Code:Code:/*Analog input, analog output, serial outputReads an analog input pin, maps the result to a range from 0 to 255and uses the result to set the pulsewidth modulation (PWM) of an output pin.Also prints the results to the serial monitor.The circuit:* potentiometer connected to analog pin 0.Center pin of the potentiometer goes to the analog pin.side pins of the potentiometer go to +5V and ground* LED connected from digital pin 9 to groundcreated 29 Dec. 2008modified 9 Apr 2012by Tom IgoeThis example code is in the public domain.*/// These constants won't change. They're used to give names// to the pins used:const int analogInPinR = A0; // Analog input pin that the potentiometer is attached toconst int analogInPinG = A1; // Analog input pin that the potentiometer is attached toconst int analogInPinB = A2; // Analog input pin that the potentiometer is attached toint sensorValueR = 0; // value read from the potint sensorValueG = 0; // value read from the potint sensorValueB = 0; // value read from the potint sensorValueR1 = 0;int sensorValueG1 = 0;int sensorValueB1 = 0;int sensorValueR2 = 0;int sensorValueG2 = 0;int sensorValueB2 = 0;int outputValueR = 0; // value output to the PWM (analog out)int outputValueG = 0; // value output to the PWM (analog out)int outputValueB = 0; // value output to the PWM (analog out)int outputRpin = 10;int outputGpin = 11;int outputBpin = 12;int flagR = 1;int toneR = 0;int flagY = 1;int toneY = 0;int flagG = 1;int toneG = 0;int flagB = 1;int toneB = 0;void setup() {// initialize serial communications at 9600 bps:Serial.begin(9600);pinMode(outputRpin, OUTPUT);pinMode(outputGpin, OUTPUT);pinMode(outputBpin, OUTPUT);}void loop() {// read the analog in value:sensorValueR1 = analogRead(analogInPinR);delay(10);sensorValueR2 = analogRead(analogInPinR);sensorValueG1 = analogRead(analogInPinG);delay(10);sensorValueG2 = analogRead(analogInPinG);sensorValueB1 = analogRead(analogInPinB);delay(10);sensorValueB2 = analogRead(analogInPinB);sensorValueR = sensorValueR1 + sensorValueR2;sensorValueG = sensorValueG1 + sensorValueG2;sensorValueB = sensorValueB1 + sensorValueB2;sensorValueR = sensorValueR/2;sensorValueG = sensorValueG/2;sensorValueB = sensorValueB/2;// map it to the range of the analog out:outputValueR = map(sensorValueR, 0, 1023, 0, 255);outputValueG = map(sensorValueG, 0, 1023, 0, 255);outputValueB = map(sensorValueB, 0, 1023, 0, 255);if (outputValueR == 0 && flagR == 1 && outputValueG > 0 && outputValueB > 0) { //if red is pressed and tone red gone flag is updigitalWrite(outputRpin, HIGH); //turn on readdigitalWrite(outputGpin, LOW);digitalWrite(outputBpin, LOW);flagR = 0; // reset flag R to offflagY = 1;toneR = 1;flagG = 1;flagB = 1;// set tone flag onint toneStop = 30;int toneStart = 60;for (int i = 0; i < 90; i = i + 1) {if (i < toneStop) {tone(6, 220); // play tone}else if (i > toneStop && i < toneStart) {noTone(6);}else {tone(6, 220);}delay(10);}noTone(6);}if (outputValueB > 0 && flagY == 1 && outputValueG == 0 && outputValueR == 0) { //YELLOWdigitalWrite (outputRpin, HIGH);digitalWrite (outputGpin, HIGH);digitalWrite (outputBpin, LOW);flagR = 1;flagY = 0;toneY = 1;flagG = 1; // reset flag R to offflagB = 1;int toneStop = 15;int toneStart = 20;for (int i = 0; i < 35; i = i + 1) {if (i < toneStop) {tone(6, 311); // play tone}else if (i > toneStop && i < toneStart) {noTone(6);}else {tone(6, 165);}delay(10);}for (int i = 0; i < 35; i = i + 1) {if (i < toneStop) {tone(6, 311); // play tone}else if (i > toneStop && i < toneStart) {noTone(6);}else {tone(6, 165);}delay(10);}noTone(6);}if (outputValueG == 0 && flagG == 1 && outputValueR > 0 && outputValueB > 0) { //GREENdigitalWrite(outputRpin, LOW);digitalWrite(outputGpin, HIGH);digitalWrite(outputBpin, LOW);flagR = 1;flagY = 1;flagG = 0; // reset flag R to offtoneG = 1;flagB = 1; // set tone flag onint toneStop = 25;int toneStart = 30;for (int i = 0; i < 35; i = i + 1) {if (i < toneStop) {tone(6, 330); // play tone}else if (i > toneStop && i < toneStart) {noTone(6);}else {tone(6, 330);}delay(10);}for (int i = 0; i < 20; i = i + 1) {if (i < 10) {tone(6, 523); // play tone}else if (i > 10 && i < 15) {noTone(6);}else {tone(6, 740);}delay(10);}noTone(6);}if (outputValueB == 0 && flagB == 1 && outputValueR > 0 && outputValueG > 0) { //BLUEdigitalWrite(outputRpin, LOW);digitalWrite(outputGpin, LOW);digitalWrite(outputBpin, HIGH);flagR = 1;flagY = 1;flagG = 1;flagB = 0; // reset flag B to offtoneB = 1; // set tone flag onint toneStop = 30;int toneStart = 40;for (int i = 0; i < 90; i = i + 1) {if (i < toneStop) {tone(6, 165); // play tone}else if (i > toneStop && i < toneStart) {noTone(6);}else {tone(6, 330);}delay(10);}noTone(6);}// change the analog out value:// analogWrite(analogOutPin, outputValue);// print the results to the serial monitor:Serial.print("sensorR = " );Serial.print(sensorValueR);Serial.print("\t outputR = ");Serial.print(outputValueR);Serial.print("\t sensorG = " );Serial.print(sensorValueG);Serial.print("\t outputG = ");Serial.print(outputValueG);Serial.print("\t sensorB = " );Serial.print(sensorValueB);Serial.print("\t outputB = ");Serial.println(outputValueB);}i have already tried this below:Code:sensorValueR1 = analogRead(analogInPinR);delay(10);sensorValueR2 = analogRead(analogInPinR);sensorValueG1 = analogRead(analogInPinG);delay(10);sensorValueG2 = analogRead(analogInPinG);sensorValueB1 = analogRead(analogInPinB);delay(10);sensorValueB2 = analogRead(analogInPinB);sensorValueR = sensorValueR1 + sensorValueR2;sensorValueG = sensorValueG1 + sensorValueG2;sensorValueB = sensorValueB1 + sensorValueB2;sensorValueR = sensorValueR/2;sensorValueG = sensorValueG/2;sensorValueB = sensorValueB/2;But it doesn't seem to work.Any help please?