• If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

arduino_ejemplo_1

Page history last edited by DFectuoso 15 years, 4 months ago

Aqui pongo el codigo de los dos ejemplos que di con el arduino.... creo que el codigo es facil de entender pero recuerden el codigo que puse Arduino es el que corre en el chip, y el que dice processing se ejecuta en la computadora:

 

 

(en la linea   " port = new Serial(this, Serial.list()[N], 9600);  " )

N debe ser igual al puerto que utilizaran para conectarse al arduino!!!

 

procesing del Oscilo:

 
import processing.serial.*;
Serial port;
String buff = "";
int NEWLINE = 10;
 
// Store the last 64 values received so we can graph them.
int[] values = new int[64];
void setup()
{
  size(512, 256);
  println("Available serial ports:");
  println(Serial.list());
  // Uses the first port in this list (number 0).  Change this to
  // select the port corresponding to your Arduino board.  The last
  // parameter (e.g. 9600) is the speed of the communication.  It
  // has to correspond to the value passed to Serial.begin() in your
  // Arduino sketch.
  port = new Serial(this, Serial.list()[2], 9600);  
  // If you know the name of the port used by the Arduino board, you
  // can specify it directly like this.
  //port = new Serial(this, "COM1", 9600);
}
 
void draw()
{
  background(53);
  stroke(255);
  for (int i = 0; i < 63; i++)
    line(i * 8, 255 - values[i], (i + 1) * 8, 255 - values[i + 1]);
  while (port.available() > 0)
    serialEvent(port.read());
}
 
void serialEvent(int serial)
{
  if (serial != NEWLINE) {
    // Store all the characters on the line.
    buff += char(serial);
  } else {
    // The end of each line is marked by two characters, a carriage
    // return and a newline.  We're here because we've gotten a newline,
    // but we still need to strip off the carriage return.
    buff = buff.substring(0, buff.length()-1);
 
    // Parse the String into an integer.  We divide by 4 because
    // analog inputs go from 0 to 1023 while colors in Processing
    // only go from 0 to 255.
    int val = Integer.parseInt(buff)/4;
 
    // Clear the value of "buff"
    buff = "";
 
    // Shift over the existing values to make room for the new one.
    for (int i = 0; i < 63; i++)
      values[i] = values[i + 1];
 
    // Add the received value to the array.
    values[63] = val;
  }
}
 
Codigo en el arduino para el oscilo:
 
void setup()
{
  Serial.begin(9600);
}
 
void loop()
{
  Serial.println(analogRead(0));
  delay(20);
}
 
 
 

Codigo para processing del mouse xy:
 
 
 
// Dimmer - sends bytes over a serial port
// by David A. Mellis
// Modded by DFectuoso to send 2 signals instade of 1
import processing.serial.*;
Serial port;
void setup()
{
  size(256, 256);
 
  println("Available serial ports:");
  println(Serial.list());
  port = new Serial(this, Serial.list()[2], 9600);  
}
 
void draw()
{
  for (int i = 0; i < 256; i++) {
    stroke(i);
    line(i, 0, i, 256);
  }
 
  port.write(mouseX);
  port.write(mouseY);
  delay(10);
}
 
 
 
 
arduino para mouse xy
 
int ledPin = 9;
int ledOtro = 6;
 
void setup()
{
  // begin the serial communication
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}
 
void loop()
{
  byte val;
 
  // check if data has been sent from the computer
  if (Serial.available()) {
    // read the most recent byte (which will be from 0 to 255)
    val = Serial.read();
    // set the brightness of the LED
    analogWrite(ledPin, val);
    while (!Serial.available()) {}
    val = Serial.read();
    // set the brightness of the LED
    analogWrite(ledOtro, val);
    
  }
}
 
 

 

Comments (0)

You don't have permission to comment on this page.