What is the output of this arduino code? - arduino-uno

My Arduino code is:
String myString;
void setup() {
// initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
myString.reserve(26);
myString = "i=";
myString += "1234";
myString += ", is that ok?";
// print the String:
Serial.println(myString);
}
void loop() {
// nothing to do here
}

O/P: "i=1234, is that ok?"
Why so silly Question?
Is there any Special Reason or any math trick?

Related

Send string on serial monitor Arduino tinkercad

I have problem with my code. I wanna send alphanumeric on serial monitor Arduino. I hope someone can help
void setup()
{
Serial.begin(9600);
}
void loop()
{
if ( Serial.available())
{
char datachar = Serial.read();
}
delay(1000)
serial.print(datachar)
}
Your code will not compile because of the following errors:
The statements delay(1000) and serial.print(datachar) are missing the ; at the end.
The variable datachar is declared inside the if block, so it will not be available outside when you call serial.print(datachar).
You misspelled the word serial in serial.print(datachar), it should start with an uppercase letter.
I believe this is what you are looking for:
void setup()
{
Serial.begin(9600);
}
void loop()
{
char datachar;
if(Serial.available())
{
datachar = Serial.read();
}
delay(1000);
Serial.print(datachar);
}

how to solve meaningless characters on serial montior Arduino

