Advertisement
RuiViana

PWM ajustavel

Oct 11th, 2015
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. //http://labdegaragem.com/forum/topics/convertendo-valores-e-jogando-no-lcd
  2. #include <LiquidCrystal.h>
  3. #include <TimerOne.h>
  4. #define botao_incremento A0
  5. #define botao_decremento A1
  6. #define botao_frequencia_incremento A2
  7. #define botao_frequencia_decremento A3
  8. unsigned long freq;
  9. float dspduty;
  10. unsigned int duty;
  11. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  12. // ------------------------------------ setup -------------------------------
  13. void setup()
  14. {
  15. Serial.begin(9600);
  16. lcd.begin(16,2);
  17. lcd.print("BEM VINDOS");
  18. pinMode(A0, INPUT);
  19. pinMode(A1, INPUT);
  20. pinMode(A2, INPUT);
  21. pinMode(A3, INPUT);
  22. pinMode(9, OUTPUT);
  23. }
  24. // ---------------------------------- map floating ------------------------
  25. float map_double(double x, double in_min, double in_max, double out_min, double out_max)
  26. {
  27. return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  28. }
  29. // ------------------------------------ loop -------------------------------
  30. void loop()
  31. {
  32.  
  33. lcd.print (" ");
  34. lcd.setCursor(0,1);
  35. Timer1.initialize(freq);
  36. Timer1.pwm(9, duty);
  37. delay(100);
  38. if(digitalRead(botao_frequencia_incremento) == HIGH)
  39. {
  40. freq = freq +1000;
  41. // Serial.println(freq);
  42. delay(500);
  43. }
  44. if(digitalRead(botao_frequencia_decremento)== HIGH)
  45. {
  46. freq = freq -1000;
  47. // Serial.println(freq);
  48. delay(500);
  49. }
  50. lcd.print (" ");
  51. lcd.setCursor(0,1);
  52. if(digitalRead(botao_incremento) == HIGH)
  53. {
  54. duty = duty +500;
  55. delay(500);
  56. }
  57. if(digitalRead(botao_decremento) == HIGH)
  58. {
  59. duty = duty -500;
  60. delay(500);
  61. }
  62. // Serial.println(duty);
  63. // dspduty = map(duty,0 ,1023,0, 100);
  64. dspduty = map_double(duty,0 ,1023,0, 100);
  65. Serial.println(dspduty,1);
  66. lcd.print(dspduty);
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement