Lesson 3 – Code LED PWM

Lesson 3 – Code LED PWM

// By Roee Bloch
//All right Reserved
// Copyright (c) 2015 All Right Reserved, http://www.electronics-freak.com
//
// This source is subject to the Roee Bloch License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Roee Bloch</author>
// <email>roeebloch@walla.co.il</email>
// <date>June 2015</date>

int x = 0;    // variable
int myled;
int incomingByte = 0;   // for incoming serial data

void setup() {
  Serial.begin(9600);      // open the serial port at 9600 bps:
  pinMode(5, OUTPUT);   // sets the pin as output
}

void loop() {
  // print labels
  Serial.print("Hello world \n");       // prints a label
  delay(1000); 
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();
    switch (incomingByte)
    {
      case '1': myled=20;
      break;
      case '2': myled=150;
      break;
      case '3': myled=255;
      break;      
      
    }
    // say what you got:
    Serial.print("I received: ");
    Serial.println(incomingByte);
    Serial.println("I will use value");
    Serial.println(myled);
  }
  analogWrite(5,myled);
  
}

Lesson 3 – this code change brightness of the led according to Serial Port input number
It uses PWM from Arduino which is “Pulse Width Modulation”

[adsenseyu6]