Project Description: Sending Hex values to an Arduino UNO
This simple tutorial will show you how to send Hexadecimal values from a computer to an Arduino Uno. The "Processing" programming language will be used to send the HEX values from the computer when a mouse button is pressed. The Arduino will use these values to adjust the brightness of an LED.
Learning Objectives
To Send Hexadecimal (Hex) values from a computer to the Arduino
Trigger an action based on the press of a mouse button
Learn to create a simple Computer to Arduino interface
Use Arduino's PWM capabilities to adjust brightness of an LED
/* ================================================================================================================================================== Project: 5 min tutorial: Send Hex from computer to Arduino Author: Scott C Created: 21th June 2015 Arduino IDE: 1.6.4 Website: http://arduinobasics.blogspot.com/p/arduino-basics-projects-page.html Description: Arduino Sketch used to adjust the brightness of an LED based on the values received on the serial port. The LED needs to be connected to a PWM pin. In this sketch Pin 10 is used, however you could use Pin 3, 5, 6, 9, or 11 - if you are using an Arduino Uno. ===================================================================================================================================================== */
byte byteRead; //Variable used to store the byte received on the Serial Port int ledPin = 10; //LED is connected to Arduino Pin 10. This pin must be PWM capable.
voidsetup() { Serial.begin(9600); //Initialise Serial communication with the computer pinMode(ledPin, OUTPUT); //Set Pin 10 as an Output pin byteRead = 0; //Initialise the byteRead variable to zero. }
voidloop() { if(Serial.available()) { byteRead = Serial.read(); //Update the byteRead variable with the Hex value received on the Serial COM port. }
analogWrite(ledPin, byteRead); //Use PWM to adjust the brightness of the LED. Brightness is determined by the "byteRead" variable. }
Processing Sketch
The latest version of the Processing IDE can be downloaded here.
/* ================================================================================================================================================== Project: 5 min tutorial: Send Hex from computer to Arduino Author: Scott C Created: 21th June 2015 Processing IDE: 2.2.1 Website: http://arduinobasics.blogspot.com/p/arduino-basics-projects-page.html Description: Processing Sketch used to send HEX values from computer to Arduino when the mouse is pressed. The alternating values 0xFF and 0x00 are sent to the Arduino Uno to turn an LED on and off. You can send any HEX value from 0x00 to 0xFF. This sketch also shows how to convert Hex strings to Hex numbers. ===================================================================================================================================================== */
import processing.serial.*; //This import statement is required for Serial communication
Serial comPort; //comPort is used to write Hex values to the Arduino boolean toggle = false; //toggle variable is used to control which hex variable to send String zeroHex = "00"; //This "00" string will be converted to 0x00 and sent to Arduino to turn LED off. String FFHex = "FF"; //This "FF" string will be converted to 0xFF and sent to Arduino to turn LED on.
voidsetup(){ comPort = new Serial(this, Serial.list()[0], 9600); //initialise the COM port for serial communication at a baud rate of 9600. delay(2000); //this delay allows the com port to initialise properly before initiating any communication. background(0); //Start with a black background.
}
voiddraw(){ //the draw() function is necessary for the sketch to compile //do nothing here //even though it does nothing. }
voidmousePressed(){ //This function is called when the mouse is pressed within the Processing window. toggle = ! toggle; //The toggle variable will change back and forth between "true" and "false" if(toggle){ //If the toggle variable is TRUE, then send 0xFF to the Arduino comPort.write(unhex(FFHex)); //The unhex() function converts the "FF" string to 0xFF background(0,0,255); //Change the background colour to blue as a visual indication of a button press. } else { comPort.write(unhex(zeroHex)); //If the toggle variable is FALSE, then send 0x00 to the Arduino background(0); //Change the background colour to black as a visual indication of a button press. } }
The Video
The tutorial above is a quick demonstration of how to convert Hex strings on your computer and send them to an Arduino. The Arduino can use the values to change the brightness of an LED as shown in this tutorial, however you could use it to modify the speed of a motor, or to pass on commands to another module. Hopefully this short tutorial will help you with your project. Please let me know how it helped you in the comments below.
If you like this page, please do me a favour and show your appreciation :
Have you ever wondered if there was a way to store and retrieve data from a USB stick with an Arduino UNO? Most people choose SD cards to store their project data, but you may be surprised there IS a way! IC Station have a nice little module which allows you store and retrieve your Arduino (or other MCU) project data to a USB stick. I am not too sure why USB storage is not widely used in Arduino projects? These modules are not expensive, they have been around for quite a while, and are relatively simple to use. You do not need any libraries to get them to work, however, I must say that documentation for this module is not that easy to find. This site and this document proved to be very useful in my endevour to get this module working, and I hope my tutorial below will help you get started and bridge some of the information gaps. The...
Introduction Nextion is a programmable human machine interface (HMI) that can be customized and designed to simplify the interaction between you and your project. This Nextion Enhanced module (NX4827K043) with a resistive touch screen display, has some additional features not seen in previous traditional versions of the Nextion series. A built in real time clock (RTC) Accessible flash memory (32MB) GPIO functionality Faster clock speed Before you connect the Nextion Enhanced module to your project, you need to design your interface with the free Nextion Editor. The editor can be downloaded here. In this project, I will be designing a simple dynamic interface, which will allow me to interact with a stepper motor in two different ways. The first interface will let me control the direction and speed of the stepper motor through the use of a simple GUI. I will have left and right arrows for the direction, and up and down arrows for the speed. I will also map the Expansion board...
PART THREE If you happened to land on this page and missed PART ONE , and PART TWO , I would advise you go back and read those sections first. This is what you'll find in partone : Downloading and setting up the Android SDK Downloading the Processing IDE Setting up and preparing the Android device Running through a couple of Processing/Android sketches on an Andoid phone. This is what you will find in part two : Introducing Toasts (display messages) Looking out for BluetoothDevices using BroadcastReceivers Getting useful information from a discovered Bluetooth device Connecting to a Bluetooth Device An Arduino Bluetooth Sketch that can be used in this tutorial InputStream and OutputStream We will now borrow some code from the Android developers site to help us to establish communication between the Android phone and the Bluetooth shield on the Arduino. By this stage we have already scanned and discovered the bluetooth device and made a successful connection. We now need to create ...
Comments
Post a Comment