Advertisement
Guest User

Untitled

a guest
May 27th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.14 KB | None | 0 0
  1. #include <SevenSegmentTM1637.h>
  2. SevenSegmentTM1637 voltdisplay(6, 5);
  3. SevenSegmentTM1637 ampdisplay(8, 7);
  4.  
  5. int displaybrightness = 0x0F;
  6.  
  7. uint8_t psuonpin = 2;//turns on psu
  8. uint8_t buckpin = 9;//buck mosfet
  9. uint8_t boostpin = 10;//boost mosfet
  10. uint8_t a1rot1pin = 3;//first knob first faze(1rot1)
  11. uint8_t a1rot2pin = 4;//first knob second faze(1rot2)
  12. uint8_t a2rot1pin = 11;//second knob first faze(2rot1)
  13. uint8_t a2rot2pin = 12;//second knob second faze(2rot2)
  14. uint8_t psubuttonpin = 13;//button that turns the psu on
  15. uint8_t currentbuttonpin = A7;//current knob button
  16. uint8_t voltagebuttonpin = A6;//voltage knob button
  17.  
  18. //knob values
  19. bool a1rot1 = 0;
  20. bool a1rot2 = 0;
  21. bool a2rot1 = 0;
  22. bool a2rot2 = 0;
  23. bool a1rot1last = 0;
  24. bool a1rot2last = 0;
  25. bool a2rot1last = 0;
  26. bool a2rot2last = 0;
  27. bool a1rotbutton;
  28. bool a2rotbutton = 0;
  29. bool lastbuttonstatecurrent = 0;
  30. bool lastbuttonstatevoltage = 0;
  31.  
  32. float setvoltage;
  33. float currentvoltage;
  34. float setcurrent;
  35. float currentcurrent;//lol
  36.  
  37. int debugcounter;
  38.  
  39. void setup() {
  40.   Serial.begin(9600);
  41.  
  42.   //display initalization
  43.   voltdisplay.init();
  44.   ampdisplay.init();
  45.   voltdisplay.setBacklight(displaybrightness);
  46.   ampdisplay.setBacklight(displaybrightness);
  47.  
  48.   //io initalization
  49.   pinMode(psuonpin, OUTPUT);
  50.   pinMode(buckpin, OUTPUT);
  51.   pinMode(boostpin, OUTPUT);
  52.   pinMode(a1rot1pin, INPUT);
  53.   pinMode(a1rot2pin, INPUT);
  54.   pinMode(a2rot1pin, INPUT);
  55.   pinMode(a2rot2pin, INPUT);
  56.   pinMode(currentbuttonpin, INPUT);
  57.   pinMode(voltagebuttonpin, INPUT);
  58.   pinMode(psubuttonpin, INPUT);
  59.   digitalWrite(psubuttonpin, INPUT_PULLUP);
  60.   digitalWrite(currentbuttonpin, INPUT_PULLUP);
  61.   digitalWrite(voltagebuttonpin, INPUT_PULLUP);
  62.  
  63.   //reads current values for both knobs
  64.   a1rot1last=digitalRead(a1rot1pin);
  65.   a2rot1last=digitalRead(a2rot1pin);
  66.   lastbuttonstatecurrent = analogRead(A7);
  67.   //lastbuttonstatevoltage = analogRead(A7);
  68.     Serial.println(analogRead(currentbuttonpin));
  69. }
  70.  
  71. void loop() {
  72.   //checks if knobs have been turned and changes voltage and current accordingly
  73.   //checkvoltageknob();
  74.   checkcurrentknob();
  75. }
  76.  
  77. void setvoltagedisplay(float voltage){//changes values displayed on voltage display
  78.   char charray[4];
  79.   dtostrf(voltage, 3, 1, charray); //seperate each character
  80.   char frs;
  81.   char sec;
  82.   char thr;
  83.  
  84.   if (String(charray[1])==".")//check if it is two digits or one(by checking where the dot is) and remove dot
  85.   {
  86.   frs = 255;//ascii space
  87.   sec = charray[0];
  88.   thr = charray[2];
  89.   }else{
  90.   frs = charray[0];
  91.   sec = charray[1];
  92.   thr = charray[3];
  93.   }
  94.  
  95.   String out = String(frs) + String(sec) + String(thr) + "U";//connect it back together without dot(display supports only colon) and add "U"(voltage)
  96.  
  97.   voltdisplay.print(out);//send to display
  98. }
  99. void setcurrentdisplay(float voltage){
  100.   char charray[4];
  101.   dtostrf(voltage, 3, 1, charray);
  102.   char frs;
  103.   char sec;
  104.   char thr;
  105.  
  106.   if (String(charray[1])=="."){
  107.   frs = 255;
  108.   sec = charray[0];
  109.   thr = charray[2];
  110.   }else{
  111.   frs = charray[0];
  112.   sec = charray[1];
  113.   thr = charray[3];
  114.   }
  115.  
  116.   String out = String(frs) + String(sec) + String(thr) + "A";
  117.  
  118.   ampdisplay.print(out);
  119. }
  120.  
  121. void checkcurrentknob(){
  122.   bool currentbuttonstatecurrent;//checks if button is pressed right now
  123.   if(analogRead(currentbuttonpin)<512){currentbuttonstatecurrent=0;}else{currentbuttonstatecurrent=1;}//slims down values becouse i'm using analog pins
  124.   if (currentbuttonstatecurrent == 0 && currentbuttonstatecurrent != lastbuttonstatecurrent){
  125.     a1rotbutton = !a1rotbutton;
  126.     debugcounter++;
  127.     Serial.println(a1rotbutton);
  128.   }
  129.   lastbuttonstatecurrent = currentbuttonstatecurrent;
  130.  
  131.   a1rot1 = digitalRead(a1rot1pin);
  132.   a1rot2 = digitalRead(a1rot2pin);
  133.   if(a1rot2 != a1rot2last && a1rot2last != 0 && a1rot1==1){
  134.     if (a1rotbutton == 1){setcurrent = setcurrent + 0.1;}else{setcurrent++;}
  135.       setcurrentdisplay(setcurrent);
  136.   }else if(a1rot1 != a1rot1last && a1rot1last != 0 && (a1rot2==1)){
  137.     if (a1rotbutton == 1){setcurrent = setcurrent - 0.1;}else{setcurrent--;}
  138.     setcurrentdisplay(setcurrent);
  139.   }
  140.   a1rot1last = a1rot1;
  141.   a1rot2last = a1rot2;
  142. }
  143. //void checkvoltageknob(){
  144.  
  145. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement