Sunday 18 March 2018

Arduino - Print Commands and the Serial Port

All Arduino boards have at least one serial port (also known as a UART or USART)
It communicates on digital pins 0 (RX) and 1 (TX) as well as with the computer via USB
 
You can use the Arduino environment’s built-in serial monitor to communicate with an Arduino board. Click the serial monitor button in the toolbar and select the same baud rate used in the call to begin().
Serial communication on pins TX/RX uses TTL logic levels (5V or 3.3V depending on the board).
 
The Print command allows us to read info from the Arduino.
   
The Syntax is
Serial.print ()

Serial.print("Hello world.") gives "Hello world."
Serial.print(78) gives "78"
 
This command prints data to the serial port as human-readable ASCII text.
 
An optional second parameter specifies the base (format) to use
ie
permitted values are BIN(binary, or base 2),  
Serial.print(78, BIN) gives "1001110"
 
OCT(octal, or base 8),  
Serial.print(78, OCT) gives "116"
 
DEC(decimal, or base 10),  
Serial.print(78, DEC) gives "78"
 
HEX(hexadecimal, or base 16).
Serial.print(78, HEX) gives "4E"
 
------------
code 1



------------------
// variables
int j=1;
int waitT=300; //delay

void setup()
{
Serial.begin (9600);
  //baud rate
    // this sets up the serial monitor
}

void loop()
{
 Serial.print(j);
  j=j+1;
    delay(waitT);
}
-----------------------------------------------------
code 2


This is really poor programming, as it's printing across the page.
 
the fix is easy
Add "ln"
 Serial.println(j); 















code 3

// variables
int j=1;
int waitT=300; //delay
String myString="j = ";

void setup()
{
Serial.begin (9600);
  //baud rate. Use higher baud rates to make it run faster.
    // this sets up the serial monitor
}

void loop()
{
 Serial.print(myString);
 Serial.println(j); // adding ln ... prints to a new line
  j=j+1;
    delay(waitT);
}

-------------------------------------------------------------------

code 4

// variables
int j=1;
int waitT=300; //delay
int x=3;
int y=7;
int z;
String myString=" + ";

void setup()
{
Serial.begin (9600);
  //baud rate. Use higher baud rates to make it run faster.
    // this sets up the serial monitor
}

void loop()
{
  z=x+y;
  Serial.print(x);
  Serial.print(myString);
  Serial.print(y);
  Serial.print(" = "); // the quotes make it print =
  Serial.println(z);
}

------------------------------------------------------------------------
 ---------------------------------
-------------------------------------

No comments:

Post a Comment