The setup:
An ATtiny85 is programmed using the Arduino IDE, it is supposed to read the ambient light from an LDR and turn on/off an LED accordingly. To get a better understanding of what my sensor readings are in different lighting situations I'd like to send the reading to my serial console using TinyDebug
, a part of the Arduino-on-ATtiny libraries.
Here's the minimum sketch:
String myString;
void setup() {
pinMode(0, OUTPUT);
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(2);
String myString = String(sensorValue);
Serial.write(sensorValue);
Serial.write("\n\r");
if (sensorValue > 500) {
digitalWrite(0, HIGH);
}
else {
digitalWrite(0, LOW);
}
delay(500);
}
On my serial console all I receive is garbage:
l
x
x
l
{
t
n
z
u
l
|
q
m
n
o
(...)
Changing the codes Serial.write(sensorValue);
to Serial.write("hello");
nicely prints out
hello
hello
hello
(...)
so I suppose the serial communication itself works.
Trying to convert the int
value to a string and sending that string, ie Serial.write(myString);
gives a compiler error:
(...)
sketch_may24b.ino: In function 'void loop()':
sketch_may24b:11: error: no matching function for call to 'TinyDebugSerial::write(String&)'
/Users/cts/Documents/Arduino/hardware/tiny/cores/tiny/TinyDebugSerial.h:728: note: candidates are: virtual size_t TinyDebugSerial::write(uint8_t)
/Users/cts/Documents/Arduino/hardware/tiny/cores/tiny/Print.h:75: note: virtual void Print::write(const uint8_t*, size_t)
/Users/cts/Documents/Arduino/hardware/tiny/cores/tiny/Print.h:74: note: virtual void Print::write(const char*)
Any idea how I can send my sensor values to my serial console instead?
utoa();
an option as alternative forString(sensorValue);
? It does require you to declare a buffer beforehand though. Some libraries or functions are not supported on ATtiny, but it is not described which exact ones. Trial and error is proposed. – jippie May 24 '13 at 10:30String myString = String(sensorValue);
only to try and print the integer itself on the next line:Serial.write(sensorValue);
? – jippie May 24 '13 at 10:57Serial.write(&sensorValue);
? Or can you skip the&
(Freaking C++). – Connor Wolf May 24 '13 at 10:58