Print line in serial monitor of Arduino - arduino

I got a very simple code to run on Arduino. The problem is that when I run the code and open the Serial Monitor then first it shows "Ple" and then adds "Please enter inputs:" which at the end I get "PlePlease enter inputs:"
How can I get rid of the "Ple" or make Serial Monitor not to print before I open it.
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("Please enter inputs:\n");
while(true);
}

Try to open the serial monitor with the shortcut (ctrl+shift+m). It triggers the reset and restarts your program. I don't know why this happens only using the shortcut.
Hope this helps.

it's the same for my latest work... in this case it seems that low baudrate could be the problem.
try to bring the baudrate up to 115200 (don't forget to change it also in the down right corner of serial monitor).
Just to be sure, put a small delay (500ms) just before the Serial Print command. Hope this could be useful also for you!
Ciao.

Related

Arduino SSD1306 Display - Strange Behaviour

I have 2 SSD1306 (126x64) displays that are behaving in a very strange way:
Here is a short video of the sketch I ran: https://streamable.com/dl0p8j
And the sketch itself is here (adafruit SSD1306 + GFX). All of the displays are on I2C port 0x3c.
The one on the left is from a different producer and works fine. I can't figure out if I have defective units or if they need slightly different treatment. When the displays are written to, it seems like the entire image is drawn into the 6-8 pixel area at the top.
I would greatly appreciate if anyone has experience in this. I'm also not sure if this is the right place to post this, so let me know if there is a more fitting SE site.
In order for I2C devices running on the same bus to work, each device need to have its own I2C addr, unfortunately the SSD1306 only has two I2C addresses that you could select, 0x3c or 0x3d.
For more than two SSD1306 running on the same I2C, you will need to either have an Arduino boards that could provided more than one I2C interface or to add some hardware solution like an I2C multiplexer.
In the meantime, if you want to run two displays together, you need to modify the code to create two display instances with different addresses.
Change the line:
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
to:
Adafruit_SSD1306 display1(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_SSD1306 display2(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Change the line:
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
to
if(!display1.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("display 1 failed"));
for(;;);
}
if(!display2.begin(SSD1306_SWITCHCAPVCC, 0x3D)) {
Serial.println(F("display 2 failed"));
for(;;);
}
For all the subsequent calling of display methods, you need to change it to display1 and add another similar line for display2, for example:
display.display();
will need to change to:
dispaly1.display();
display2.display();

Writing to Arduino's serial monitor box

I'm trying to send SMS using GPRS attached to Arduino.
I have this code:
// send an SMS!
char sendto[21], message[141];
flushSerial();
Serial.print(F("Send to #"));
readline(sendto, 20);
Serial.println(sendto);
Serial.print(F("Type out one-line message (140 char): "));
readline(message, 140);
Serial.println(message);
if (!fona.sendSMS(sendto, message)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("Sent!"));
}
I tried to send, and it is working perfect, but I have to use the serial monitor box to enter the number and the message, and the library is using only this method. I want the code to run by itself without entering anything in the box.
Is there is any way so I can do that? I mean something like writing automatically to the box from the code.
Thank you in advance!
Best regards,
Take out the parts of the code that read the line from the Serial monitor and replace with lines of code that either have the data it needs hard-coded or has some other way to get that data. You really haven't shown enough of the code to help you figure out how to do that.

Passing data to arduino through windows command line

Im attempting to play with sending data to my arduino and keep running into the same problem. The code on the arduino is as follows:
void setup()
{
Serial.begin(9600);
for (int i = 3; i <= 13; i++)
{
pinMode(i, OUTPUT);
digitalWrite(i, LOW);
}
}
void loop()
{
if (Serial.available())
{
char ch = Serial.read();
int it = ch - '0';
digitalWrite(it, HIGH);
delay(1000);
digitalWrite(it, LOW);
}
}
This basically makes it so when you send a character over the serial monitor to the device it lights up the light connected to the specified pin for one second.
In the built in serial monitor this works fine, you send the device a number 1-9 (haven't figured out how to do 10+ yet) and the specified light turns on, just as intended. However, my goal is to write a c++ program to send data to the device using the system() command from windows.h. Before i can do that i need the command to send data to the device. I found:
echo i > COM1 //with i being the number to be sent over
Well i tried that and got a fairly interesting result half the time i would receive this message in the command line:
C:/users/XXXXX> echo 7 > COM3 //im 100% sure im using com3
Access is denied
The other half of the time i would see the data go through (The RX light would light up) but nothing would happen, the light connected to pin 7 wouldn't light up. I immedatly thought that you might need to pass the data in ASCII, but nope,
echo 55 > COM3
produced the same result. If any one knows how to send data over window command line to arduio i would really appreciate it, thanks.
Welp after no response here and 7 more hours of research i finally found the solution to my problem here. basically from command line do
powershell //to enter powershell
$port= new-Object System.IO.Ports.SerialPort COM#,Baudrate,None,8,one
//to create a new port object
$port.open() //to open a connection
$port.WriteLine(data)
$port.close()
Looks like the key thing is that you cant just send raw data over to the arduino, you need to first open a connection before with the arduino before it will actually recognize the data as valid serial input.
update:
If you want to run it from command line all you need to do is write a powershell script like so:
$com = $args[0]
$baud = $args[1]
$write = $args[2]
$port = $port= new-Object System.IO.Ports.SerialPort $com,$baud,None,8,one
$port.open()
$port.write($write)
$port.close()
Which can then be called from the command line and have the arguments passed like so:
powershell.exe -ExecutionPolicy Bypass -file filelocation/test.ps1 COM3 2400 7
I had a similar problem.
First, the "Access is denied" error is caused by the Serial Monitor holding the port.
Second, simply "echoing" a string will not work properly, because you also send along the line termination.
The trick is to send something like this:
set /p x="A" <nul >\\.\COM4
Source:
https://batchloaf.wordpress.com/2013/02/12/simple-trick-for-sending-characters-to-a-serial-port-in-windows/

Arduino: serial.read within ISR

I am writing a small test program that attempts to perform a serial.write() followed by a serial.read() within an ISR. The code will eventually be used to prompt an external GSM shield to send an SMS on a regular basis.
ISR(TIMER2_OVF_vect) {
Serial.println("AT+CMGS=\"0123456789\""); // Tell Sim900 To prepare sms to number 01...
while(Serial.read()!='>'); // Wait for Sim900 to respond
Serial.print("A text message"); // the SMS body
Serial.write(0x1A); //Confirm send instruction
Serial.write(0x0D);
Serial.write(0x0A);
}
}
What I have found after a lot of testing is that Serial.read() within an ISR is not capable of reading a live serial prompt, instead it will only read any input that was buffered before the ISR was triggered.
Is there any way around this?
The only solution I have found is to place this code instead within the main loop(). But I want to send the SMS using a timer interrupt.
Thank you
You need to place the code in the loop() but using an IF:
float toBeSent = interval;
loop() {
if (millis() > toBeSent) {
Send();
toBeSent = milli() + interval;
}
}
interval is your sending interval in milliseconds.
I had a similar problem a while ago which I managed to resolve by using the Arduino SoftwareSerial library instead of the hardware based Serial.read.
There are some overheads associated with using SoftwareSerial, and you can only read one port at a time, so I leave it up to those with a better understanding of the Arduino platform to tell you if this is a good idea, but one of the benefits of this library is that you can use it within an ISR.
To use the SoftwareSerial library, add the following two lines of code at the top of your sketch remembering to replqce the rx_pin and tx_pin with the corresponding pin values you want to use:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(rx_pin, tx_pin);
Then replace the key word Serial throughout your sketch with mySerial (or whatever name you have chosen to give your SoftwareSerial instance).
An important thing to keep in mind when using SoftwareSerial is that you can only use certain pins on the Arduino so read the documentation first.
If you wanted to live dangerously you could enable interrupts inside the ISR and use a flag to prevent reentry.
int flag=0;
ISR(TIMER2_OVF_vect) {
flag = 1
if (flag){return;}
sei();
Serial.println("AT+CMGS=\"0123456789\""); // Tell Sim900 To prepare sms to number 01...
while(Serial.read()!='>'); // Wait for Sim900 to respond
Serial.print("A text message"); // the SMS body
Serial.write(0x1A); //Confirm send instruction
Serial.write(0x0D);
Serial.write(0x0A);
}
flag = 0;
}