Hi i am new at Arduino Uno , i am trying to use imu sensor and read datas from it so i installed mpu9500 lib from arduino website and run one example from its lib. However as i open the serial monitor, it displays strange characters . I check the pin configuration many times and load different libs and examples but i could not fix it.
Here is the codes i run.
#include "MPU9250.h"
MPU9250 mpu;
void setup() {
Serial.begin(115200);
Wire.begin();
delay(2000);
if (!mpu.setup(0x68)) { // change to your own address
while (1) {
Serial.println("MPU connection failed. Please check your connection with `connection_check` example.");
delay(5000);
}
}
}
void loop() {
if (mpu.update()) {
static uint32_t prev_ms = millis();
if (millis() > prev_ms + 25) {
print_roll_pitch_yaw();
prev_ms = millis();
}
}
}
void print_roll_pitch_yaw() {
Serial.print("Yaw, Pitch, Roll: ");
Serial.print(mpu.getYaw(), 2);
Serial.print(", ");
Serial.print(mpu.getPitch(), 2);
Serial.print(", ");
Serial.println(mpu.getRoll(), 2);
}
And this is what i get also i changed value of boud but nothing changed
01:10:23.688 -> ⸮7в⸮⸮⸮⸮⸮⸮`f⸮⸮⸮⸮⸮f⸮⸮⸮~⸮⸮xxf⸮~⸮⸮fx⸮⸮⸮⸮⸮⸮怘⸮
Try running simple Serial print code at the same baud rate(115200) and check if your board is alright. Then run the following code once:
#include "MPU9250.h"
MPU9250 mpu; // You can also use MPU9255 as is
void setup() {
Serial.begin(115200);
Wire.begin();
delay(2000);
mpu.setup(0x68); // change to your own address
}
void loop() {
if (mpu.update()) {
Serial.print(mpu.getYaw()); Serial.print(", ");
Serial.print(mpu.getPitch()); Serial.print(", ");
Serial.println(mpu.getRoll());
}
}

Attiny85 with ArduinoUno for I2c comm

I am working on attiny85 for I2C communication. I have gone through different libraries already like Wire.h, TinyWire.h, tinyWireM.h, tinyWireS.h.
In the start I want to send some byte of data through I2C comm and tried to scope the pin with oscilloscope but its not giving me the appropriate results. Looking on the internet about different ways to make attiny85 work with I2c is really heartless and I could not achieve the task. Finally, I tried to make attiny85 as master and arduino Uno as slave as it was spare in my box.
I tried to make attiny85 as master and send data to arduino and looks the output on serial monitor but its showing zero.
For the reference, the master and slave codes are attached and my task is just simple to check on serial.
Attiny85 as Master
#include <TinyWireM.h>
void setup()
{
TinyWireM.begin();
}
void loop()
{
TinyWireM.begin();
TinyWireM.beginTransmission(0x08);
TinyWireM.send(0x99);
int Byte1 = TinyWireM.endTransmission();
delay(1000);
}
Arduino as Slave
#include <Wire.h>
const byte add = 0x08;
int byte1;
void setup()
{
Wire.begin(add);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
}
void loop()
{
Serial.println ("Data receiving");
Serial.println(byte1);
delay(1000);
}
void receiveEvent(int bytes)
{
byte1 = Wire.read();
}
But I am not able to get the output on serial monitor of arduino.
What am i doing wrong here?
I have used Atiny85 as a slave using TinyWireS lib (https://github.com/nadavmatalon/TinyWireS) some time back and it worked fine.
Below were the pin configurations
ATtiny85 pin 5 with Arduino Uno A4 and
ATtiny85 pin 7 with Arduino Uno A5
Below are my codes
Atiny.
#include "TinyWireS.h"
const byte SLAVE_ADDR = 100;
const byte NUM_BYTES = 4;
volatile byte data = { 0, 1, 2, 3 };
void setup() {
TinyWireS.begin(SLAVE_ADDR);
TinyWireS.onRequest(requestISR);
}
void loop() {}
void requestISR() {
for (byte i=0; i<NUM_BYTES; i++) {
TinyWireS.write(data[i]);
data[i] += 1;
}
}
Uno.
#include <Wire.h>
const byte SLAVE_ADDR = 100;
const byte NUM_BYTES = 4;
byte data[NUM_BYTES] = { 0 };
byte bytesReceived = 0;
unsigned long timeNow = millis();
void setup() {
Serial.begin(9600);
Wire.begin();
Serial.print(F("\n\nSerial is Open\n\n"));
}
void loop() {
if (millis() - timeNow >= 750) { // trigger every 750mS
Wire.requestFrom(SLAVE_ADDR, NUM_BYTES); // request bytes from slave
bytesReceived = Wire.available(); // count how many bytes received
if (bytesReceived == NUM_BYTES) { // if received correct number of bytes...
for (byte i=0; i<NUM_BYTES; i++) data[i] = Wire.read(); // read and store each byte
printData(); // print the received data
} else { // if received wrong number of bytes...
Serial.print(F("\nRequested ")); // print message with how many bytes received
Serial.print(NUM_BYTES);
Serial.print(F(" bytes, but got "));
Serial.print(bytesReceived);
Serial.print(F(" bytes\n"));
}
timeNow = millis(); // mark preset time for next trigger
}
}
void printData() {
Serial.print(F("\n"));
for (byte i=0; i<NUM_BYTES; i++) {
Serial.print(F("Byte["));
Serial.print(i);
Serial.print(F("]: "));
Serial.print(data[i]);
Serial.print(F("\t"));
}
Serial.print(F("\n"));
}

Arduino Serial.write to Processing returning 0?

I've currently got an Arduino program communicating accelerometer values via a Serial event to processing working perfectly. I'm trying to add a thermometer to my setup however processing is only receiving 0 from reading the pin. If I Serial.print the reading in setup it does print to the serial monitor just fine, however I can't get it to send proper values alongside my accelerometer readings.
Arduino code:
int inByte = 0;
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
establishContact(); // send a byte to establish contact until receiver responds
}
void loop() {
if (Serial.available() > 0) {
// get incoming byte:
inByte = Serial.read();
// send sensor values:
Serial.write(analogRead(A3)); // X AXIS
Serial.write(analogRead(A2)); // Y AXIS
Serial.write(analogRead(A1)); // Z AXIS
Serial.write(analogRead(A0)); // TEMPERATURE
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.print('A'); // send a capital A
delay(300);
}
}
Processing code:
import processing.serial.*;
Serial myPort;
int[] serialInArray = new int[4];
int serialCount = 0;
int xInput, yInput, zInput;
float temperature;
boolean firstContact = false;
void setup() {
size(600, 600, P3D);
pixelDensity(2);
noStroke();
background(0);
printArray(Serial.list());
String portName = Serial.list()[4];
myPort = new Serial(this, portName, 9600);
}
void draw() {
}
void serialEvent(Serial myPort) {
// read a byte from the serial port:
int inByte = myPort.read();
// if this is the first byte received, and it's an A,
// clear the serial buffer and note that you've
// had first contact from the microcontroller.
// Otherwise, add the incoming byte to the array:
if (firstContact == false) {
if (inByte == 'A') {
myPort.clear(); // clear the serial port buffer
firstContact = true; // you've had first contact from the microcontroller
myPort.write('A'); // ask for more
}
} else {
// Add the latest byte from the serial port to array:
serialInArray[serialCount] = inByte;
serialCount++;
// If we have 3 bytes:
if (serialCount > 2 ) {
zInput = serialInArray[0]-80;
yInput = serialInArray[1]-80+69;
xInput = serialInArray[2]-77;
temperature = serialInArray[3]; // should return voltage reading (i.e 16ºc = 130);
//println("x = " + xInput + ", y = " + yInput + ", z = " + zInput + ", Temp = " + serialInArray[3]);
// Send a capital A to request new sensor readings:
myPort.write('A');
// Reset serialCount:
serialCount = 0;
}
}
}
The accelerometer values print perfectly, but temperature just returns a 0. Serial.print(analogRead(A0)) in the serial monitor gives me the correct values, so the thermometer is definitely working.
Any help would be greatly appreciated, thank you!
In this line ,
if (serialCount > 2 ) {
change for
if (serialCount >= 4 ) {
OR try to use typecasting or change the temperature for integer !!!
int temperature;

Measuring Time of Flight with Arduino Uno

I am trying to implement a master/slave setup to determine the time of flight between two Arduino Uno boards and ultimately use that as a measure of distance between the two. Using the standard 16MHz crystal and the APC220 from DFRobot communicating between the two is easy but getting a time of flight reading is where I get stuck.
I use the following code on the master side to send the first signal and receive the echo from the slave:
// set pins:
const int switchPin = 3; // pin number of the switch
// variables:
int switchState = 0; // variable for reading switch status
int iDisplay = 1;
unsigned long start, finished, elapsed;
// initialize
void setup()
{
// initialize switch pin as input:
pinMode(switchPin, INPUT);
// initialize serial wireless communication:
Serial.begin(9600);
// read initial state of switch
switchState = digitalRead(switchPin);
}
void displayResult()
{
float h,m,s,ms;
unsigned long over;
elapsed=finished-start;
h=int(elapsed/3600000);
over=elapsed%3600000;
m=int(over/60000);
over=over%60000;
s=int(over/1000);
ms=over%1000;
Serial.print("Raw elapsed time: ");
Serial.println(elapsed);
Serial.print("Elapsed time: ");
Serial.print(h,0);
Serial.print("h ");
Serial.print(m,0);
Serial.print("m ");
Serial.print(s,0);
Serial.print("s ");
Serial.print(ms,0);
Serial.println("ms");
Serial.println();
}
// program loop
void loop()
{
if (Serial.available()>0 && iDisplay == 1)
{
finished=millis();
displayResult();
iDisplay = 0;//Only once
}
// read switch state and print line if state has changed
switch (digitalRead(switchPin)) { // read pin status
case HIGH:
if (switchState == LOW)
{ // check if message has to be sent
start=millis();
delay(200); // for debounce
iDisplay = 1;//Only once
Serial.println(100); // send message about switch
switchState = HIGH; // message has been sent
}
break;
case LOW:
if (switchState == HIGH)
{ // check if message has to be sent
start=millis();
delay(200); // for debounce
iDisplay = 1;//Only once
Serial.println(100); // send message about switch
switchState = LOW; // message has been sent
}
break;
}
}
And the following code for the slave:
// variables:
int intTime = 0;
// initialize
void setup()
{
// initialize serial wireless communication:
Serial.begin(9600);
}
// program loop
void loop()
{
if (Serial.available()>0)
{
intTime = Serial.parseInt();
if (intTime > 1)
{
Serial.println(intTime);
}
intTime = 0;
}
}
Yet this only returns the 200 Milliseconds from the debounce delay, can this be done with the Arduino? Am I getting the math or the code wrong?

Resources