"Arduino Official Tutorial First series" Sample program 3-3 analog signal output [Mega]

Source: Internet
Author: User

analog signal Output [Mega]

This session uses the Arduino mega or Genuino Mega board to allow 12 LEDs to be dimmed one after the other. The reason for using Mega is because it has more pins that support PWM operations.

The hardware required to complete this example
    • Arduino Mega or Genuino mega board
    • 12 x Red LEDs
    • 12 x 220ω Resistors
    • Jumper
    • Bread Plate
    • DuPont Line/Circuit cable
Circuit Connection

The 12 LEDs of the anode (foot) are connected to the 2-13 pin via the 220ω current limiting resistor respectively. Connect the cathode (short foot) of the 12 LEDs to GND.

schematic diagram

Routine Code

A for () loop is used in the setup () function to set the operating mode to output for the number 2-13 pin.
Then in the loop () function, there is a nested for loop.
The outermost loop is:

for (int thisPin =lowestPin; thisPin <= highestPin; thisPin++)

The outermost loop will traverse all LEDs. There are two things to do before the outermost loop switches to the next led.

    • First: You need to gradually light up the current LED.
for0255; brightness++) {analogWrite(thisPin, brightness);delay(2);}
    • Second: You need to make the current led fade out
      Each time this loop occurs, the value of the brightness variable is +1, and the value is written to the pin. When the brightness reaches the highest PWM value of 255, the following loop begins to execute:
for2550; brightness--) {analogWrite(thisPin, brightness);delay(2);}

This cycle reduces the value of the brightness variable, which reduces the LED brightness to 0. When the brightness value is 0 o'clock, the outermost for loop continues to loop execution. The next LED will repeat the process.

/ * analog signal output [Mega] This example lets the 2-13-pin LED connect the LEDs one after the other.  This example runs on the Arduino Mega Board and the previous board is not available. Circuit Connection: * LEDs connected from 2 to 13 respectively to the GND code are public. *///constant, used to define the PIN rangeConst intLowestpin =2;Const intHighestpin = -;voidSetup () {//Set PIN number 2nd to:   for(intThispin = Lowestpin; Thispin <= Highestpin;  thispin++) {Pinmode (Thispin, OUTPUT); }}voidLoop () {//Iterate through all pins:   for(intThispin = Lowestpin; Thispin <= Highestpin; thispin++) {//Let LED fade:     for(intBrightness =0; Brightness <255;      brightness++) {analogwrite (thispin, brightness); Delay2); }//Let the LED fade:     for(intBrightness =255; Brightness >=0;      brightness--) {analogwrite (thispin, brightness); Delay2); }//Between LED gradients delay:Delay -); }}
Related Information

for ()
Analogwrite ()
Delay ()
Analog signal and serial port: read a value from the analog input pin and use this value to control the LED light
Analog signal Input: Use a potentiometer to control the blinking of the LEDs.
Analog output [Mega]: Use the Arduino Mega board to allow 12 LEDs to fade in one after the other.
Analog signal Calibration: Set the maximum and minimum value of the sensor output analog signal to achieve the desired effect.
LED brightness fade: Use the pulse width pin (PWM pin) to weaken the brightness of an LED.
Signal smoothing: Enables more uniform and smooth input values for multiple analog pins.

Update process
Date Log Translator
May 9, 2016 Create and translate documents Tang Jiaxun

Translator: Tang Jiaxun
Email: [Email protected]
qq:649196486
Reproduced please specify the source, all because of love.
I hope you can point out your valuable comments, thank you.

"Arduino Official Tutorial First series" Sample program 3-3 analog signal output [Mega]

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.