Strange initial output using Serial.print

When I'm writing to the serial interface, I'm getting strange and unexpected output when my sketches first run. The output seems to be a variant of what should be printed:
eg:
String text1 = "foobar";
void setup() {
Serial.begin(9600);
Serial.print("\n");
Serial.print(text1);
}
void loop() {
}
Results in the output:
fo
foobar
(the new line appears before "fo" but I couldn't work out how to include it).
So some variant of whatever is supposed to be printed, gets printed before the actual text that is supposed to be printed. Changing the output, changes the anomalous text (sometimes it'll be two characters, sometimes three). Making changes that don't affect the output and recompiling has no effect on the anomalous text.
I'm a total Arduino newbie (I only started writing my own code today), but I can only assume this isn't normal.
I'm using a Freetronics EtherTen and the 1.0 IDE
thanks in advance
Arduino is restarting your sketch when you open its serial port on the computer.
so it prints out, and then initialized again.
after
Serial.begin(9600);
try to put either:
delay(500)
or
while (!Serial); // while the serial stream is not open, do nothing:
This is most likely a Serial communication Reset issue as Eran W pointed out. See my previous answer here.
The Arduino automatically resets when it receives serial communication from most things other than the Arduino IDE. This is why you can send from the IDE but not anything else.
I have an Uno and put a capacitor between Reset and Ground.Here's a page with some good info on the subject.
Good luck. http://arduino.cc/playground/Main/DisablingAutoResetOnSerialConnection
You should probably terminate your string with a 0.
Like:
String text1 = "foobar",0;

Resources