Components Required:
1. Arduino Uno
2. Linux PC/Windows PC (Tested with Windows 10, Ubuntu 14.04 LTS)
3. USB cable A to B
4. Breadboard
5. 5 mm LED (your favourite colour)
6. 220 Ohms Resistor (colour code – Red, Red, Brown)
7. Jumper Wires
Way to go ->
1. Connect Arduino Uno to PC via the USB cable.
2. Write the ArduinoReceive.ino sketch in the Arduino IDE, and upload it.
ArduinoReceive.ino
char val; // Data received from the serial port
int ledPin = 13; // Set the pin to digital I/O 13
void setup() {
pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
if (Serial.available())
{ // If data is available to read,
val = Serial.read(); // read it and store it in val
}
if (val == '1')
{ // If 1 was received
digitalWrite(ledPin, HIGH); // turn the LED on
} else {
digitalWrite(ledPin, LOW); // otherwise turn it off
}
delay(10); // Wait 10 milliseconds for next reading
}
button.pde
import processing.serial.*;
Serial myPort;
boolean button = true;
int x = 25;
int y = 25;
int w = 100;
int h = 75;
int r = 10;
void setup() {
size(150, 125); // width, height
String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 9600);
smooth();
}
void draw() {
if (button) {
background(0, 255, 0);
stroke(255, 255, 0);
myPort.write('1');
println("1");
}
else {
background(255, 0, 0);
stroke(255, 255, 0);
myPort.write('0');
println("0");
}
fill(0, 0, 255);
rect(x, y, w, h, r);
}
// When the mouse is pressed, the state of the button is toggled.
// Try moving this code to draw() like in the rollover example. What goes wrong?
void mousePressed() {
if (mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h) {
button = !button;
}
}
Button -> OFF
Button -> ON
Hardware Connections ->
1. LED to Arduino Uno
No comments:
Post a Comment