#include #include #include #define ANALOG_IN_PIN A1 #define ANALOG_IN_PIN_2 A2 //external hardware DHT11 dht11(13); LiquidCrystal_I2C lcd1(0x27, 16, 2); LiquidCrystal_I2C lcd2(0x26, 16, 2); //button controls const int buttonPinIncrement = 2; // Button pin for incrementing value const int buttonPinDecrement = 3; const int buttonPinSwitch = 4; unsigned long lastDebounceTime = 0; int lastButtonState = LOW; // Variable to store the previous state of the button unsigned long lastDebounceTimeSwitch = 0; unsigned long debounceDelaySwitch = 25; // Adjust this value as needed unsigned long debounceDelay = 50; // Adjust this value as needed int buttonState = 0; int value = 2; // Initial value int state = 0; //temperature sensor int F; int H; //pwm control int PWM1 = 11; int PWM = 10; float PWM_voltage_ch1; float PWM_voltage_ch2; float ch1_input = 6; //user input line float ch2_input = 10; //user input line float step = 8.425; //voltage sensor float adc_voltage = 0.0; float adc_voltage2 = 0.0; float in_voltage = 0.0; float in_voltage2 = 0.0; float R1 = 31000.0; float R2 = 7500.0; float ref_voltage = 5.0; float adc_value = 0.0; float adc_value2 = 0.0; //current sensor double Vout = 0; double Vout2 = 0; double Current = 0; double Current2 = 0; const double scale_factor = 0.185; // 5A - Sensitivity for 0B const double vRef = 5.0; const double resConvert = 1024; double resADC = vRef/resConvert; double zeroPoint = 2.505; // the setup routine runs once when you press reset: void setup() { user_inputs(); lcd1.init(); // Initialize the LCD lcd1.backlight(); // Turn on the backlight lcd1.setCursor(0, 0); lcd1.print("CH1:V=0"); lcd1.print(value, 2); lcd1.print(" A=0"); lcd2.init(); // Initialize the LCD lcd2.backlight(); // Turn on the backlight lcd2.setCursor(0, 0); lcd2.print("CH2:V=0"); lcd2.print(value, 2); lcd2.print(" A=0"); pinMode(buttonPinIncrement, INPUT); // Set button pin for incrementing as input pinMode(buttonPinDecrement, INPUT); // Set button pin for decrementing as input pinMode(buttonPinSwitch, INPUT); attachInterrupt(digitalPinToInterrupt(2), Increment_Voltage, RISING); attachInterrupt(digitalPinToInterrupt(3), Decrement_Voltage, RISING); Serial.begin(9600); Serial.println("DC Voltage Test"); // declare pin 9 to be an output: pinMode(PWM, OUTPUT); // declare the button pin as an input: } void state_loop() { buttonState = digitalRead(buttonPinSwitch); if(buttonState == HIGH && state == 0){ state = 1; } else if(buttonState == HIGH && state == 1){ state = 0; } } void loop() { state_loop(); updateLCD(); dht11.update(); F = dht11.readFahrenheit(); // Check if increment button is pressed // Read the Analog Input adc_value = analogRead(ANALOG_IN_PIN); // Determine voltage at ADC input adc_voltage = (adc_value * ref_voltage) / 1024.0; // Calculate voltage at divider input in_voltage = adc_voltage / (R2/(R1+R2)); // Print results to Serial Monitor to 2 decimal places Serial.print("Output Voltage CH1 = "); Serial.print(in_voltage2*.94, 2); Serial.print(","); // Read the Analog Input adc_value2 = analogRead(ANALOG_IN_PIN_2); // Determine voltage at ADC input adc_voltage2 = (adc_value2 * ref_voltage) / 1024.0; // Calculate voltage at divider input in_voltage2 = adc_voltage2 / (R2/(R1+R2)); // Print results to Serial Monitor to 2 decimal places Serial.print("Output Voltage CH2 = "); Serial.print(in_voltage*.94, 2); Serial.println(","); for(int i = 0; i < 400; i++) { Vout = (Vout + (resADC * analogRead(A0))); delay(1); } for(int i = 0; i < 400; i++) { Vout2 = (Vout2 + (resADC * analogRead(A3))); delay(1); } // Get Vout in mv Vout = Vout /1000; Vout2 = Vout2 / 1000; // Convert Vout into Current using Scale Factor Current = (Vout - zeroPoint)/ scale_factor; Current2 = (Vout2 - zeroPoint) / scale_factor; // Print Vout and Current to two Current = "); Serial.print("Output Current CH1 = "); Serial.print(Current * (-1),3); Serial.print(","); Serial.print("Output Current CH2 = "); Serial.print(Current2* (-1),3); Serial.print(","); } float user_inputs() { if (ch1_input <= 8) { PWM_voltage_ch1 = (16.6 * ch1_input) * 1.06; analogWrite(PWM, PWM_voltage_ch1); } else if (ch1_input > 8) { PWM_voltage_ch1 = (234 - (16.85 * (14 - ch1_input))) * 1.06; analogWrite(PWM, PWM_voltage_ch1); } if (ch2_input <= 8) { PWM_voltage_ch2 = (16.6 * ch2_input) * 1.06; analogWrite(PWM1, PWM_voltage_ch2); } else if (ch2_input > 8) { PWM_voltage_ch2 = (234 - (16.85 * (14 - ch2_input))) * 1.06; analogWrite(PWM1, PWM_voltage_ch2); } } void Increment_Voltage(){ if (millis() - lastDebounceTime > debounceDelay) { if(state == 0){ // Check if increment button is pressed if (PWM_voltage_ch1 < 234) { PWM_voltage_ch1 = PWM_voltage_ch1 + step;// Increment brightness analogWrite(PWM, PWM_voltage_ch1); // delay(500); // Debounce delay } else if(PWM_voltage_ch1 > 234){ PWM_voltage_ch1 = 234; analogWrite(PWM, PWM_voltage_ch1); } } else if(state == 1){ if (PWM_voltage_ch2 < 234) { PWM_voltage_ch2 = PWM_voltage_ch2 + step;// Increment brightness analogWrite(PWM1, PWM_voltage_ch2); // delay(500); // Debounce delay } else if(PWM_voltage_ch2 > 234){ PWM_voltage_ch2 = 234; analogWrite(PWM1, PWM_voltage_ch2); } } EIFR = bit(INTF0); EIFR = bit(INTF1); lastDebounceTime = millis(); } } void Decrement_Voltage(){ if (millis() - lastDebounceTime > debounceDelay) { if(state == 0){ if (PWM_voltage_ch1 > 33.7) { PWM_voltage_ch1 = PWM_voltage_ch1 - step;// Increment brightness analogWrite(PWM, PWM_voltage_ch1); // delay(500); } else{ PWM_voltage_ch1 = 33.7; analogWrite(PWM, PWM_voltage_ch1); // delay(500); } } if(state == 1){ if (PWM_voltage_ch2 > 33.7) { PWM_voltage_ch2 = PWM_voltage_ch2 - step;// Increment brightness analogWrite(PWM1, PWM_voltage_ch2); // delay(500); } else if(PWM_voltage_ch2 < 33.7){ PWM_voltage_ch2 = 33.7; analogWrite(PWM1, PWM_voltage_ch2); // delay(500); } } EIFR = bit(INTF0); EIFR = bit(INTF1); lastDebounceTime = millis(); } } void updateLCD() { lcd1.setCursor(0,0); lcd1.print("CH2:V="); lcd1.print(in_voltage * .94, 2); // Clear previous value lcd1.setCursor(10, 0); lcd1.print("mA="); lcd1.print(Current * (-1000), 0); lcd1.setCursor(0, 1); if(state == 0){ lcd1.print("^(F): "); lcd1.print(F); } else{ lcd1.print(" (F): "); lcd1.print(F); } lcd2.setCursor(0,0); lcd2.print("CH1:V="); lcd2.print(in_voltage2 * .94, 2); // Clear previous value lcd2.setCursor(10, 0); lcd2.print("mA="); lcd2.print(Current2 * (-1000), 0); lcd2.setCursor(0, 1); if(state == 1){ lcd2.print("^(F): "); lcd2.print(F); } else{ lcd2.print(" (F): "); lcd2.print(F); } }