/* * 2017-01-09 * A demonstration on PIN 3 of PWM Out levels. * Uses a "1602" LCD Keypad Shield with 5 button inputs. * Handy for demonstrating oscilloscope use. */ #include LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // pins 2,3 available. // define some values used by the panel and buttons int lcd_key = 0; int adc_key_in = 0; #define btnNONE 0 #define btnRIGHT 1 #define btnUP 2 #define btnDOWN 3 #define btnLEFT 4 #define btnSELECT 5 // read the buttons int getButton() { adc_key_in = analogRead(0); // read the value from the sensor // my buttons when read are centered at these valies: 0, 144, 329, 504, 741 // we add approx 50 to those values and check to see if we are close if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result // For V1.1 us this threshold if (adc_key_in < 50) return btnRIGHT; if (adc_key_in < 250) return btnUP; if (adc_key_in < 450) return btnDOWN; if (adc_key_in < 650) return btnLEFT; if (adc_key_in < 850) return btnSELECT; return btnNONE; // when all others fail, return this... } void setup() { delay(20); lcd.begin(16, 2); // start the library delay(20); lcd.setCursor(0,0); lcd.print("IFL PWM Out"); // print a simple message pinMode(2, OUTPUT); pinMode(3, OUTPUT); } void loop() { static int pwmLevel = 128; static int lastButton = 0; int button = getButton(); if(button == btnUP) pwmLevel++; else if(button == btnDOWN) pwmLevel--; else if(lastButton == btnNONE && button == btnRIGHT) pwmLevel++; else if(lastButton == btnNONE && button == btnLEFT) pwmLevel--; lastButton = button; if(pwmLevel < 0) pwmLevel = 0; else if(pwmLevel > 255) pwmLevel = 255; analogWrite(3, pwmLevel); lcd.setCursor(0,1); lcd.print(millis()/1000); lcd.setCursor(6,1); lcd.print("level "); lcd.print(pwmLevel); lcd.print(" "); delay(10); }