
Untitled
By: a guest on
Aug 18th, 2012 | syntax:
C++ | size: 1.18 KB | hits: 12 | expires: Never
/* PWM controller. Control PWM with serial input. Display info on LCD.
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
volatile int inDutyCycle=0;
volatile int inSer=-1;
void setup(){
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// initialize the serial communications:
Serial.begin(9600);
}
void loop(){
inSer = serialReadInt();
if(inSer>=0 && inSer<=100){
inDutyCycle=inSer;
Serial.println(inDutyCycle);
}
analogWrite(9, 255*(inDutyCycle/100));
lcd.clear();
lcd.write("Duty-Cycle: ");
lcd.print(inDutyCycle);
lcd.setCursor(0,1);
lcd.write(" ");
delay(1000);
}
//Reads integer from serial input
int serialReadInt(){
int i, bytesAvailable;
char input[4];
if(Serial.available()>0){
delay(5);
bytesAvailable = Serial.available(); //number of input bytes
for(i=0;i<bytesAvailable;i++)
input[i] = Serial.read();
input[i]='\0'; //end of string
return atoi(&input[0]);
}
else
return -1;
}