arduino compilation error - Page 1
Forum section: electronics / general

author: gonejko
author_rank: Newbie
timestamp: January 22, 2024, 06:00:25 pm
content: Hello,I get a compilation error when uploading code to arduinowhere am I going wrongCode:#include <SoftwareSerial.h>// Yazılım seri bağlantı noktasını yapılandırmaSoftwareSerial SIM900(7, 8);// Gelen SMS karakterlerini kaydetmek için değişkenchar incoming_char=0;//Allarm statusbyte allarmSent = LOW;void setup() {// Vin algılama için pin 2'yi giriş olarak ayarlayınpinMode(2,INPUT);// Arduino, SIM900 GSM shield ile 19200 baud hızında iletişim kurarSIM900.begin(19200);// GSM shield ağda oturum açması için zaman verindelay(30000);// SIM900'ü SMS moduna ayarlamak için AT komutuSIM900.print("AT+CMGF=1\");delay(100);sendSMSready();// Modülü, SMS verilerini alındıktan sonra seri çıkışa gönderecek şekilde ayarlayın// SIM900.print("AT+CNMI=2,2,0,0,0\");// delay(100);}void loop(){// GSM kalkanının gönderdiği herhangi bir metni seri monitörde görüntülemeif(SIM900.available() >0) {// Hücresel seri bağlantı noktasından karakteri alincoming_char=SIM900.read();// Gelen karakteri terminale yazdırırSerial.print(incoming_char);}while (digitalRead(2) == HIGH) {}if(digitalRead(2) == LOW && allarmSent == LOW) {// 1" daha bekleyin ve pin 2 zaten DÜŞÜK ise mesaj gönderindelay(1000);if(digitalRead(2) == LOW) sendSMSallarm();}while (digitalRead(2) == LOW) {}if(digitalRead(2) == HIGH && allarmSent == HIGH) sendSMSallarmOFF();}void sendSMSallarm(){// Alıcının cep telefonu numarasını uluslararası formatta ayarlayınSIM900.println("AT + CMGS = \\"+905458587878\\"");delay(100);// SMS mesajını ayarlaSIM900.println("*** test3 ***");delay(100);// AT komutunu ^Z ile sonlandırın, ASCII kodu 26SIM900.println((char)26);delay(100);SIM900.println();// Modüle SMS göndermesi için zaman verindelay(5000);allarmSent = HIGH;}void sendSMSallarmOFF(){// Alıcının cep telefonu numarasını uluslararası formatta ayarlayınSIM900.println("AT + CMGS = \\"+905458587878\\"");delay(100);// SMS mesajını ayarlaSIM900.println("*** test 2 ***");delay(100);// AT komutunu ^Z ile sonlandırın, ASCII kodu 26SIM900.println((char)26);delay(100);SIM900.println();// Modüle SMS göndermesi için zaman verindelay(5000);allarmSent = LOW;}void sendSMSready(){// Alıcının cep telefonu numarasını uluslararası formatta ayarlayınSIM900.println("AT + CMGS = \\"+905458587878\\"");delay(100);// SMS mesajını ayarlaSIM900.println("*** test 1 ***");delay(100);// AT komutunu ^Z ile sonlandırın, ASCII kodu 26SIM900.println((char)26);delay(100);SIM900.println();// Modüle SMS göndermesi için zaman verindelay(5000);}

author: agehall
author_rank: Frequent Contributor
timestamp: January 22, 2024, 06:22:42 pm
content: You have ”\” where you should have “\n”.

author: gonejko
author_rank: Newbie
timestamp: January 22, 2024, 06:35:43 pm
content: Quote from: agehall on January 22, 2024, 06:22:42 pmbut this is a working code why is it giving me an error.also where exactly should I fix in the line.

author: MarkT
author_rank: Frequent Contributor
timestamp: January 22, 2024, 07:12:05 pm
content: SIM900.println("AT + CMGS = \\"+905458587878\\"");For instance this line broken. Backslash confusion?Please include error messages as text, not as an image,

