Go Down

Topic: Error with tone library (Read 2396 times) previous topic - next topic

Strontium


How is this all wired up?

http://www.gammon.com.au/switches

I'll start wiring that all up. I never knew that can happen.

Strontium

Alright, so I have added pull down resistors to all my switches (including the button). Now I am getting some very strange behavior.

When I first start it up, it plays a very high pitched noise until I press my "enter" button. Then, it plays the correct note for the correct duration.

My LED is staying on the WHOLE time, and I don't really know why.

When I configure my switches (where the passcode is entered), then press the "enter" button, it does nothing, whether the passcode is wrong or not.

I'll post the new version once I get the Fritzing file edited.

Strontium

Alright, new Fritzing file with the pull-down resistors. (In attachments)

Nick Gammon

Code: [Select]

  pinMode(53, INPUT);
  pinMode(51, INPUT);
  pinMode(49, INPUT);
  pinMode(47, INPUT);
  pinMode(45, INPUT);
  pinMode(52, OUTPUT);
  pinMode(50, OUTPUT);
  pinMode(48, INPUT);


How about some meaningful constants here, like LED for the LED, and so on? This is just guesswork looking at this.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

Code: [Select]

#define enter 48
...
  if (digitalRead (enter) == HIGH) { //Run check process when "enter" is pressed
   ...
      digitalWrite(48, HIGH);



If pin 48, which is an input, is HIGH, write HIGH to it. ?

This is why you need symbols and not sprinkle numbers all through your code.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Strontium


Code: [Select]

#define enter 48
...
  if (digitalRead (enter) == HIGH) { //Run check process when "enter" is pressed
   ...
      digitalWrite(48, HIGH);



If pin 48, which is an input, is HIGH, write HIGH to it. ?

This is why you need symbols and not sprinkle numbers all through your code.


Me being a derp. That should have been the LED pin. I'll label my code and re-post it.

Code: [Select]

#define NOTE_C4  262
int speakerPin = 52;

void setup() {
  pinMode(53, INPUT); //DIP posistion 1
  pinMode(51, INPUT); //DIP position 2
  pinMode(49, INPUT); //DIP position 3
  pinMode(47, INPUT); //DIP position 4
  pinMode(45, INPUT); //DIP position 5
  pinMode(52, OUTPUT); //Piezo Buzzer
  pinMode(50, OUTPUT); //LED
  pinMode(48, INPUT); //"enter" button

}

void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds)  //code for working out the rate at which each note plays and the frequency.
{
  int x;     
  long delayAmount = (long)(1000000/frequencyInHertz);
  long loopTime = (long)((timeInMilliseconds*1000)/(delayAmount*2));
  for (x=0;x<loopTime;x++)   
  {   
    digitalWrite(speakerPin,HIGH);
    delayMicroseconds(delayAmount);
    digitalWrite(speakerPin,LOW);
    delayMicroseconds(delayAmount);
  }   
  delay(20);
}       

void loop() {
  if (digitalRead (48) == HIGH) { //Run check process when "enter" is pressed
    if (digitalRead (53) == HIGH && digitalRead (51) == HIGH && digitalRead (49) == LOW && digitalRead (47) == LOW && digitalRead (45) == LOW) { //Checking the passcode
      digitalWrite(50, HIGH); //Turn on LED
      delay(5000);
      digitalWrite(50, LOW); //Turn off LED
    }
    else {
      beep(speakerPin, NOTE_C4, 500); //Play tone
    }
  }
}   


That may have been part of the issue. I'll try it now.

Strontium

#21
Sep 23, 2013, 01:04 am Last Edit: Sep 23, 2013, 01:15 am by Strontium Reason: 1
Yay! It works now. However, when I start it (and restart it) I am greeted with a high pitches buzz and keeps the LED on until I press enter. I think there is an easy fix for this, though.

I feel like such an idiot.

EDIT: Found a fix. It seems that after a pins type is declared, it sets it to high (for OUTPUTS). So I made it so once setup is complete, it sets them to LOW.
Code: [Select]

void setup() {
  pinMode(53, INPUT); //DIP posistion 1
  pinMode(51, INPUT); //DIP position 2
  pinMode(49, INPUT); //DIP position 3
  pinMode(47, INPUT); //DIP position 4
  pinMode(45, INPUT); //DIP position 5
  pinMode(52, OUTPUT); //Piezo Buzzer
  pinMode(50, OUTPUT); //LED
  pinMode(48, INPUT); //"enter" button
  digitalWrite(50, LOW);
  digitalWrite(52, LOW);
}

Nick Gammon

Code: [Select]

  pinMode(53, INPUT); //DIP posistion 1
  pinMode(51, INPUT); //DIP position 2


I didn't mean with comments. I meant (like you did for "enter")...

Code: [Select]

const byte DIP_SWITCH_1 = 53;
const byte DIP_SWITCH_2 = 51;

...

  pinMode(DIP_SWITCH_1, INPUT);
  pinMode(DIP_SWITCH_2, INPUT);

...

if (digitalRead (DIP_SWITCH_1) == HIGH && digitalRead (DIP_SWITCH_2) == HIGH && ...


And so on.


Whilst comments are nice, they can say anything. Proper constants actually show what the code is doing.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up