PDA

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



bolubeyi
13.Haziran.2015, 18:34
i_NTYyME8Qk

Devre Bağlantısı:
https://temrinlerim.org/images/arduino8.jpg

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
Serial.begin(9600);
}

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.
byteRead=byteRead-'0';

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

if(byteRead>0){
//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);
myPort.bufferUntil('\n');
}

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

// Fill canvas grey
background( 100 );

// Set the stroke colour to white
stroke(255);

// 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.
line(nX,0,nX,height);
}


// 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.
new_sX=(int)map(nX,0,800,0,10);

if(new_sX==old_sX){
//do nothing
} else {
//only send values to the Arduino when the new X coordinates are different.
old_sX = new_sX;
myPort.write(""+new_sX);
}
}
Orjinal konu: https://temrinlerim.org/index.php?topic=178.0