Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 18th, 2012  |  syntax: C++  |  size: 1.18 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /* PWM controller. Control PWM with serial input. Display info on LCD.
  2. */
  3.  
  4.  
  5.  // include the library code:
  6. #include <LiquidCrystal.h>
  7.  
  8. // initialize the library with the numbers of the interface pins
  9. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  10.  
  11. volatile int inDutyCycle=0;
  12. volatile int inSer=-1;
  13.  
  14.  void setup(){
  15.    // set up the LCD's number of columns and rows:
  16.   lcd.begin(16, 2);
  17.   // initialize the serial communications:
  18.   Serial.begin(9600);
  19.  }
  20.  
  21.  void loop(){
  22.    inSer = serialReadInt();
  23.    if(inSer>=0 && inSer<=100){
  24.      inDutyCycle=inSer;
  25.      Serial.println(inDutyCycle);
  26.    }
  27.    analogWrite(9, 255*(inDutyCycle/100));
  28.    lcd.clear();
  29.    lcd.write("Duty-Cycle: ");
  30.    lcd.print(inDutyCycle);
  31.    lcd.setCursor(0,1);
  32.    lcd.write("    ");
  33.    delay(1000);
  34.    
  35.  }
  36.  
  37.  
  38.  //Reads integer from serial input
  39.  int serialReadInt(){
  40.    int i, bytesAvailable;
  41.    char input[4];
  42.    if(Serial.available()>0){
  43.        delay(5);
  44.        bytesAvailable = Serial.available(); //number of input bytes
  45.        for(i=0;i<bytesAvailable;i++)
  46.          input[i] = Serial.read();
  47.        input[i]='\0'; //end of string
  48.        return atoi(&input[0]);
  49.    }
  50.    else
  51.      return -1;
  52.  }