Advertisement
Tarielect

Prac_64A_IR_Rem_dig_variable_5v_dc

Jul 17th, 2018
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.10 KB | None | 0 0
  1. //*************************************************************************
  2. //  Name     : Prac_64A_IR_Rem_dig_variable_5v_dc                         *
  3. //  Author   : Tarilayefa Edward                                          *
  4. //  Notice   : Tari Electronics & Embedded Systems (TEES), 2018.          *
  5. //           : tarielectronics@yahoo.com                                  *
  6. //           : tarielect.edward@gmail.com                                 *
  7. //           : +23408184754883,+23408062251186, Nigeria.                  *
  8. //           : https//:web.facebook.com/groups/teestraining/              *
  9. //           : https//:web.facebook.com/groups/picarduino/                *
  10. //  Date     : 15/07/2018                                                 *
  11. //  Version  : 1.1                                                        *
  12. //  Notes    : A program to digitally control a variable 5v dc supply     *
  13. //           : using an RC5 (Philips infrared) remote control. Arrow up   *
  14. //           : button increases the pwm while arrow down button decreases *
  15. //           : the volum using IRLib and SoftPWM libraries.               *
  16. //  Reference: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx         *
  17. //  Compiler : avrgcc                                                     *
  18. //  IDE      : Arduino 1.8.5                                              *
  19. //  Target   : Atmega 328, Arduino Uno, Nano.                             *
  20. //*************************************************************************
  21. # include <IRLib.h>  //include the ir remote control library
  22. #include <SoftPWM.h> //add the software pwm library
  23. #include <LiquidCrystal.h> //add the lcd library
  24. LiquidCrystal lcd (7, 6, 5, 4, 3, 2); //(rs,en,d4,d5,d6,d7)
  25.  
  26. int feedBackPin = A0; //feedback input pin
  27. int spwmVal = 2; //initialize soft pwm value to 2
  28. float voltsCheck = 0; //voltage check variable
  29. float volts; //output voltage variable
  30. int spwmPin = 11; //software pwm output pin
  31. int spwmBtnUp = 8; //push button to increase (pwm) voltage
  32. int spwmBtnDwn = 9; //push button to decrease (pwm) voltage
  33. int irsigled = 13;  //ir signal led
  34.  
  35. //........................................................................
  36. SOFTPWM_DEFINE_CHANNEL(11, DDRB, PORTB, PORTB3);//soft pwm on Arduino pin 11
  37. SOFTPWM_DEFINE_OBJECT_WITH_PWM_LEVELS(20, 100);//100 pwm levels
  38. //........................................................................
  39. //put these commands before setup for ir remote to work
  40. IRrecv My_Receiver(12); //ir remote sensor on arduino pin 12
  41. IRdecode My_Decoder;
  42. IRdecodeHash My_Hash_Decoder;
  43. //---------------------------------------------------------------------
  44. void setup() //setup function
  45. {
  46.     pinMode(spwmPin, 1);//PWM output pin
  47.     pinMode(spwmBtnUp, INPUT_PULLUP);//button 1
  48.     pinMode(spwmBtnDwn, INPUT_PULLUP);//button 2
  49.     pinMode(irsigled, 1);
  50.     digitalWrite(irsigled, 1); //turn on irsigled
  51.     //-----------------------------------------------------------------
  52.     lcd.begin(16, 2);//initialize a 16x2 char lcd
  53.     lcd.clear(); //clear lcd
  54.     delay(100); //wait for 100ms
  55.     lcd.print(" IR Dig Voltage ");
  56.     //----------- ---------------------------------------------------
  57.     My_Receiver.enableIRIn(); // Start the receiver
  58.     //--------------------------------------------------------------
  59.     Palatis::SoftPWM.begin(1000); // begin with 1000hz pwm frequency
  60.     //--------------------------------------------------------------
  61.     Serial.begin(9600); //initialize serial communication
  62.     Serial.println("IR Remote Controlled Dig Variable 5v dc");
  63.     delay (2000);
  64. }
  65. //---------------------------------------------------------------------
  66. //---------------------------------------------------------------------
  67. void loop() //main loop function
  68. {
  69.     volts = (analogRead(feedBackPin));//read ADC value at A0
  70.     volts = (volts * 5) / 1024;//converting to 5v range
  71.     int upVal = digitalRead(spwmBtnUp); //read voltage up push button
  72.     int dwnVal = digitalRead(spwmBtnDwn); //read voltage down push button
  73.     //-------------------------------------------------------------------
  74.     if ((voltsCheck > (volts + 0.05))|(voltsCheck < (volts - 0.05)))
  75.         //check if voltage change is higher or lower than 0.5 of previous
  76.             //value to avoid fluctuations
  77.     {
  78.         voltsCheck = volts;//store previous value
  79.     }
  80.     //--------------------------------------------------------------
  81.     Palatis::SoftPWM.set(spwmPin, spwmVal);
  82.     //--------------------------------------------------------------
  83.     if (upVal == LOW)//if voltage up button is pressed
  84.     {
  85.         delay(30); //short button debounce delay
  86.         spwmUp(); //increase pwm
  87.     }
  88.     if (dwnVal == LOW)//if voltage down button is pressed
  89.     {
  90.         delay(30);
  91.         spwmDown(); //decrease pwm
  92.     }
  93.     //--------------------------------------------------------------
  94.      ir_remote_dec();
  95.     //--------------------------------------------------------------
  96.     lcd.setCursor(0,1);
  97.     lcd.print("V=");
  98.     lcd.print(volts); //print out voltage value
  99.     lcd.print("v");
  100.     lcd.print(" PWM=");
  101.     lcd.print(spwmVal); //print out pwm value
  102.     lcd.print("%");
  103.     lcd.print("   ");
  104.     lcd.setCursor(9, 0);
  105.     //--------------------------------------------------------------
  106.     Serial.print("pwmVal: ");
  107.     Serial.print(spwmVal); //print out pwm value
  108.     Serial.print("%; ");
  109.     Serial.print("voltage: ");
  110.     Serial.print(volts); //print voltage value
  111.     Serial.println("v");
  112.     delay(200);
  113.  
  114. }
  115. //......................................................................
  116. void spwmUp(){ //function to increase pwm
  117.     spwmVal++; //increase pwm
  118.     if (spwmVal > 99){
  119.         spwmVal = 99; //make sure spwmVal does not go above 99
  120.     }
  121. }
  122. //......................................................................
  123. //......................................................................
  124. void spwmDown(){ //function to decrease pwm
  125.     spwmVal--; //decrease pwm
  126.     if (spwmVal < 0){
  127.         spwmVal = 0; //make sure spwmVal does not go below 0
  128.     }
  129. }
  130. //......................................................................
  131. //......................................................................
  132. void ir_remote_dec() { //ir remote decoding function
  133.     if (My_Receiver.GetResults(&My_Decoder)) {//Puts results in My_Decoder
  134.         My_Hash_Decoder.copyBuf(&My_Decoder);//copy the results to the hash decoder
  135.         My_Decoder.decode();
  136.         sigled(); //call signal led function
  137.         My_Hash_Decoder.decode();
  138.         ir_rem_btn_control(); //call ir rem button control function
  139.         //....................................................................
  140.         Serial.print("Prot: ");
  141.         Serial.print(Pnames(My_Decoder.decode_type)); //remote control type
  142.         Serial.print("; Value: ");
  143.         Serial.println(My_Hash_Decoder.hash, DEC); //value of remote control button pressed in decimal
  144.         //....................................................................
  145.         My_Receiver.resume();  //resume the ir decoding
  146.     }
  147. }
  148. //..........................................................................
  149. //..........................................................................
  150. void ir_rem_btn_control() { //ir rem button control function
  151.     switch (My_Hash_Decoder.hash) { //value remote control button pressed
  152.         //----------------------------------------------------------------
  153.     case 2891014758:;  //arrow up button PHILIPS RC7940 REMOTE CONTROL
  154.         delay(50); //short button debounce delay
  155.         spwmUp(); //increase pwm
  156.         break;       //exit after execution of command
  157.     case 3843267751:;
  158.         delay(50);
  159.         spwmUp();
  160.         break;
  161.         //-------------------------------------------------------------------
  162.     case  2907792379:;  //arrow down button PHILIPS RC7940 REMOTE CONTROL
  163.         delay(50);
  164.         spwmDown(); //decrease pwm
  165.         break;  
  166.     case  3826490130:;
  167.         delay(50);
  168.         spwmDown();
  169.         break;
  170.         //-------------------------------------------------------------------
  171.     }
  172. }
  173. //.........................................................................
  174. //.........................................................................
  175. void sigled() { //signal led function
  176.     //blink led when noise or ir signal is recieved
  177.     digitalWrite(irsigled, 0);  //turn off signal led
  178.     delay(100);  //delay for 100ms
  179.     digitalWrite(irsigled, 1); //turn on signal led
  180. }
  181. //........................................................................
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement