// 16 bit PWM on any pin
// Example uses built in LED on pin 13 (PORTB bit 5)
const int currentPin = A0; // pin that the current sensor is attached to
const int voltagePin = A1; // pin that the voltage sensor is attached to
const int PowerPin = A3; // pin that the voltage sensor is attached to
const int IGBTonPin = 7;
const int tempPin = A4; // pin that the relay is attached to
const int relayPin = 12; // pin that the relay is attached to
const int voltagerelayPin = 2; //pin that the charging cut out relay is attached to
const int shutdownPin = 4; //pin that the shutdown relay is attached to
const int threshold = 20;//8=50 mv//61; // the current draw (300mv)threshold level that's in the range of the analog input
const int threshold1 = 120; // the voltage draw (600mv)threshold level that's in the range of the analog input
unsigned long starTime = 0; // global variable
unsigned long interval = 1000; // 5 seconds
//int x = 1000;
bool relayFlag = false;
byte attempts = 0;
void setup(){
// initialize the RELAY pins as an output:
pinMode(relayPin, OUTPUT);
pinMode(voltagerelayPin, OUTPUT);
pinMode(shutdownPin, OUTPUT);
Serial.begin(9600);
pinMode(3, OUTPUT);// GATE IGBT 20 Amps 1200 Volts
cli(); // Disable all interrupts// Disable all interrupts
TCCR1A = 0; // Clear all flags in control register A
TCCR1B = 0; // Clear all flags in control register B
TCNT1 = 0;
TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM10) |_BV(WGM00);
TCCR1B = _BV(WGM12) | _BV(CS00); // prescaler = 1
TIMSK1 |= _BV(OCIE1A); // Enable timer compare interrupt
TIMSK1 |= _BV(TOIE1); // Enable timer overflow interrupt
sei(); // enable all interrupts
}
void loop(){
for(unsigned int x = 0; x < 65535; x++){
OCR1A = x/map(analogRead(5),140,0,35,0);//power out put control A5
delayMicroseconds (1);
// read the value of the current sensor:
int analogValue = analogRead(currentPin);
// if the analog value is high enough, power the relay:
if (relayFlag == false && attempts < 999 && analogValue > threshold)
{
digitalWrite(relayPin, HIGH);
relayFlag = true;
starTime = millis(); // start timer
digitalWrite(shutdownPin, LOW);
delay (5000);
}
if(relayFlag == true && millis() - starTime >= 1000UL)
{
// current time - start time >= 5 seconds
digitalWrite(relayPin,LOW);
//delay(100); //this could be removed if needed
if(voltagePin > 5)
digitalWrite (voltagerelayPin,HIGH);
digitalWrite (shutdownPin, LOW);
relayFlag = false;
attempts = attempts + 1;
delay (1000);
digitalWrite(shutdownPin, HIGH);
delay (1000);
}
}
}
ISR(TIMER1_OVF_vect){ // Timer1 overflow interrupt service routine
PORTD |= _BV(PORTD3);
PORTD |= _BV(PORTD7);
}
ISR(TIMER1_COMPA_vect){ // Timer1 compare interrupt service routine
PORTD &= ~_BV(PORTD3);
PORTD &= ~_BV(PORTD7);
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น