Lesson 3 – Code LED PWM
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
// 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”
Arduino Nano Ebay Link Arduino Nano Arduino Uno Ebay link Arduino Uno Arduino Mega2560 Ebay link Arduino Mega 2560 Arduino Pro-mini ebay link Arduino Pro Mini |