No output from serial monitor - arduino

I'm trying to convert a sensor value to binary to be stored in a small char array (tbuf) and concatenate the small arrays into one bigger array. Ultimately, I want to send the big array (pbuf) filled with 7, 10-bit values over serial with Serial.write(). This is the code that I've tried so far:
void setup() {
Serial.begin(115200);
}
void loop() {
if (Serial.available() > 0) {
int s = 251;
char pbuf[78];
char tbuf[11];
itoa(s, tbuf, 2);
strcat(pbuf, tbuf);
Serial.println(pbuf);
}
Serial.end();
}
It doesn't output the permanent buffer (pbuf), or anything, to the serial monitor. My output to the serial monitor is nothing.
I would appreciate any suggestions on how to get an output from code similar to this.

Don't use Serial.end() in function loop(). Your code runs one time Serial.begin() and if your Serial is not available then you are calling only Serial.end().
Maybe you could try this approach:
int varA =0;
void setup() {
Serial.begin(115200);
}
void loop() {
if (Serial.available() > 0) {
varA = Serial.parseInt(); //or Serial.parseFloat();
Serial.println(varA);
}
}

Related

how to read char array from serial monitor and command arduino accordingly?

currently, I am working on a project to read char inputs from serial monitor and command Arduino to switch on/off specific pins. The problem I am facing is, I am unable to read the complete char array entered in the serial monitor. can anyone tell me what am I doing wrong?
#define X 13 //led pin
char txt[15];
int i;
int Status=0;
void setup() { // put your setup code here, to run once:
pinMode(X,OUTPUT);// setting the pin flow of control as output
Serial.begin(9600);
while(!Serial)
{
; //to wait for pc to connect
}
Serial.println("\nHome Automation");
dashprint();
}
void loop() { // put your main code here, to run repeatedly:
if(Serial.available()>0)
{ i=0;
while(Serial.available()>0) //if serial available
{ char inchar=Serial.read();
txt[i]=inchar; // add char to txt string
i++;// increment to where to write next
txt[i]='\0'; //null termination
}
Serial.print(txt);
check();
}
}
void dashprint() //to print dashes
{
Serial.println("-----------------------------------------------");
Serial.println("give me some command"); //ask for command
}
void check()
{ if(strncmp(txt,"ON",2)==0)
{
digitalWrite(X,HIGH);
Status=1;
}
else if(strncmp(txt,"OFF",3)==0)
{ digitalWrite(X,LOW);
Status=0;
}
else if(txt=="STATUS")
{
}
else Serial.println("ERROR");
}
output:
Home Automation
give me some command
OERROR
NERROR
ERROR
expected output:
Home Automation
give me some command
ON
Your arduino is too fast to read the text "ON" in one round.
9600 is 1 ms per character.
A simple workaround is, to add a little delay
if(Serial.available()>0) {
delay(3); // wait for the whole message
i=0;
while(Serial.available()>0) {
...
ADD:
Additionally, you obviously receive a '\n' (newline) character. Make sure it's not causing troubles.
And increase the delay or have a better approach in general, if you expect more than 3 characters ( "STATUS" )
struct MYObject {char a[256];};
//structure works well with EEPROM put and get functions.
//also lets to input large strings, more then 64 bytes, as http
void setup() {
MYObject str ={" "};
Serial.begin(115200);
while (!Serial.available()) delay (10); //wait for string
int i = 0;
int k= Serial.available();
while (k > 0){
char inchar = Serial.read(); //read one by one character
str.a[i] = inchar;
i++;
if (k < 3) delay (10); //it gives possibility to collect more characters on stream
k = Serial.available();
}
str.a[i]='\0'; //null terminator
//now lets see result
Serial.println(str.a);
//.....
}

Call of overloaded 'println(char [4], int)' is ambiguous

I've two Arduinos and I want to make them communicate via TX/RX. Arduino one as a sender, and Arduino two as a receiver. But I have a problem with the receiver code and get this error:
call of overloaded 'println(char [4], int)' is ambiguous
Can anyone help me? Thank you.
This is my receiver code:
char str[4];
void setup(){
Serial.begin(9600);
Serial.begin(9600);
}
void loop() {
int i=0;
if (Serial.available()){
delay(100); //allows all serial sent to be received together
while(Serial.available() && i<4){
str[i++] = Serial.read();
}
str[i++]='\0';
}
if(i>0){
Serial.println(str, 4);
}
}
Why do you have two Serial.begin(9600) in the setup?
After the while loop, the i var is equal to 4. Your biggest index in str array is str[3] (0-3), so you are accesing the 4th index which doesn't exist.You should remove that str[i++] = '\0', and if you want to make communication you should do something like :
if (i > 0) {
for (int j=0; j<4; j++) {
Serial.print(str[j]);
}
Serial.println("");
}

Basic Arduino serial communication

I'm just trying to get the very basics of serial communication started; I'm trying to use this example I found, from what I understand it should be working. I just want what I type into the serial monitor to be output back, so I can see how it works. I also tried removing the while serial.available in case the serial monitor doesn't trigger that condition.
Here is my code:
// Buffer to store incoming commands from serial port
String inData;
void setup() {
Serial.begin(9600);
Serial.println("Initialized\n");
}
void loop() {
while (Serial.available() > 0)
{
char received = Serial.read();
inData += received;
// Process message when new line character is received
if (received == '\n')
{
Serial.println("Arduino Received: ");
Serial.println(inData);
inData = ""; // Clear received buffer
}
}
}
It currently uploads fine, and prints "initialized", but it doesn't work if I try to "send" any data.
Serial.read() returns an int.
You need to cast to (char) in order to store it as a char.
char received = (char)Serial.read();
Maybe you are never receiving any data for some reason.
Let's try something super simple. Use serialEvent() as suggested by sohnryang and then print some text as soon as Serial.available() triggers:
while (Serial.available() > 0) {
Serial.println("Something has been received");
}
You should see this message every time you send something to Arduino.
Use SerialEvent. So the code will look like this.
String inData;
void setup() {
Serial.begin(9600);
Serial.println("Initialized\n");
}
void loop() {
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inData += inChar;
if (inChar == '\n') {
Serial.println("Arduino Recieved : ");
Serial.println("inData");
inData = "";
}
}
}

Arduino Serial Input to Stop and Start

I am trying to wait for user input to start a program operation and then when the user sends a stop command, the loop stops running. I have been unable to get the Serial port to keep reading an input while the loop is running.
So I want the user to Press 1 and then it'll go into the loop and will display the data from the interrupt. But I want it to keep monitoring the Serial Input so when I type in 2, I will get out of the loop and stop printing to the Serial Port.
The serial port isn't registering my second input.
I left out some of the code, but the important stuff should be there.
int userStart = 0; // Holder for user input to start program
int userStop = 0; // Holder for user input to stop program
void setup() {
Serial.begin(115200);
pinMode(motorEncoderA, INPUT);
digitalWrite(motorEncoderA, HIGH); // Pull up resistor
pinMode(motorEncoderB, INPUT);
digitalWrite(motorEncoderB,HIGH); // Pull up resistor
// Interrupt on change of Pin A
attachInterrupt(digitalPinToInterrupt(2), encoderFunc, CHANGE);
Serial.print("Press 1 to start the Process & 2 to Stop");
}
void loop() {
if (Serial.available() > 0)
{
userStart = Serial.read();
if (userStart = 1) {
Serial.print('\n');
while(userStop != 2) {
unsigned long timee = millis();
// Only update if the shaft has moved
if (encoderPositionLast != rotationCounter) {
Serial.print("Time: ");
Serial.print(timee);
Serial.print(" Count: ");
Serial.print (rotationCounter);
Serial.print('\n');
encoderPositionLast = rotationCounter;
Serial.print(userStart);
}
if (Serial.available() > 0) {
userStop = Serial.read();
Serial.print(userStop);
}
}
}
}
Well, I think your problem is that userStart and userStop should not be 1 and 2, but '1' and '2'.
That said, there are some things in your code I dislike.
First of all why is everybody using int as the base type for all numeric variables? If one single byte is enough, use it. On 32bit machines int and byte are almost the same, but on 8bit ones working with ints wastes space and time.
Secondly, I highly discourage you to block the loop function, otherwise you won-t be able to do anything else. Instead, use a variable to track wheter you are running or not, update it with the serial interface, and then execute the code if you are running.
This code should do it. And IMHO it is much better than blocking the loop:
bool running = false;
void setup()
{
...
running = false;
}
void loop()
{
if (Serial.available() > 0)
{
switch(Serial.read())
{
case '1':
running = true;
Serial.print('\n');
break;
case '2':
running = false;
Serial.print("stopped");
break;
}
}
if (running)
{
unsigned long timee = millis();
// Only update if the shaft has moved
if (encoderPositionLast != rotationCounter) {
Serial.print("Time: ");
Serial.print(timee);
Serial.print(" Count: ");
Serial.print (rotationCounter);
Serial.print('\n');
encoderPositionLast = rotationCounter;
Serial.print("running");
}
}
}

Arduino Serial Communication can't read integers

I used the following code to read sequences of integers from Arduino (Arduino-Mega 2560) serial communication
but it gives me garbage (or wrong value), help me to find out the bug please...
int time=0,i;
void setup() {
pinMode(6,OUTPUT);
Serial.begin(9600);
}
void loop(){
i=0;
while(Serial.available()>0) {
time=Serial.parseInt();
}
for(i=1;i<=time;i++){
digitalWrite(6,1);
delay(150);
digitalWrite(6,0);
delay(100);
}
time=0;
}
Your code is wrong. Or at least the logic is wrong.
Try this:
void loop(){
if(Serial.available()) {
time=Serial.parseInt();
for(i=1;i<=time;i++){
digitalWrite(6,1);
delay(150);
digitalWrite(6,0);
delay(100);
}
time=0;
}
}
Your code looks like it should work, but maybe the problem is in the data you are sending.
Serial.parseInt() expects ascii characters that represent a number. Are you sending a number encoded as a string of chars? Note that Serial.write(99) will send a single byte with value 99.
Since the numbers are in the range 0 to 99, they will fit in a single byte. Why not just send a byte and read the byte on the other end?
void setup() {
pinMode(6,OUTPUT);
Serial.begin(9600);
}
void loop(){
int time=0;
if(Serial.available()>0) { // using while will read all the bytes and use the last one
time = Serial.read();
}
for(int i=1; i<=time; i++){
digitalWrite(6,1);
delay(150);
digitalWrite(6,0);
delay(100);
}
}

Resources