Author Topic: LED Strip Buzzer break in void loop  (Read 2505 times)

0 Members and 1 Guest are viewing this topic.

Offline dleebellTopic starter

  • Contributor
  • Posts: 19
  • Country: gb
LED Strip Buzzer break in void loop
« on: May 13, 2015, 02:57:36 pm »
Hi

I 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: [Select]
  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
 

Offline electr_peter

  • Supporter
  • ****
  • Posts: 1302
  • Country: lt
Re: LED Strip Buzzer break in void loop
« Reply #1 on: May 13, 2015, 03:55:04 pm »
Did you turn off LED in main loop? Like this
Code: [Select]
digitalWrite(outputR, LOW);
 

Offline mikerj

  • Super Contributor
  • ***
  • Posts: 3240
  • Country: gb
Re: LED Strip Buzzer break in void loop
« Reply #2 on: May 13, 2015, 05:15:46 pm »
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.
 

Offline dleebellTopic starter

  • Contributor
  • Posts: 19
  • Country: gb
Re: LED Strip Buzzer break in void loop
« Reply #3 on: May 13, 2015, 06:49:26 pm »
Hi

Yes 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: [Select]
/*
  Analog input, analog output, serial output

 Reads an analog input pin, maps the result to a range from 0 to 255
 and 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 ground

 created 29 Dec. 2008
 modified 9 Apr 2012
 by Tom Igoe

 This 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 to
const int analogInPinG = A1;  // Analog input pin that the potentiometer is attached to
const int analogInPinB = A2;  // Analog input pin that the potentiometer is attached to

int sensorValueR = 0;       // value read from the pot
int sensorValueG = 0;        // value read from the pot
int sensorValueB = 0;        // value read from the pot

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 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 up
    digitalWrite(outputR, HIGH);    //trun on read
    flagR = 0;                      // reset flag R to off
    toneR = 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 1000
    tone(6, 220);              // play tone
    }
    else{                      // else if red counter is above 1000
       noTone(6);             //stop tone
       countR = 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
 

Offline dleebellTopic starter

  • Contributor
  • Posts: 19
  • Country: gb
Re: LED Strip Buzzer break in void loop
« Reply #4 on: May 14, 2015, 06:00:42 pm »
Hi

I 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: [Select]
/*
  Analog input, analog output, serial output

 Reads an analog input pin, maps the result to a range from 0 to 255
 and 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 ground

 created 29 Dec. 2008
 modified 9 Apr 2012
 by Tom Igoe

 This 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 to
const int analogInPinG = A1;  // Analog input pin that the potentiometer is attached to
const int analogInPinB = A2;  // Analog input pin that the potentiometer is attached to

int sensorValueR = 0;       // value read from the pot
int sensorValueG = 0;        // value read from the pot
int sensorValueB = 0;       // value read from the pot

int 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 up

    digitalWrite(outputRpin, HIGH);    //turn on read
    digitalWrite(outputGpin, LOW);
    digitalWrite(outputBpin, LOW);
    flagR = 0;     // reset flag R to off
    flagY = 1;
    toneR = 1;
    flagG = 1;
    flagB = 1;
    // set tone flag on
    int 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) { //YELLOW
    digitalWrite (outputRpin, HIGH);
    digitalWrite (outputGpin, HIGH);
    digitalWrite (outputBpin, LOW);
    flagR = 1;
    flagY = 0;
    toneY = 1;
    flagG = 1;                      // reset flag R to off
    flagB = 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) { //GREEN
    digitalWrite(outputRpin, LOW);
    digitalWrite(outputGpin, HIGH);
    digitalWrite(outputBpin, LOW);
    flagR = 1;
    flagY = 1;
    flagG = 0;                      // reset flag R to off
    toneG = 1;
    flagB = 1;                       // set tone flag on
    int 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) { //BLUE
    digitalWrite(outputRpin, LOW);
    digitalWrite(outputGpin, LOW);
    digitalWrite(outputBpin, HIGH);
    flagR = 1;
    flagY = 1;
    flagG = 1;
    flagB = 0;                      // reset flag B to off
    toneB = 1;                      // set tone flag on
    int 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: [Select]
  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?

 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf