I am following the projects in the Arduino starter kit.
I am currently on the 7th project which is a keyboard instrument which requires me to read values using a serial monitor.
However whenever I open the serial monitor on Arduino studio it runs for a second, showing values then freezes the entire program entirely, forcing me to close it. Below is the current code I am using.
int notes[] = {262,294,330,349}; // set the frequencies of the notes used
void setup() {
Serial.begin(9600);
}
void loop() {
int keyVal = analogRead(A0); // read the values from the buttons
Serial.println(keyVal); // print the values
if(keyVal == 1023){
tone(8, notes[0]); // play note 0
}
else if(keyVal >= 990 && keyVal <= 1010){
tone(8, notes[1]); // play note 1
}
else if(keyVal >=505 && keyVal <= 515){
tone(8, notes[2]); // play note 2
}
else if(keyVal >= 3 && keyVal <= 20){
tone(8, notes[3]); // play note 3
}
else{
noTone(8); // If not value is read play no note
}
}
It has happened on all programs that i have tried except one which was I used to test the monitor as shown below.
/* YourDuinoStarter_SerialMonitor_SEND_RCVE
- WHAT IT DOES:
- Receives characters from Serial Monitor
- Displays received character as Decimal, Hexadecimal and Character
- Controls pin 13 LED from Keyboard
- SEE the comments after "//" on each line below
- CONNECTIONS:
- None: Pin 13 built-in LED
-
- V1.00 02/11/13
Questions: terry@yourduino.com */
/*-----( Import needed libraries )-----*/
/*-----( Declare Constants and Pin Numbers )-----*/
#define led 13 // built-in LED
/*-----( Declare objects )-----*/
/*-----( Declare Variables )-----*/
int ByteReceived;
void setup() /****** SETUP: RUNS ONCE ******/
{
Serial.begin(9600);
Serial.println("--- Start Serial Monitor SEND_RCVE ---");
Serial.println(" Type in Box above, . ");
Serial.println("(Decimal)(Hex)(Character)");
Serial.println();
}
//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
if (Serial.available() > 0)
{
ByteReceived = Serial.read();
Serial.print(ByteReceived);
Serial.print(" ");
Serial.print(ByteReceived, HEX);
Serial.print(" ");
Serial.print(char(ByteReceived));
if(ByteReceived == '1') // Single Quote! This is a character.
{
digitalWrite(led,HIGH);
Serial.print(" LED ON ");
}
if(ByteReceived == '0')
{
digitalWrite(led,LOW);
Serial.print(" LED OFF");
}
Serial.println(); // End the line
// END Serial Available
}
}
//--(end main loop )---
/*-----( Declare User-written Functions )-----*/
/*********( THE END )***********/
I think the problem is that the codes I have been using are constantly updating whereas the code I tested it with above, only updates once I send a value using the serial monitor.
With previous codes I have inserted a delay of 1-15 milliseconds but still made no difference.
I have the serial monitor set to 9600 baud rate and tried multiple different end of line settings.