Turn on a light, or 10. Arduino control over serial using Arduino

Arduino control over serial

This is something I use all the time, so I figured I would share it with you. Controlling an Arduino over serial can be extremely simple if you let it, and extremely useful if you know how to use it. Many languages such as Processing and ActionScript 3 (with a proxy) have ways to communicate over serial, and typically you see people using this so they can display the output of an Arduino, or use firmata to send information to the arduino but end up loosing a lot of programming space and overcomplicating the arduino side. But, you can go the other way and build interfaces that are able to control the arduino without firmata and pretty easily at that.Arduino control over serial

This may sound totally obvious, and it may be, but I’m going to show you anyways. For this example I’m going to use the serial monitor built in to the Arduino software, but you can use anything that will write serial to the Arduino. And, although I’m only showing the hookup for LEDs, bildr is all about simple things that you can easily expand / change to make something really cool. So, not to leave you out in the cold, I’ll talk about next steps and how you could expand on this in a bit.

Keeping it simple

Sending a long serial string in Arduino is super simple ( Serial.print(“Hello”) ) , but reading one is a little more difficult. To simplify things, the commands we will send to the Arduino will all be single character. It may sound limiting, but you have 100 or so usable single characters (about 200 if you use special characters) so you can call that many functions over serial in like 3 lines of code. Later in the article, I will also use the single character calls as an advantage other than just saving coding time. Arduino control over Serial

The core code setup for this is:

if (Serial.available()) {
	char ser = Serial.read();
	//OR
	int ser = Serial.read();
}
Unless otherwise stated, this code is released under the MIT License – Please use, change and share it.

 

What this does is it checks to see if any serial is available in the arduino’s buffer, and if it is, it reads the next available byte as either a character (char) or an integer (int). If you read the serial byte as an int, the read byte will be stored as its ASCII decimal value. So “a” will be read as 97, 0 as 48, 1 as 49 and so on. If they are read as a characters (char), “a” will be read as ‘a’, and numbers will be read as characters as well. So 1 will be read as ‘1’. This is not the number 1, but the character ‘1’. He can not be used as a number, and must be compared as a string, not an int (1 != ‘1’; ‘1’ == ‘1’). – NOTE: in Arduino, the use of single ‘ and double ” quotes are not interchangeable. Single quotes signify a character, while double quotes signify a string.

 

Read more: Turn on a light, or 10. Arduino control over serial

 

Leave a Comment

Your email address will not be published. Required fields are marked *