Orijinalini görmek için tıklayınız : 11. Mouse ile led kontrol

13 06 2015, 18:34

Devre Bağlantısı:

Arduino kodu:

/* This program was created by ScottC on 9/5/2012 to receive serial
signals from a computer to turn on/off 1-9 LEDs */

void setup() {
// initialize the digital pins as an output.
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
// Turn the Serial Protocol ON

void loop() {
byte byteRead;

/* check if data has been sent from the computer: */
if (Serial.available()) {

/* read the most recent byte */
byteRead = Serial.read();
//You have to subtract '0' from the read Byte to convert from text to a number.

//Turn off all LEDS
for(int i=2; i<11; i++){
digitalWrite(i, LOW);

//Turn on the relevant LEDs
for(int i=1; i<(byteRead+1); i++){
digitalWrite(i+1, HIGH);

Processing kodu:

//Created by ScottC on 12/05/2012 to send mouse coordinates to Arduino

import processing.serial.*;

// Global variables
int new_sX, old_sX;
int nX, nY;
Serial myPort;

// Setup the Processing Canvas
void setup(){
size( 800, 400 );
strokeWeight( 10 );

//Open the serial port for communication with the Arduino
//Make sure the COM port is correct
myPort = new Serial(this, "COM9", 9600);

// Draw the Window on the computer screen
void draw(){

// Fill canvas grey
background( 100 );

// Set the stroke colour to white

// Draw a circle at the mouse location
ellipse( nX, nY, 10, 10 );

//Draw Line from the top of the page to the bottom of the page
//in line with the mouse.

// Get the new mouse location and send it to the arduino
void mouseMoved(){
nX = mouseX;
nY = mouseY;

//map the mouse x coordinates to the LEDs on the Arduino.

//do nothing
} else {
//only send values to the Arduino when the new X coordinates are different.
old_sX = new_sX;
Orjinal konu: http://temrinlerim.org/index.php?topic=178.0