hi, i don't even know if this code is correct but im trying to add a 1 second delay before the first pulse out and then no delay with the second pulse out here is the code
//KaizenRelay Template
//Version 4.0 - 2022 January 22
//Kaizen Speed, LLC. - Vadim Belogorodsky
void EnableGreenLED();
void DisableGreenLED();
void EnableRedLED();
void DisableRedLED();
#include "KaizenRelay.h"
#define UseGenericCANRx 0
#define UseGenericCANTx 0
//Program Variable Area:
bool InputState = false;
bool InputStateLast = false;
byte PulsePhase = 0;
bool PulseTimerRunning = false;
unsigned int PulseTimer = 0;
void setup()
{
//Setup Pins and CAN Timer:
HardwareSetup();
//Input Pin 1 Setup:
//Enable5VSupply();
//Disable5VSupply();
//EnableInput1Pullup();
//DisableInput1Pullup();
EnableInput1Pulldown();
//DisableInput1Pulldown();
//DisableInput1Switches();
//Input Pin 2 Setup:
//EnableInput2Pullup();
//DisableInput2Pullup();
//EnableInput2Pulldown();
//DisableInput2Pulldown();
DisableInput2Switches();
//Input Pin 3 Setup:
//EnableInput3Pullup();
//DisableInput3Pullup();
//EnableInput3Pulldown();
//DisableInput3Pulldown();
DisableInput3Switches();
//Output Pin 1 Setup:
Output_1_Mode = 2;
//Output Pin 2 Setup:
Output_2_Mode = 2;
//Output Pin 3 Setup:
Output_3_Mode = 0;
//Output PWM Setup:
//SetupPWM10Hz();
//SetupPWM50Hz();
//SetupPWM100Hz();
//CAN Baud Rate Setup:
//SetupCAN_125kbps();
//SetupCAN_250kbps();
//SetupCAN_500kbps();
//SetupCAN_1000kbps();
//CAN Termination Resistor:
//EnableCANTermResistor();
DisableCANTermResistor();
//Setup USB Interface
Serial.begin(57600);
}
void loop()
{
//Check if CAN Messages are Available
if(CAN_MSGAVAIL == CAN_.checkReceive())
{
//Read in CAN Message Length and Data
CAN_.readMsgBuf(&CAN_Read_Len, CAN_Read_Msg);
//Read in CAN Message ID
CAN_Read_ID = CAN_.getCanId();
//Generic CAN Listen
if(UseGenericCANRx)
{
if((CAN_Read_ID >= CAN_Base_Address + 7) && (CAN_Read_ID <= CAN_Base_Address + 10))
CANListenMsg();
}
CAN_MissedMsgs = 0;
}
else CAN_MissedMsgs++;
CAN_Timeout = (CAN_MissedMsgs > 10000);
//Variable Overflow Handle
if(CAN_MissedMsgs > 60000) CAN_MissedMsgs = 11000;
//Check if Serial Messages are Available()
if(Serial.available())
{
while(Serial.available())
{
SerialInput += (char)Serial.read();
}
SerialIndex = SerialInput.substring(0, 3);
SerialData = SerialInput.substring(4, SerialInput.length());
ReadHWStatusUSB();
SerialInput = "";
}
if(Flag10ms)
{
Timer10ms();
Flag10ms = false;
}
if(Flag20ms && !Flag10ms)
{
Timer20ms();
Flag20ms = false;
}
if(Flag50ms && !Flag20ms && !Flag10ms)
{
Timer50ms();
Flag50ms = false;
}
if(Flag100ms && !Flag50ms && !Flag20ms && !Flag10ms)
{
Timer100ms();
Flag100ms = false;
}
if(Flag500ms && !Flag100ms && !Flag50ms && !Flag20ms && !Flag10ms)
{
Timer500ms();
Flag500ms = false;
}
if(Flag1000ms && !Flag500ms && !Flag100ms && !Flag50ms && !Flag20ms && !Flag10ms)
{
Timer1000ms();
Flag1000ms = false;
}
}
//This sets a timer delay for the output pin if voltage is suplied to the input and sets delay for the ouput once voltage is removed from the input.
void loop() {
// Check if the input is high
if (digitalRead(Input1Digital) == HIGH && !isInputHigh) {
// Input is detected, wait one second and set output1 high
delay(1000);
digitalWrite(Output1Digital, HIGH);
isInputHigh = true;
} else if (digitalRead(Input1Digital) == LOW && isInputHigh) {
// Input is gone, wait 1 second and set output 1 low
delay(1000);
digitalWrite(Output1Digital, LOW);
isInputHigh = false;
}
}
void USB_Write()
{
//User Area for USB Writing - Message Beginning ""
Serial.print("");
//Place Your Required Text Output Here -- for example:
Serial.print("In: "); Serial.print(Input_1_Digital);
Serial.print(", Ph: "); Serial.print(PulsePhase);
//Serial.print(", T_RunnRunning: "); Serial.print(PulseTimerRunning);
Serial.print(", Tmr: "); Serial.print(PulseTimer);
Serial.print(", Out: "); Serial.print(Output_1_Digital);
//Message Ending ";"
Serial.println(";");
}
void Timer10ms()
{
if(UseGenericCANTx) SendHWStatusCAN();
Input_1_Digital = ReadInput1Digital(false);
InputState = Input_1_Digital;
if(InputState != InputStateLast)
{
if(!InputState) //On to Off
{
PulsePhase = 1;
PulseTimer = 0;
PulseTimerRunning = true;
}
if(InputState) //Off to On
{
PulsePhase = 2;
PulseTimer = 0;
PulseTimerRunning = true;
}
}
InputStateLast = InputState;
if(PulseTimerRunning) PulseTimer++;
if(PulseTimer > 20000) PulseTimer = 10000;
if(PulsePhase == 1)
{
if(PulseTimerRunning && (PulseTimer < 50)) {DisableOutput1Digital(); EnableOutput2Digital(); DisableGreenLED();}
if(PulseTimerRunning && ((PulseTimer >= 50) && (PulseTimer < 100))) {EnableOutput1Digital(); DisableOutput2Digital(); EnableGreenLED();}
if(PulseTimer == 100) {PulsePhase = 0; PulseTimerRunning = false; DisableOutput1Digital(); EnableOutput2Digital(); DisableGreenLED();}
}
if(PulsePhase == 2)
{
if(PulseTimerRunning && (PulseTimer <= 50)) {EnableOutput1Digital(); DisableOutput2Digital(); EnableGreenLED();}
if(PulseTimer == 50) {PulsePhase = 0; PulseTimerRunning = false; DisableOutput1Digital(); EnableOutput2Digital(); DisableGreenLED();}
}
}
void Timer20ms()
{
USB_Write();
}
void Timer50ms()
{
SendHWStatusUSB();
}
void Timer100ms()
{
}
void Timer500ms()
{
}
void Timer1000ms()
{
//Generic CAN Listen
if(UseGenericCANRx)
{
if(CAN_Timeout)
{
//CAN Error
if(Output_1_Mode == 1) Output_1_Duty = 0;
if(Output_1_Mode == 2) DisableOutput1Digital();
Output_1_Mode = 0;
if(Output_2_Mode == 1) Output_2_Duty = 0;
if(Output_2_Mode == 2) DisableOutput2Digital();
Output_2_Mode = 0;
if(Output_3_Mode == 1) Output_3_Duty = 0;
if(Output_3_Mode == 2) DisableOutput3Digital();
Output_3_Mode = 0;
DisableGreenLED();
DisableRedLED();
}
}
}
ISR(TIMER1_COMPA_vect) //Timer1 Interrupt
{
PWMpulseCounter++;
if(PWMpulseCounter >= 99) PWMpulseCounter = 0;
//Port Manipulation:
/*
//Green LED: PD1
if(xxx)
{
if(PWMpulseCounter < Output_x_Duty) PORTD = PORTD | 0b00000010;
else PORTD = PORTD & 0b11111101;
}
//Red LED: PD0
if(xxx)
{
if(PWMpulseCounter < Output_y_Duty) PORTD = PORTD | 0b00000001;
else PORTD = PORTD & 0b11111110;
}
*/
//Output 1: PB5
if(Output_1_Mode == 1)
{
if(PWMpulseCounter < Output_1_Duty) PORTB = PORTB | 0b00100000;
else PORTB = PORTB & 0b11011111;
}
//Output 2: PC7
if(Output_2_Mode == 1)
{
if(PWMpulseCounter < Output_2_Duty) PORTC = PORTC | 0b10000000;
else PORTC = PORTC & 0b01111111;
}
//Output 3: PC6
if(Output_3_Mode == 1)
{
if(PWMpulseCounter < Output_3_Duty) PORTC = PORTC | 0b01000000;
else PORTC = PORTC & 0b10111111;
}
}
void CANListenMsg()
{
if(CAN_Read_ID == (CAN_Base_Address + 7))
{
if(CAN_Read_Msg[0] == 0x01)
{
Output_1_Mode = 2;
}
else if (CAN_Read_Msg[0] == 0x02)
{
Output_1_Mode = 1;
if(Output_1_PWMFreq != 10) SetupPWM10Hz();
}
else if (CAN_Read_Msg[0] == 0x04)
{
Output_1_Mode = 1;
if(Output_1_PWMFreq != 50) SetupPWM50Hz();
}
else if (CAN_Read_Msg[0] == 0x08)
{
Output_1_Mode = 1;
if(Output_1_PWMFreq != 100) SetupPWM100Hz();
}
else
{
Output_1_Digital = false;
Output_1_Duty = 0;
Output_1_Mode = 0;
}
if(Output_1_Mode == 2)
{
if(CAN_Read_Msg[1]) EnableOutput1Digital();
else DisableOutput1Digital();
}
if(Output_1_Mode == 1)
{
DisableOutput1Digital();
if(CAN_Read_Msg[1] <= 100) Output_1_Duty = CAN_Read_Msg[1];
else Output_1_Duty = 0;
}
}
if(CAN_Read_ID == (CAN_Base_Address + 8))
{
if(CAN_Read_Msg[0] == 0x01)
{
Output_2_Mode = 2;
}
else if (CAN_Read_Msg[0] == 0x02)
{
Output_2_Mode = 1;
if(Output_2_PWMFreq != 10) SetupPWM10Hz();
}
else if (CAN_Read_Msg[0] == 0x04)
{
Output_2_Mode = 1;
if(Output_2_PWMFreq != 50) SetupPWM50Hz();
}
else if (CAN_Read_Msg[0] == 0x08)
{
Output_2_Mode = 1;
if(Output_2_PWMFreq != 100) SetupPWM100Hz();
}
else
{
Output_2_Digital = false;
Output_2_Duty = 0;
Output_2_Mode = 0;
}
if(Output_2_Mode == 2)
{
if(CAN_Read_Msg[1]) EnableOutput2Digital();
else DisableOutput2Digital();
}
if(Output_2_Mode == 1)
{
DisableOutput2Digital();
if(CAN_Read_Msg[1] <= 100) Output_2_Duty = CAN_Read_Msg[1];
else Output_2_Duty = 0;
}
}
if(CAN_Read_ID == (CAN_Base_Address + 9))
{
if(CAN_Read_Msg[0] == 0x01)
{
Output_3_Mode = 2;
}
else if (CAN_Read_Msg[0] == 0x02)
{
Output_3_Mode = 1;
if(Output_3_PWMFreq != 10) SetupPWM10Hz();
}
else if (CAN_Read_Msg[0] == 0x04)
{
Output_3_Mode = 1;
if(Output_3_PWMFreq != 50) SetupPWM50Hz();
}
else if (CAN_Read_Msg[0] == 0x08)
{
Output_3_Mode = 1;
if(Output_3_PWMFreq != 100) SetupPWM100Hz();
}
else
{
Output_3_Digital = false;
Output_3_Duty = 0;
Output_3_Mode = 0;
}
if(Output_3_Mode == 2)
{
if(CAN_Read_Msg[1]) EnableOutput3Digital();
else DisableOutput3Digital();
}
if(Output_3_Mode == 1)
{
DisableOutput3Digital();
if(CAN_Read_Msg[1] <= 100) Output_3_Duty = CAN_Read_Msg[1];
else Output_3_Duty = 0;
}
}
if(CAN_Read_ID == (CAN_Base_Address + 10))
{
if(CAN_Read_Msg[0] & 0x01) EnableCANTermResistor();
else DisableCANTermResistor();
if(CAN_Read_Msg[0] & 0x02) EnableGreenLED();
else DisableGreenLED();
if(CAN_Read_Msg[0] & 0x04) EnableRedLED();
else DisableRedLED();
}
}