Go Down

Topic: break statements (Read 1 time) previous topic - next topic

Mike44449

#15
Jul 06, 2015, 04:25 am Last Edit: Jul 06, 2015, 04:27 am by Mike44449
like i said, im not very good, i have the 5v going through a 330 ohm which then goes to the pushbuttons and then to their pins.

And by the way, the pushbutton sound keyboard works absolutely fine, my only problem is when i try to use the remote
I think computers are way cool, who else has tricked a rock into thinking by giving it lightning?!

gpop1

well if you want to shorten the code at the beginning you can just delete a whole load of lines and add a for loop like this

Code: [Select]
#include <IRremote.h>
#include <toneAC.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int button1 = A0;
int button2 = A1;
int button3 = A2;
int button4 = A3;
int button5 = 6;
int button6 = 8;
int buzzer = 7;
int led = 7;      //green LED
int led2 = A5;  //red LED
int screen = 40;
int setupDelay = 500;
int loopDelay = 2000;
int RECV_PIN = 13;
int digit = 0;
int buzzerState = LOW;
long previousMillis = 0;
long interval = 1;
IRrecv irrecv(RECV_PIN);
decode_results results;
#define Button0 0xFF6897
#define Button1 0xFF30CF
#define Button2 0xFF18E7
#define Button3 0xFF7A85
#define Button4 0xFF10EF
#define Button5 0xFF38C7
#define Button6 0xFF5AA5
#define Button7 0xFF42BD
#define Button8 0xFF4AB5
#define Button9 0xFF52AD
void setup()
{
  irrecv.enableIRIn();
  lcd.begin(16, 2);
  lcd.print("SOUND BOARD V2.3");
  lcd.setCursor(0, 1);
  lcd.print("Made by Mike P.!");
  delay(5000);
  lcd.setCursor(0, 1);
  lcd.print("Starting Up...  ");
  delay(2750);
  lcd.setCursor(0, 1);
  lcd.print("Please Wait     ");
  delay(setupDelay);
  //repeat the next section 4 times
 for (int i=0; i <= 4; i++){
  lcd.setCursor(12, 1);
  lcd.print(".  ");
  delay(setupDelay);
  lcd.setCursor(12, 1);
  lcd.print("..");
  delay(setupDelay);
  lcd.setCursor(12, 1);
  lcd.print("...");
  delay(setupDelay);
  lcd.setCursor(12, 1);
  lcd.print("   ");
  delay(setupDelay);
}//end of section
  delay(2000);
  lcd.setCursor(0, 1);
  lcd.print("SETTING UP I/O..  ");
  delay(3000);
  pinMode(button1,INPUT);
  pinMode(button2,INPUT);
  pinMode(button3,INPUT);
  pinMode(button4,INPUT);
  pinMode(button5,INPUT);
  pinMode(button6,INPUT);
  pinMode(buzzer,OUTPUT);
  pinMode(led,OUTPUT);
  pinMode(led2,OUTPUT);
  delay(2500);
  lcd.setCursor(0, 1);
  lcd.print("Ready!       ");
  toneAC(440,5);
  delay(500);
  toneAC();
}

Mike44449

#17
Jul 06, 2015, 04:41 am Last Edit: Jul 06, 2015, 04:42 am by Mike44449
thats exactly what i did, and i made a few other optimizations like shortening and while loops, how would i write this in proper syntax:

while(button2 on remote is pressed) {
     tone();
     digitalwrite(led, stuff);
     lcd.print("stuff");
}

I think computers are way cool, who else has tricked a rock into thinking by giving it lightning?!

gpop1

looking at a learning sample I used months ago there was a line after the "if"

