Interface & Applications Programming
How to make your board talk to your computer.
How to make your board talk to your computer.
The FTDI is popularly used for communication to and from microcontrollers which do not have USB interfaces.
In this assignment we will control a LED light using the computer.
We will need a FTDI board, the echo board made previously and the Processing Software.
The program I used is referenced from Physical Pixel.
First we use Arduino IDE to program the controller, in this case Im using the ATTINY85.
Code reference:
#include <SoftwareSerial.h> SoftwareSerial mySerial(1,2); //tx, rx const int ledPin = 4; // the pin that the LED is attached to int incomingByte; // a variable to read incoming serial data into void setup() { // initialize serial communication: mySerial.begin(9600); pinMode(ledPin, OUTPUT); } void loop() { if (mySerial.available() > 0) { //read data incomingByte = mySerial.read(); if (incomingByte == 'H') { // on led digitalWrite(ledPin, HIGH); } if (incomingByte == 'L') { // off led digitalWrite(ledPin, LOW); } } }
RX and TX stands for Recieve and Transmit. They are used for serial communication. The program reads serial com. If the byte "H" is received, the LED will be turned off and if byte "L" is recieved, the LED will be off.
Once you upload the program via the ISP programmer, you can disconnect the board from the ISP and connect the FTDI and connect it to the computer.
Next, open Processing.
reference code:
import processing.serial.*; float boxX; float boxY; int boxSize = 20; boolean mouseOverBox = false; Serial port; void setup() { size(200, 200); boxX = width / 2.0; boxY = height / 2.0; rectMode(RADIUS); println(Serial.list()); // Open the port that the Arduino board is connected to (in this case #0) // Make sure to open the port at the same speed Arduino is using (9600bps) port = new Serial(this, Serial.list()[0], 9600); } void draw() { background(0); // Test if the cursor is over the box if (mouseX > boxX - boxSize && mouseX < boxX + boxSize && mouseY > boxY - boxSize && mouseY < boxY + boxSize) { mouseOverBox = true; // draw a line around the box and change its color: stroke(255); fill(153); // send an 'H' to indicate mouse is over square: port.write('H'); } else { // return the box to its inactive state: stroke(153); fill(153); // send an 'L' to turn the LED off: port.write('L'); mouseOverBox = false; } // Draw the box rect(boxX, boxY, boxSize, boxSize); }
Here it draws a box, and when your curser is not in the box it transmits "L", turning the LED off. When your cursur is detected in the box, it will write "H", turning on the LED.
Debug the program and the light should turn on whn your cursur is in the box.