I know this is very simple but i still can't it right. I want to send 1 to the serial monitor to chose case 1 and same for case 2. Am i miss out something?
void loop() {
if (Serial.available() > 0) {
int op = Serial.read();
switch (op){
case 1:
SEND();
break;
case 2:
REC();
break;
default:
Serial.println("Please enter number 1 or 2: ");
}
}
Serial.read() return a char, not an int. so in the switch, use '1' instead of 1, etc.
Edit: sorry, Serial.read return an int, but it will be the ASCII decimal value of the character that was received. Still, the solution is to use '1', or it's ASCII decimal value: 49
guix:
Serial.read() return a char, not an int. so in the switch, use '1' instead of 1, etc.
Oh thanks. But why it skip the line whereby enter a mobile number. Can't i do this way? I want to merge send and receive together.
void SEND(){
Serial.print("Enter a mobile number: ");
char remoteNum[20]; // telephone number to send sms
readSerial(remoteNum);
Serial.println(remoteNum);
// sms text
Serial.print("Now, enter SMS content: ");
char txtMsg[200];
readSerial(txtMsg);
Serial.println("SENDING");
Serial.println();
Serial.println("Message:");
Serial.println(txtMsg);
// send the message
sms.beginSMS(remoteNum);
sms.print(txtMsg);
sms.endSMS();
Serial.println("\nCOMPLETE!\n");
}
/*
Read input serial
*/
int readSerial(char result[])
{
int i = 0;
while(1)
{
while (Serial.available() > 0)
{
char inChar = Serial.read();
if (inChar == '\n')
{
result[i] = '\0';
Serial.flush();
return 0;
}
if(inChar!='\r')
{
result[i] = inChar;
i++;
}
}
}
}
void REC(){
char c;
// If there are any SMSs available()
if (sms.available())
{
Serial.println("Message received from:");
// Get remote number
sms.remoteNumber(senderNumber, 20);
Serial.println(senderNumber);
// An example of message disposal
// Any messages starting with # should be discarded
if(sms.peek()=='#')
{
Serial.println("Discarded SMS");
sms.flush();
}
// Read message bytes and print them
while(c=sms.read())
Serial.print(c);
Serial.println("\nEND OF MESSAGE");
// Delete message from modem memory
sms.flush();
Serial.println("MESSAGE DELETED");
}
delay(1000);
}