if (irrecv.decode(&results))
   {
     irrecv.resume(); // Receive the next value

have you checked the code you got your example from?

Mike44449

no, so i need to add that last line of your code to tell it to look for another input from the remote?
I think computers are way cool, who else has tricked a rock into thinking by giving it lightning?!

gpop1

no idea as I only tried ir once in a test. I presumed you got your code somewhere so it might be worth checking the original code


did notice a few things you need to look at. The break being in the "if" doesn't look right

Code: [Select]
    case Button7:
      if(currentMillis - previousMillis > interval) {
   previousMillis = currentMillis;//this timer resets even if the the next if fails
      if(buzzerState == LOW) {//this if carnt fail as its global set low and not changed that i can see
     toneAC(180);
        digitalWrite(led2,LOW);
        digitalWrite(led,HIGH);
        lcd.clear();
        lcd.print("TRIAL BUTTON 7");
        lcd.setCursor(0, 1);
        lcd.print("180 Hz");
      }
           else//this will never fire as the if will not change to HIGH
           toneAC();
        break;
      }//break is inside a "if" not sure if that will work

el_supremo

There's no way the buttons can be working because you haven't wired them correctly.
If the button is not pushed then its input pin floats. when it is pushed the pin is set HIGH.

You need to wire them so that each pin is either HIGH or LOW.
One way to do this is to simply wire each pin to a button and the other side of the button goes to ground - which means you don't need the resistors. Then in setup where you define pinMode for each pin, use INPUT_PULLUP instead of INPUT. Finally, if you do it this way, it means that a pin will be LOW when it is pushed so that all your tests such as "if (digitalRead(button1) == HIGH) {" will have to be changed to "if (digitalRead(button1) == LOW) {"

Pete
Don't send me technical questions via Private Message.

UKHeliBob

I have not looked no far into the code but stuff like this
Code: [Select]

int button1 = A0;
int button2 = A1;
int button3 = A2;
int button4 = A3;
int button5 = 6;
int button6 = 8;

makes me want to shout USE AN ARRAY !
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

el_supremo

I have this feeling of déjà vu all over again. In what started out as the "Happy Birthday" thread in Exhibition/Gallery I told Mike44449 that there was no way that the code he had posted would compile. He replied that "it works for me". Very soon after that he started a different thread in Programming Questions asking about a compiler error message in exactly the same code which he claimed worked for him! Those two threads were later merged by Nick (probably in response to my report) into "Happy Birthday" in Programming Questions. Mike44449 then deleted every post he had made in those threads, except the first, which now makes that topic impossible to follow.
Now in this thread, I asked him how the buttons are wired and in his response he stated "the pushbutton sound keyboard works absolutely fine".
There is absolutely no way that those buttons will work "absolutely fine" if they are wired as he described.

Another thing that I find odd about all this is that I can't see how the person who apparently wrote the code posted in messages #1 and #2 could be the same person who thinks that "renaming some variables might help" and who also couldn't figure out whether to use "break:" or "break;" (see http://forum.arduino.cc/index.php?topic=334050.0).

To top it all off, he has now posted a slightly modified version of this code as a new thread "Sound Keyboard now with Accessories!" in Exhibition/Gallery http://forum.arduino.cc/index.php?topic=334461.0 as if it is a working version. You'll notice in this new code that he now tests for the pins being LOW instead of HIGH as I suggested here in message #21, which means that "the pushbutton sound keyboard works absolutely fine" was a lie.

I think we are all being led down the garden path. Unless and until there's a rational, believable and acceptable explanation of what's going on here, I'm not going to waste any more of my time on someone who's playing silly buggers. I'd respectfully suggest that those of you who have been trying to help out Mike44449 here and in his other threads do the same.


Pete
Don't send me technical questions via Private Message.

gpop1

lmao that funny in a sad kinda way. im going to be a programming hero come hell or high water





I love this piece of code. why move the break out of the "if" when all you have to do is make sure the if shouldn't  fail

interval is globally set to 1


Code: [Select]
   case Button5:
      if(currentMillis - previousMillis > interval) {
   previousMillis = currentMillis;
      if(buzzerState == LOW) {
     toneAC(280);
        digitalWrite(redLED,LOW);
        digitalWrite(greenLED,HIGH);
        lcd.clear();
        lcd.print("SOUND BOARD V2.3");
        lcd.setCursor(0, 1);
        lcd.print("280 Hz");
      }
           else {
     toneAC();
           }
        break;
      }




nearly crying now.....wtf is this doing in there.

Code: [Select]
  unsigned long currentMillis = millis();
  while(digitalRead(tempButton) == HIGH) {
    int reading = analogRead(tempSensor);
    float voltage = reading * 5.0;
    voltage /= 1024.0;
    lcd.clear();
    lcd.print(voltage); lcd.print("Volts");
    float tempatureC = (voltage - 0.5) * 100 ;
    float tempatureF = (tempatureC * 9.0 / 5.0) + 32.0;
    lcd.setCursor(0, 1);
    lcd.print(tempatureF); lcd.print("Degrees F");
    delay(1000);
  }





but like the man said


2.3
    -IR Support
    -TMP36 Support
    -Much Smaller Code
    -Various Optimizations
    -Uses ToneAC instead of Tone


boy I would love to have seen the original code if this is much smaller......its Optimized im telling ya

ok now im smiling I will steer clear of these posts....

Go Up