Диммер (Dimmer)

Алгоритмы
Диммер (Dimmer)

Демонстрирует передачу данных с компьютера на плату Arduino, в нашем случае для того, чтобы управлять яркостью светодиода. Данные посылаются разными количествами байтов, каждый из которых колеблется от 0 до 255. Ардуино считывает эти байты и использует их, чтобы определить яркость диода.
Вы можете посылать байты на Arduino с помощью любого программного обеспечения, которое имеет доступ к последовательному порту компьютера. Примеры для Processing и Max/MSP пятой версии показаны ниже.

Схема, цепь:
Диод подключается к 9-му пину. Используйте соответствующий резистор. Для большинства светодиодов, можно обойтись без резистора, поскольку текущий выход цифровых пинов ввода/вывода ограничен.





Код:

 /*
   Dimmer
 Демонстрирует передачу данных с компьютера на плату Arduino, в нашем случае для того, чтобы управлять яркостью светодиода.

created 2006
  by David A. Mellis
  modified 14 Apr 2009
  by Tom Igoe and Scott Fitzgerald
  
  */

 const int ledPin = 9;      // the pin that the LED is attached to

 void setup()
 {
   // initialize the serial communication:
   Serial.begin(9600);
   // initialize the ledPin as an output:
   pinMode(ledPin, OUTPUT);
 }

 void loop() {
   byte brightness;

   // check if data has been sent from the computer:
   if (Serial.available()) {
     // read the most recent byte (which will be from 0 to 255):
     brightness = Serial.read();
     // set the brightness of the LED:
     analogWrite(ledPin, brightness);
   }
 }


Код Processing:

Скопируйте этот код в Processing. Байты с последовательного порта пойдут на Arduino-вскую плату, чтобы сделать темнее яркость диода.

/* Processing code for this example */
  // Dimmer - sends bytes over a serial port
  // by David A. Mellis
  
  import processing.serial.*;
  Serial port;
  
  void setup() {
  size(256, 150);
  
  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()[0], 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() {
  // draw a gradient from black to white
  for (int i = 0; i < 256; i++) {
  stroke(i);
  line(i, 0, i, 150);
  }
  
  // write the current X-position of the mouse to the serial port as
  // a single byte
  port.write(mouseX);
  }
  */


Код Max:

Патч для Max/MSP выглядит следующим образом. Скопируйте текст и вставьте его в новое окно патча.



Processing

Взято с официального сайта
technik 14.12.2009 в 14.13 комментарии 0 0

комментарии(0)

Комментировать

Для добавления комментария авторизируйтесь.
Последние комментарии:
© 2012 pobot.ru