Hi all! First off, sorry if this topic is placed in the wrong forum. I did not know where my issue would fall into and this is my first time posting. Now onto the issue at hand.
I am working on making a conveyor belt that will turn a motor (thus the belt) until an object triggers a proximity sensor. My code was working well when using the Arduino Nano however, I had recently switched over the the Arduino Nano Every so that I could use more interrupt pins in the future. The code was working perfectly for the Arduino Nano however there are issues with the Arduino Nano Every.
First, my function calls (Speed_Up() and Speed_Down()) do not seem to be called and if they do get called the motor is not behaving properly. Again, this was working perfectly on the Arduino Nano. The other issue is that I am trying to get my LEDs to blink however that does not work and the lights are just turned off. However trying the simple Blink.ino, it was working perfectly.
I am thinking that there could be an issue with loop functions (for loop and while loops) but I do not know if that is the problem. I am welcome to input and suggestions about how to troubleshoot this problem. Thanks ahead of time!
// Include Files
/*
* Nothing to Include
*/
// Pin Declaration
#define LED_On 15 // Green LED
#define LED_Pause 14 // Yellow LED
#define LED_Off 13 // Red LED
#define Stepper_Pul 6
#define Stepper_Dir 5
#define Sensor_Pin 3 // Proximity Sensor
#define EStop 2 // Normally Open Latched Emergency Pushbutton
// Global Variables
const unsigned long Stepper_Speed = 1500;
const unsigned long RampTime = 500;
const bool Rotation_Dir = 1; // High (CCW) and Low (CW)
volatile bool EStop_State; // 0 (EStop is unlatched) and 1 (EStop is latched)
volatile bool Sensor_State; // 0 (Not obstructions) and 1 (Obstruction)
//*****************************************//
// SETUP Function //
//*****************************************//
void setup() {
Serial.begin(9600);
// Setting pinMode for I/O signals
pinMode(Sensor_Pin, INPUT);
pinMode(EStop, INPUT);
pinMode(LED_On, OUTPUT);
pinMode(LED_Pause, OUTPUT);
pinMode(LED_Off, OUTPUT);
pinMode(Stepper_Pul, OUTPUT);
pinMode(Stepper_Dir, OUTPUT);
// Setting Initial Output States and Routine States
digitalWrite(Stepper_Dir, Rotation_Dir);
if (digitalRead(EStop)) {
digitalWrite(LED_On, LOW);
digitalWrite(LED_Off, HIGH);
EStop_State = 1;
}
else {
digitalWrite(LED_On, HIGH);
digitalWrite(LED_Off, LOW);
EStop_State = 0;
}
if (digitalRead(Sensor_Pin)) {
digitalWrite(LED_Pause, HIGH);
Sensor_State = 1;
}
else {
digitalWrite(LED_Pause, LOW);
Sensor_State = 0;
}
// Setting ISR Functions
attachInterrupt(digitalPinToInterrupt(EStop), EStop_ISR, CHANGE);
attachInterrupt(digitalPinToInterrupt(Sensor_Pin), Sensor_ISR, CHANGE);
}
//*****************************************//
// LOOP Function //
//*****************************************//
void loop() {
// Check if the system can move -> not in EStop or Sensor states
if (!(EStop_State || Sensor_State)) {
digitalWrite(Stepper_Pul, LOW);
digitalWrite(Stepper_Pul, HIGH);
delayMicroseconds(Stepper_Speed);
}
// Stopping the Motor and waiting until the EStop state is resolved
else if (EStop_State) {
digitalWrite(LED_On, LOW);
digitalWrite(LED_Off, HIGH);
Speed_Down();
// Blinking the Red only while the EStop state is active
while (EStop_State) {
digitalWrite(LED_Off, HIGH);
delay(1000);
digitalWrite(LED_Off, LOW);
delay(1000);
}
digitalWrite(LED_On, HIGH);
Speed_Up();
}
// Stopping the Motor and waiting until the Sensor state is resolved
else if (Sensor_State) {
Speed_Down();
// Blinking Yellow LED only is the Pause State is active and EStop is not
while (Sensor_State && !EStop_State) {
digitalWrite(LED_Pause, HIGH);
delay(1000);
digitalWrite(LED_Pause, LOW);
delay(1000);
}
// If the system is in EStop state then do not resume motor spinning
if (!EStop_State) {
Speed_Up();
}
}
else {
// Do Nothing
}
}
//*****************************************//
// ISR Functions //
//*****************************************//
void EStop_ISR() {
EStop_State = !EStop_State;
}
void Sensor_ISR() {
Sensor_State = !Sensor_State;
}
//*****************************************//
// Other Functions //
//*****************************************//
// Slowing speeding up the motor until set speed
void Speed_Up() {
int i;
for (i = Stepper_Speed + RampTime; i > Stepper_Speed; i--) {
if (EStop_State || Sensor_State) {
break;
}
digitalWrite(Stepper_Pul, LOW);
digitalWrite(Stepper_Pul, HIGH);
delayMicroseconds(i);
}
}
// Slowing reducing the speed of the motor
void Speed_Down() {
int i;
for (i = Stepper_Speed; i < Stepper_Speed + RampTime; i++) {
digitalWrite(Stepper_Pul, LOW);
digitalWrite(Stepper_Pul, HIGH);
delayMicroseconds(i);
}
}