author: pcprogrammer
author_rank: Super Contributor
timestamp: January 22, 2024, 08:15:06 pm
content: Code:SIM900.print("AT+CMGF=1\");This is also an issue. Here the string is not normally terminated because the backslash followed by the " character is used to embed the " character in the string, therefore leaving the string open until the next " character is seen. The \ (backslash) character is an escape character in C. See thiswikipedia article.Normally the compiler will give a line number where it encounters the issue, and in this case you have to backtrack to the error.

author: newbrain
author_rank: Super Contributor
timestamp: January 22, 2024, 08:20:58 pm
content: Quote from: gonejko on January 22, 2024, 06:00:25 pmI gather the intention is to print:Quotewith the number between double quotes.You need to escape the double quote, but a single backslash ( \ ) character is enough.Code:SIM900.println("AT + CMGS = \"+905458587878\"");Writing \\ will generate a backslash in the string, not escape the following character.If, instead, the idea is to pass the string to something that needs its own escaping of double quotes, you need three backslash: the first two generate the one in the output, the third one escape the double quote:Code:SIM900.println("AT + CMGS = \\\"+905458587878\\\"");will generate:QuoteQuote from: gonejko on January 22, 2024, 06:35:43 pmIf it does not compile, it is not working (or not written in C++).Also this is valid C/C++ but probably does not do what you think it does (or, if it does there's no pint to it!):Code:SIM900.print("AT+CMGF=1\");The code above is 100% equivalent to this:Code:SIM900.print("AT+CMGF=1");As a \ immediately followed by a newline is simply deleted from the source during parsing.If a newline is wanted in the string, use the \n escape sequence, as already suggested.

author: newbrain
author_rank: Super Contributor
timestamp: January 22, 2024, 08:29:08 pm
content: Quote from: pcprogrammer on January 22, 2024, 08:15:06 pmNo it's not, please check before giving answers to beginners.That backslash does not appear at all after translation phase 2, where, quoting from ISO/IEC9899:2011, 5.1.1.2 Translation phases:QuoteSame text appears in the C++ standards.Pre-processing tokens - and string literals arepp- are parsed in phase three.

author: pcprogrammer
author_rank: Super Contributor
timestamp: January 23, 2024, 07:31:38 am
content: Quote from: newbrain on January 22, 2024, 08:29:08 pmSorry, you are absolutely right, and now that you pointed this out, I recall it as being a continuation character. Mainly used it in long macro definitions to continue on the next line.Edit: I tried compiling the code on one of my machines with IDE version 1.8.8 and it gives a very direct indication of the actual errors and not just the bit shown in the original post. In hindsight I should have done that before posting my erroneous message. And in all fairness user agehall came to the same faulty conclusion before me.Code:Arduino: 1.8.8 (Linux), Board: "Arduino Nano, ATmega328P (Old Bootloader)"sketch_jan23a:53:3: error: stray '\' in programSIM900.println("AT + CMGS = \\"+905458587878\\"");^sketch_jan23a:53:3: error: stray '\' in programsketch_jan23a:70:3: error: stray '\' in programSIM900.println("AT + CMGS = \\"+905458587878\\"");^sketch_jan23a:70:3: error: stray '\' in programsketch_jan23a:87:3: error: stray '\' in programSIM900.println("AT + CMGS = \\"+905458587878\\"");^sketch_jan23a:87:3: error: stray '\' in program/home/peter/Arduino/sketch_jan23a/sketch_jan23a.ino: In function 'void sendSMSallarm()':sketch_jan23a:53:50: error: expected ')' before string constantSIM900.println("AT + CMGS = \\"+905458587878\\"");^/home/peter/Arduino/sketch_jan23a/sketch_jan23a.ino: In function 'void sendSMSallarmOFF()':sketch_jan23a:70:49: error: expected ')' before string constantSIM900.println("AT + CMGS = \\"+905458587878\\"");^/home/peter/Arduino/sketch_jan23a/sketch_jan23a.ino: In function 'void sendSMSready()':sketch_jan23a:87:49: error: expected ')' before string constantSIM900.println("AT + CMGS = \\"+905458587878\\"");^exit status 1stray '\' in programThis report would have more information with"Show verbose output during compilation"option enabled in File -> Preferences.As an additional pointer to the OP, using the println function for all the prints that need a new line at the end is better then to mix the different methods.