Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SevenSegmentTM1637.h>
- SevenSegmentTM1637 voltdisplay(6, 5);
- SevenSegmentTM1637 ampdisplay(8, 7);
- int displaybrightness = 0x0F;
- uint8_t psuonpin = 2;//turns on psu
- uint8_t buckpin = 9;//buck mosfet
- uint8_t boostpin = 10;//boost mosfet
- uint8_t a1rot1pin = 3;//first knob first faze(1rot1)
- uint8_t a1rot2pin = 4;//first knob second faze(1rot2)
- uint8_t a2rot1pin = 11;//second knob first faze(2rot1)
- uint8_t a2rot2pin = 12;//second knob second faze(2rot2)
- uint8_t psubuttonpin = 13;//button that turns the psu on
- uint8_t currentbuttonpin = A7;//current knob button
- uint8_t voltagebuttonpin = A6;//voltage knob button
- //knob values
- bool a1rot1 = 0;
- bool a1rot2 = 0;
- bool a2rot1 = 0;
- bool a2rot2 = 0;
- bool a1rot1last = 0;
- bool a1rot2last = 0;
- bool a2rot1last = 0;
- bool a2rot2last = 0;
- bool a1rotbutton;
- bool a2rotbutton = 0;
- bool lastbuttonstatecurrent = 0;
- bool lastbuttonstatevoltage = 0;
- float setvoltage;
- float currentvoltage;
- float setcurrent;
- float currentcurrent;//lol
- int debugcounter;
- void setup() {
- Serial.begin(9600);
- //display initalization
- voltdisplay.init();
- ampdisplay.init();
- voltdisplay.setBacklight(displaybrightness);
- ampdisplay.setBacklight(displaybrightness);
- //io initalization
- pinMode(psuonpin, OUTPUT);
- pinMode(buckpin, OUTPUT);
- pinMode(boostpin, OUTPUT);
- pinMode(a1rot1pin, INPUT);
- pinMode(a1rot2pin, INPUT);
- pinMode(a2rot1pin, INPUT);
- pinMode(a2rot2pin, INPUT);
- pinMode(currentbuttonpin, INPUT);
- pinMode(voltagebuttonpin, INPUT);
- pinMode(psubuttonpin, INPUT);
- digitalWrite(psubuttonpin, INPUT_PULLUP);
- digitalWrite(currentbuttonpin, INPUT_PULLUP);
- digitalWrite(voltagebuttonpin, INPUT_PULLUP);
- //reads current values for both knobs
- a1rot1last=digitalRead(a1rot1pin);
- a2rot1last=digitalRead(a2rot1pin);
- lastbuttonstatecurrent = analogRead(A7);
- //lastbuttonstatevoltage = analogRead(A7);
- Serial.println(analogRead(currentbuttonpin));
- }
- void loop() {
- //checks if knobs have been turned and changes voltage and current accordingly
- //checkvoltageknob();
- checkcurrentknob();
- }
- void setvoltagedisplay(float voltage){//changes values displayed on voltage display
- char charray[4];
- dtostrf(voltage, 3, 1, charray); //seperate each character
- char frs;
- char sec;
- char thr;
- if (String(charray[1])==".")//check if it is two digits or one(by checking where the dot is) and remove dot
- {
- frs = 255;//ascii space
- sec = charray[0];
- thr = charray[2];
- }else{
- frs = charray[0];
- sec = charray[1];
- thr = charray[3];
- }
- String out = String(frs) + String(sec) + String(thr) + "U";//connect it back together without dot(display supports only colon) and add "U"(voltage)
- voltdisplay.print(out);//send to display
- }
- void setcurrentdisplay(float voltage){
- char charray[4];
- dtostrf(voltage, 3, 1, charray);
- char frs;
- char sec;
- char thr;
- if (String(charray[1])=="."){
- frs = 255;
- sec = charray[0];
- thr = charray[2];
- }else{
- frs = charray[0];
- sec = charray[1];
- thr = charray[3];
- }
- String out = String(frs) + String(sec) + String(thr) + "A";
- ampdisplay.print(out);
- }
- void checkcurrentknob(){
- bool currentbuttonstatecurrent;//checks if button is pressed right now
- if(analogRead(currentbuttonpin)<512){currentbuttonstatecurrent=0;}else{currentbuttonstatecurrent=1;}//slims down values becouse i'm using analog pins
- if (currentbuttonstatecurrent == 0 && currentbuttonstatecurrent != lastbuttonstatecurrent){
- a1rotbutton = !a1rotbutton;
- debugcounter++;
- Serial.println(a1rotbutton);
- }
- lastbuttonstatecurrent = currentbuttonstatecurrent;
- a1rot1 = digitalRead(a1rot1pin);
- a1rot2 = digitalRead(a1rot2pin);
- if(a1rot2 != a1rot2last && a1rot2last != 0 && a1rot1==1){
- if (a1rotbutton == 1){setcurrent = setcurrent + 0.1;}else{setcurrent++;}
- setcurrentdisplay(setcurrent);
- }else if(a1rot1 != a1rot1last && a1rot1last != 0 && (a1rot2==1)){
- if (a1rotbutton == 1){setcurrent = setcurrent - 0.1;}else{setcurrent--;}
- setcurrentdisplay(setcurrent);
- }
- a1rot1last = a1rot1;
- a1rot2last = a1rot2;
- }
- //void checkvoltageknob(){
- //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement