// https://forum.arduino.cc/t/combined-2-sketches-into-1-loop-only-1-sketch-works/1468660/
// nov28e
// works on ATMEGA2560
#include <SevSeg.h>
SevSeg sevseg; // Create display controller object
const int pwmPinA = 11; // Timer 1 output A
const int pwmPinB = 12; // Timer 1 output B
const int potPinA = A0; // pot A
const int potPinB = A1; // pot B
const int buzzerPin = 41;
const int ledPinC = 43;
uint32_t start;
const int noPwmSenseA = 2; // from 4538a output
const int noPwmSenseB = 3; // from 4538b output
const int mosfetSenseA = 4; //from mosfet drain A
const int mosfetSenseB = 5; //from mosfet drain B
int noPwmStateA;
int mosfetStateA = LOW;
int noPwmStateB;
int mosfetStateB = LOW;
#define delayTime 100 // 100mS to stop flicker
unsigned long lastExecutedMillisA = 0; // create a variable to save the last executed time
unsigned long lastExecutedMillisB = 0; // create a variable to save the last executed time
bool fault = false;
const int tempSensorA = A2;
const int tempSensorB = A3;
const float refVoltage = 5.0;
const float threshold = 1.25; // Trigger when voltage is less than 1.25V
int buzzerState = LOW; // buzzer
unsigned long previousMillis = 0; // buzzer
const long interval = 500; // buzzer 1000 ms = 1 second
unsigned long previousMillisDisp = 0; // display
bool displayOn = true;
void setup() {
pinMode(pwmPinA, OUTPUT);
pinMode(pwmPinB, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPinC, OUTPUT);
pinMode(mosfetSenseA, INPUT);
pinMode(noPwmSenseA, INPUT);
pinMode(mosfetSenseB, INPUT);
pinMode(noPwmSenseB, INPUT);
Serial.begin(115200);
byte numDigitsA = 4; // number of digits display
byte digitPinsA[] = {29, 30, 39, 40}; // Pins for Digits 1, 2, 3, and 4
byte segmentPinsA[] = {22, 23, 24, 25, 26, 27, 28}; // Pins for segments A, B, C, D, E, F, G,
bool resistorsOnSegments = true; // 'true' if resistors are on segment pins
byte hardwareConfig = COMMON_ANODE; // CHANGE THIS to COMMON_CATHODE
bool updateWithDelays = false; // Highly recommended 'false' for clean multiplexing
bool leadingZeros = false; // Set to true if you want '0050%' instead of ' 50%'
bool disableDecPoint = true; // We don't need decimal points for simple percentages
sevseg.begin(hardwareConfig, numDigitsA, digitPinsA, segmentPinsA, resistorsOnSegments,
updateWithDelays, leadingZeros, disableDecPoint); // Initialise the 7-segment display A
sevseg.setBrightness(90);
TCCR1A = 0; // Clear timer registers
TCCR1B = 0;
//TCNT1 = 0;
// Set Fast PWM Mode 14: WGM13, WGM12, WGM11 high; WGM10 low
// COM1A1 and COM1B1 set for non-inv PWM on OC1A (Pin 9) and OC1A (pin 10)
TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM11);
TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS10); // Prescaler = 1
long freq = 1000; // 1 kHz, ICR1 = (16,000,000 / (1 * 1000)) - 1 = 15999 16 MHz clock and prescaler = 1
long icrValue = (F_CPU / freq) - 1;
ICR1 = icrValue;
// Set initial duty cycle (e.g., 99%) for 2 seconds
OCR1A = 0.99 * (ICR1 + 1); //(duty_percentage / 100) * (ICR1 + 1);
OCR1B = 0.99 * (ICR1 + 1); //(duty_percentage / 100) * (ICR1 + 1);
//OCR1A = icrValue / 2;
start = millis();
while (millis() - start < 1000)
{
sevseg.setNumber(9999);
sevseg.refreshDisplay();
}
}
void normal() {
unsigned long currentMillisA = millis(); // copy the current millis to our variable
if (currentMillisA - lastExecutedMillisA >= delayTime) {// check to see how much time has passed
lastExecutedMillisA = currentMillisA; // save the last executed time
int potValueA = analogRead(potPinA);
unsigned int dutyCycleA = map(potValueA, 0, 1023, 1600, 15840); //0 to 5V, 10 to 99%
unsigned int dutyPercentA = map(potValueA, 0, 1023, 10, 99); // sets display to match %
OCR1A = dutyCycleA; //(ICR1 + 1) * 0.25;
int potValueB = analogRead(potPinB);
unsigned int dutyCycleB = map(potValueB, 0, 1023, 1600, 15840); //0 to 5V, 10 to 99%
unsigned int dutyPercentB = map(potValueB, 0, 1023, 10, 99); // sets display to match %
OCR1B = dutyCycleB; //(ICR1 + 1) * 0.25;
digitalWrite(ledPinC, LOW);
digitalWrite(buzzerPin, LOW);
sevseg.setNumber((dutyPercentA * 100) + dutyPercentB);
}
}
void alarmOCA() { // open-circuit on channel A
int dutyPercentB = 1;
int dutyCycleB = 160; //0 = 0%
OCR1B = dutyCycleB;
unsigned long currentMillisDisp = millis();
if (currentMillisDisp - previousMillisDisp >= interval) {
previousMillisDisp = currentMillisDisp;
displayOn = !displayOn;
if (displayOn) {
sevseg.setChars("OC--");
} else {
sevseg.blank();
}
}
}
void alarmOCB() { // open-circuit on channel B
int dutyPercentA = 1;
int dutyCycleA = 160; //0 = 0%
OCR1A = dutyCycleA;
unsigned long currentMillisDisp = millis();
if (currentMillisDisp - previousMillisDisp >= interval) {
previousMillisDisp = currentMillisDisp;
displayOn = !displayOn;
if (displayOn) {
sevseg.setChars("--OC");
} else {
sevseg.blank();
}
}
}
void alarmOHA() { // overheating on channel A
unsigned long currentMillisDisp = millis();
if (currentMillisDisp - previousMillisDisp >= interval) {
previousMillisDisp = currentMillisDisp;
displayOn = !displayOn;
if (displayOn) {
sevseg.setChars("OH--");
} else {
sevseg.blank();
}
}
}
void alarmOHB() { // overheating on channel B
unsigned long currentMillisDisp = millis();
if (currentMillisDisp - previousMillisDisp >= interval) {
previousMillisDisp = currentMillisDisp;
displayOn = !displayOn;
if (displayOn) {
sevseg.setChars("--OH");
} else {
sevseg.blank();
}
}
}
void buzzer() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
buzzerState = (buzzerState == LOW) ? HIGH : LOW;
digitalWrite(buzzerPin, buzzerState);
}
}
void loop() {
noPwmStateA = digitalRead(noPwmSenseA);
noPwmStateB = digitalRead(noPwmSenseB);
mosfetStateA = digitalRead(mosfetSenseA);
mosfetStateB = digitalRead(mosfetSenseB);
if (noPwmStateA == LOW && mosfetStateA == LOW) {
digitalWrite(ledPinC, HIGH);
fault = true;
alarmOCA();
buzzer();
}
if (noPwmStateB == LOW && mosfetStateB == LOW) {
digitalWrite(ledPinC, HIGH);
fault = true;
alarmOCB();
buzzer();
}
if (noPwmStateA == HIGH && noPwmStateB == HIGH) {
fault = false;
}
int rawValueA = analogRead(tempSensorA);
float voltageA = (rawValueA * refVoltage) / 1024.0; // 10-bit ADC (0-1023)
if (voltageA < threshold) {
fault = true;
alarmOHA();
buzzer();
}
int rawValueB = analogRead(tempSensorB);
float voltageB = (rawValueB * refVoltage) / 1024.0; // 10-bit ADC (0-1023)
if (voltageB < threshold) {
fault = true;
alarmOHB();
buzzer();
}
if (fault == false) {
normal();
}
sevseg.refreshDisplay();
}pwmA
pwmB
buzzer
ledC
pwmA
pwmB
mosA
mosB
potA
potB
tempA
tempB
PWM