Dimmer: Change the brightness of the LEDs by moving the mouse

Source: Internet
Author: User

Original Address-Https://www.arduino.cc/en/Tutorial/Dimmer

Light dimmer

This example shows how to send data from a PC to the Arduino/genuino Development Board to control the brightness of an LED. The data is sent in separate bytes, with a range of 0 to 255 for each number. The program reads these bytes and uses them to set the brightness of the LEDs.

Hardware requirements

Arduino or Genuino Development Board

Led

220 Ohm Resistor

Software Requirements

Processing or

Max/msp version 5

Circuit

Connect a 220-ohm current-limiting resistor to the digital pin 9 and to the LED string. The long pin (anode) of the LED should be connected to the output of the resistor and the short pin (cathode) to GND

Schematic diagram

Code
/* Dimmer controls the brightness of the LEDs by sending data from the computer to the Arduino Development Board. The transmitted data is single-byte and the data range is from 0 to 255. The Arduino reads these byte data and uses them to set the brightness of the LEDs. Circuit: The LED is connected to the digital pin 9 and grounded. Serial port connection to processing, MAX/MSP, or other serial port program. Date written: 2006 compiled by: David A. Mellis update: August 30, 2011 Update: Tom Igoe, Scott Fitzgerald translation Date: December 14, 2016 translator: The Suhamben example is posted on the public network.      Http://www.arduino.cc/en/Tutorial/Dimmer */const int ledpin = 9;  LED Connected pin void Setup () {//Initialize serial connection: Serial.begin (9600); Initialize the LED pin as output: Pinmode (Ledpin, Output);}  void Loop () {byte brightness;    Check if any data is sent from the computer: if (serial.available ()) {//reads the last byte data sent (from 0 to 255): Brightness = Serial.read ();  Set the brightness of the LEDs: Analogwrite (Ledpin, brightness); }}/* This example processing code//dimmer-send data to a serial port//written by David A. Mellis//The code for this example is published on the public network. Import processing.serial.*; Serial Port; void Setup () {size (println); ("Available serial Ports:"); If you use processing 2.1 or later, use Serial.printarray () println (Serial.list ()). Use the first port in this list (ordinal 0). You can change the serial number to select the port that your Arduino is connected to.   The last parameter (for example, 9600) is the speed of the communication, which must correspond to the Serial.begin ()//parameter in your Arduino program. PORT = new Serial (this, serial.list () [0], 9600); If you know the port name that your Arduino board is connected to, you can declare it directly as follows://port = new Serial (This, "COM1", 9600);  } void Draw () {//Draw a black-to-white gradient for (int i = 0; i < n; i++) {Stroke (i); line (i, 0, I, 150);}//To write the current x position of the mouse as a single byte to the serial port Port.write (MouseX); } *//* This example Max/msp V5 patch----------begin_max5_ Patcher----------1008.3ocuxszaiacd9r8uha5rqaehia0aamaavf1s6hdoyqasdil6jqzhq2mywr+2kex4vjnjxkkkkhhigq9meycnz+ x9rnmp63sqvub+ MLa1OlOalSjUvrCymEUytKuh05TKJWUWyk5nE9eSyuS6jesvHu4F4MxOuUzB6X57sPKWVzBLXiPxZtGj6q2vafaaT0.BzJfjj.p8ZPukazsQvpfcpFs8mXR3p Lh8bobxuriowykrxspz0yi.etceh5vqp+wgtfxzmke6czc3ywzwtdcmyw.bbkdiby8v0r+st. Wsd9sdukn8fysppbqvnbnftzwiuylmlejwo0vukzeuj2vpjlawa7yie7wreuifpdfdp1kcbafcp5sjovxp4nb5jq40ougidxjt1wo3gdzhinockhiiexx +owvadoeaksds.rrrooww1arc.9rvn2j9tamwjkcqknvae0l+8wnjhqrenet8whkz6mukik4d+ xknv3jstvjs8eirmmhxsziuset25jxbx8xczil5xpvxhpctgfuxndu9rxtucg37g9q8yc+euofiymg8qdkpcronxsahwys3rwx9pgso+ Pqueg2unqbqwfh1x7qqg+3.vhchrfo1nyr2tlqptm9mdslkncqvz6ko.+sfc5j1ykjjzkn2jwndrp7lvb3d9ltowbaonvb92le6yrmz4uf7ypqhifi7a5ka8zxhkda4r9trgg7v4coisbajkdxrwnhhf0hnuh7uba4mba0l7juk+ OmjDMwkSn95IzrTOwkdp7W.oPRmNRQsiKeu4j3CkfVgt.NYPEYqMGvvJ48vIlPiyzrIuZskWISxGJPcmPiWOfLodybH3wjPbMYwlbFIMNHPHFOtLBNaLSa9sG K1txmzcx5kta6wih2ocxsdngm0qpqfrxyphfsprrhgc9gy9xobjz0nwdr2yw9dua2f85jg2v9fgto4q8qic7fzzqnpmnpsy3brypvjbmjq1uvmoitrhw9nrvg o3nmnzyz+ zs73wtvtonuydg5khmklqaojte7fn2bgsxozdkmrbrgq9j1gonbey0k4gvo8qhccxmfxvihwz6a3yqy9nazzuykua9unynadotogw.jfsvgrvnebwf8i +ehtcwj+wlxqzesdwlo+ fqf6731tva0bisktx.clwmgjsuttvkg1ysnxmxdge.cdr7xd6ymx6fmznaf7kdczmjxwm.xsoordohhna7gmizylzzr.+ 4lconmajp6joz8ftcs1ywhzi3o.sixezx5ihmsuxzztk3ai1mxrsczocs32haydeyxneu5shysxqzqbd3zldera1ipqyxom++ V7susz-----------End_max5_patcher-----------*/
Processing code

The processing program in the sample code sends the bytecode from the computer's serial port to the Development Board to control the LED brightness.

Refer to Max Code

The Max/msp patch in the example above looks like the following picture. Copy and paste into a new patch window.

See Also
    • Serial ()
    • Serial.read ()
    • Analogread ()
    • Analogwrite ()
    • Asciitable-demonstrates the advanced functionality of the Arduino serial output.
    • Graph-transmits data to the computer and uses it to draw within the processing.
    • Midi-sends MIDI tone information in sequence.
    • Multiserialmega-use two serial ports on the Arduino Mega.
    • Physicalpixel-sends data from the processing or Max/msp to the Arduino to light or turn off the LED lights.
    • readasciistring-resolves a comma-delimited number string, which causes the LED to lighten or darken.
    • Serialcallresponse-Multiple variable values are emitted using the call-reply method (handshake).
    • SERIALCALLRESPONSEASCII-emits multiple variable values using a call-response (handshake) and ASCII-encoded before sending.
    • Serial Event: Demonstrates how to use Serialevent ().
    • Virtualcolormixer-sends multiple variable values from the Arduino to the computer and reads them using processing or MAX/MSP.


Final version Edit 2015/07/29 SM

QQ Group

Welcome to our extended family Click to join the hard hit Alliance Arduino group number 8580606

Translation: Hard-hit Alliance translation group-Suzhou and Hangzhou
Audit: Hard-hit Alliance translation Group technical Language audit-magic conch, lepton-, Chengdu Jie

Dimmer: Change the brightness of the LEDs by moving the mouse

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.