Pages: 1 [2]   Go Down
Author Topic: Error with tone library  (Read 1777 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Hello! I call myself the 11 year old idiot. That tells a lot about me. Bye!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
Hello! I call myself the 11 year old idiot. That tells a lot about me. Bye!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
Hello! I call myself the 11 year old idiot. That tells a lot about me. Bye!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

* Passcode CIRCUIT.fzz (12.68 KB - downloaded 11 times.)
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 473
Posts: 18695
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 473
Posts: 18695
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
Hello! I call myself the 11 year old idiot. That tells a lot about me. Bye!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#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:
#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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
Hello! I call myself the 11 year old idiot. That tells a lot about me. Bye!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);
}
« Last Edit: September 22, 2013, 06:15:57 pm by Strontium » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 473
Posts: 18695
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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:
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.
Logged

Pages: 1 [2]   Go Up
Jump to: