Hi guys! I have some questions, could you help me?
I am trying to insert an integer value with serial.read(); but the value showed in the console is the ASCII value.
For instance, when I input a 10, the value showed is 49, then 48 (the ASCII values for 1 and 0, respectively). How can I show the DEC value? please help.
I add the code right here.
Thanks in advance
int asd = 0;
void setup() {
Serial.begin(9600);
}
void loop()
{
int asd = int(Serial.read());
Serial.write(asd);
delay(1500);
Hi, thanks for the support friend, but when I tried a error code has appeared, and is this:
Arduino: 1.6.0 (Windows 7), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"
prueba_de_impresion.ino: In function 'void loop()':
prueba_de_impresion.ino:11:22: error: call of overloaded 'write(int&, int)' is ambiguous
prueba_de_impresion.ino:11:22: note: candidates are:
In file included from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Stream.h:26:0,
from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/HardwareSerial.h:29,
from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:223,
from prueba_de_impresion.ino:1:
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:54:12: note: size_t Print::write(const char*, size_t)
size_t write(const char buffer, size_t size) {
^
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:54:12: note: no known conversion for argument 1 from 'int' to 'const char'
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:53:20: note: virtual size_t Print::write(const uint8_t*, size_t)
virtual size_t write(const uint8_t buffer, size_t size);
^
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:53:20: note: no known conversion for argument 1 from 'int' to 'const uint8_t {aka const unsigned char*}'
Error compiling.
This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
Simple serial servo test code that might have some of what you want.
//zoomkat 7-30-10 serial servo test
//type servo position 0 to 180 in serial monitor
// Powering a servo from the arduino usually *DOES NOT WORK*.
String readString;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
Serial.begin(9600);
myservo.attach(9);
Serial.println("servo-test"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the String readString
delay(2); //slow looping to allow buffer to fill with next character
}
if (readString.length() >0) {
Serial.println(readString); //so you can see the captured String
int n = readString.toInt(); //convert readString into a number
Serial.println(n); //so you can see the integer
myservo.write(n);
readString="";
}
}
I'm not a huge fan of the String class, so I rewrote zoomkat's code to use char arrays:
//zoomkat 7-30-10 serial servo test
//type servo position 0 to 180 in serial monitor
// Powering a servo from the arduino usually *DOES NOT WORK*.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
char buffer[6];
void setup() {
Serial.begin(9600);
myservo.attach(9);
Serial.println("servo-test"); // so I can keep track of what is loaded
}
void loop() {
int index;
while (Serial.available()) {
index = Serial.readBytesUntil('\n', buffer, 5); //newline or max of 5 chars
buffer[index] = '\0';
Serial.println(buffer); //so you can see the captured String
int n = atoi(buffer); //convert readString into a number
Serial.println(n); //so you can see the integer
myservo.write(n);
buffer[0] = '\0';
}
}
It appears the two versions function the same, but without the String class, the code size shrinks from 6042 bytes to 4324 bytes. True, the String class brings a lot to the party, but he also eats a lot while he's there.