Jump to content

[Version 1] Using Serial.println()


Recommended Posts

I know this is probably old news if you're experienced with Arduino, but in case nobody mentioned it...

You can use Serial.println() and Serial.print() to send messages to Arduino's "Serial Monitor". This works when you build as "USB Keyboard/Mouse" because there is a separate custom HID communication interface that sends to the Serial Monitor, completely independent of whatever you're doing with the Keyboard and Mouse. Of course, when you use a real Arduino or Teensy with USB Serial, the Arduino Serial Monitor listens to an actual COM port (windows) or serial device file (mac, linux).... and on a genuine Arduino board it actually is slow serial from the AVR chip over to a USB-Serial converter chip. On Teensy it's always native USB speed. Currently the serial monitor is broken if you're using the experimental Teensyduino USB Disk, but I'm going to fix it in alpha #3.

If you've been turning the orange LED on and off to see where your code's running, making use of Serial.print() and watching with the Serial Monitor is a much more satisfying experience.

Happy hacking, and please use Ducky responsibly!

Edited by Paul Stoffregen
Link to comment
Share on other sites

Ohhh thats awesome, I guess the Serial.print(); has some opposite option Serial.read();?!?

for communication between the teensy and the comp?

if not what use it it? or purley debugging?

Yes, Serial.read() and Serial.available() can be used to receive data. The usual approach looks like:

if (Serial.available()) {

mybyte = Serial.read();

}

Serial.read() only reads a single byte, so if you want to receive strings or more than 1 byte numbers, you need to call it in a loop and parse the bytes into whatever you need. If there isn't actually a byte received, Serial.read() will return -1. If you want to wait for a byte, you need something like:

while (Serial.available() < 1) { /* wait */

mybyte = Serial.read();

When using USB Serial, which is the only way you can use normal Arduino boards, these can be used to build code which communicate with PC-based code. There's lots of code out there which communicates via serial ports, because for many years (since the 1980's even), using serial communication has been the main way to communicate with microcontrollers.

Over the next several months, I'm planning to add more USB types. It's a LOT of work, especially integrating them nicely into Teensyduino and making them easy to use. USB Disk still has a long way to go, as you can see from 0.9 alpha #2. Still, I'm looking for more ideas.... Joystick/Gamepad and MIDI will almost certainly be the next two, and maybe the Raw HID packet code if I can figure out ways to make it easier (more stuff on the PC side). Any other USB ideas?

Link to comment
Share on other sites

To test your code, Serial.print() is definitely the way to go, but when your running it on someone else's computer, weather for good or evil, there isn't any way to get the serial output, so a led is really the only way to do it, if you want feedback.

Link to comment
Share on other sites

  • 2 months later...
Over the next several months, I'm planning to add more USB types. It's a LOT of work, especially integrating them nicely into Teensyduino and making them easy to use. USB Disk still has a long way to go, as you can see from 0.9 alpha #2. Still, I'm looking for more ideas.... Joystick/Gamepad and MIDI will almost certainly be the next two, and maybe the Raw HID packet code if I can figure out ways to make it easier (more stuff on the PC side). Any other USB ideas?

Can you also make teensy to be recognized as a usb wifi dongle or usb bluetooth dongle? I have seen some projects on your website that have incorporated teensy to work with bluetooth devices, joystick, in drum sets... I think they are great! :) I hope it helps.

Bits1

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...