Ok, das ist schonmal was ganz anderes. Es ist immer gut, wenn man im Ausgangspost den verwendeten Arduino angibt. Da gibt es ja inzwischen eine ganze Menge, und wenn nichts angegeben wird, geht man halt von den 'standard'-Arduinos ( UNO/Nano) aus.
Nun habe ich so ein Board nicht, und dementsprechend auch keinerleic Erfahrung damit.
Da muss man dann schon ein paar Dinge beachten
. Bei Boards mit nativem USB-Port ist wichtig, dass man nach dem Serial.begin auf den Aufbau der Verbindung wartet, wie das in dem Beispielsketch ja auch gemacht wird:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Sonst funktioniert hinterher die serielle Schnittstelle mit hoher Wahrscheinlichkeit nicht.
Und in der Tat, durch das return 0; kann man den Code nicht so einfach nach loop verpflanzen, sondern sollte in einer eigenen Funktion bleiben. Wobei die Funktion readSerial() mMn nicht gerade als Vorbild dienen sollte - um es mal vorsichtig auszudrücken
Dass readSerial() während der Eingabe blockierend ist, muss bei dem Aufbau des Beispiels wohl so bleiben.
Woran merkst DU , dass die Nummer nicht übernommen wird?
Ich habe dein Beispiel mal einfach umd die GSM-Teile 'entschlackt', dass nur noch das Lesen und Ausgeben über die serielle Schnittstelle bleibt, und auf einem Leonardo mit nativem USB getestet:
/*
SMS sender
This sketch, for the MKR GSM 1400 board,sends an SMS message
you enter in the serial monitor. Connect your Arduino with the
GSM shield and SIM card, open the serial monitor, and wait for
the "READY" message to appear in the monitor. Next, type a
message to send and press "return". Make sure the serial
monitor is set to send a newline when you press return.
Circuit:
* MKR GSM 1400 board
* Antenna
* SIM card that can send SMS
created 25 Feb 2012
by Tom Igoe
*/
void setup() {
// initialize serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("SMS Messages Sender");
Serial.println("Test initialized");
}
void loop() {
// enter mobile number
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");*/
}
int readSerial(char result[]) { // Read input serial
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++;
}
}
}
}
Ergibt im seriellen Monitor:
SMS Messages Sender
Test initialized
Enter a mobile number: 0170123456789
Now, enter SMS content: SENDING
Message:
Zu sendende SMS-Nachricht
Enter a mobile number:
P.S. noch ein HInweis zum seriellen Monitor bei Boards mit nativer Schnittstelle: Der serielle Monitor ist nicht in der Lage die USB Verbindung automatisch neu aufzubauen, nachdem er sie einmal verloren hat. Bei Boards mit serieller Schnittstelle wird die USB Verbingung aber erstmal unterbrochen, wenn man da ein Reset macht. Danch geht deshalb auf dem seriellen Monitor erstmal gar nichts mehr. Den musst Du nach einem manuellen Board-Reset neu starten.