Hello everyone,
I have an issue with the interruption in Arduino mega. I have an external interrupt using proximity sensor that turns on a relay and a motor (variable named "y"). While another external interrupt by another proximity sensor used to count number of gear teeth that once reach 16 teeth, it turns off the relay and the motor. As you can see on the code I reset the number of counting teeth once the first interrupt is triggered (count = 0).
The problem is that the turning on and off it occurs erratically such that it works fine for a while and then some errors and then works fine. Any suggestion of any errors on the code? or the problem occurs because of the unstable sensors' signals?
Here is the code,
#include "EasyNextionLibrary.h"
EasyNex myNex(Serial);
const byte motor1_run = 21;
const byte motor1_spd = 8;
const byte motor2_run = 4;
const byte motor2_spd = 9;
const byte motor3_run = 14;
const byte motor3_dir = 15;
const byte motor3_spd = 10;
const byte motor4_run = 16;
const byte motor4_dir = 17;
const byte motor4_spd = 11;
const byte count_sensor = 32; //PNP sensor: used for counts 2
const int clutch_sensor_2 = A3; //NPN sensor:used for turn off clutch after certain counts
const int fill_sensor = A4; //NPN sensor:used for turn off clutch
const byte door_sensor = 25;
const byte reset_weight_sensor = 30;
const byte reset_press_sensor = 31;
const byte motor1 = 26;
const byte motor2 = 27;
const byte clutch1 = 28; //used for turn on clutch motor
const byte clutch2 = 29;
byte x = 0;
byte number = 0;
byte Press = 0;
long Press_value = 0;
byte weight = 0;
int weight_value = 0;
bool operating = false;
bool Reset = false;
bool prepare = false;
const byte interruptPin = 3;
volatile byte count = 0;
const byte clutch_sensor_1 = 19;
volatile byte y = 0;
const byte count_interrupt = 18;
volatile byte count_d = 0;
byte x1 = 0;
byte x2 = 0;
unsigned long Time1 = 0;
unsigned long lastTime1 = 0;
/*unsigned long Time2 = 0;
unsigned long lastTime2 = 0;
unsigned long Time3 = 0;
unsigned long lastTime3 = 0;*/
void setup() {
myNex.begin(9600);
pinMode(4,OUTPUT);
pinMode(14,OUTPUT);
pinMode(15,OUTPUT);
pinMode(16,OUTPUT);
pinMode(17,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(21,OUTPUT);
pinMode(26,OUTPUT);
pinMode(27,OUTPUT);
pinMode(28,OUTPUT);
pinMode(29,OUTPUT);
pinMode(25,INPUT);
pinMode(30,INPUT);
pinMode(31,INPUT);
attachInterrupt(digitalPinToInterrupt(interruptPin),counting,RISING);
attachInterrupt(digitalPinToInterrupt(clutch_sensor_1),stateInv,RISING);
attachInterrupt(digitalPinToInterrupt(count_interrupt),countingballs,FALLING);
digitalWrite(motor1,LOW);
digitalWrite(motor2,LOW);
delay(2000);
}
void loop() {
myNex.NextionListen();
if (prepare){
servo_weight();
servo_press();
prepare = false;
}
if (operating){
digitalWrite(motor1,HIGH);
digitalWrite(motor2,HIGH);
if(y == 1 && analogRead(fill_sensor)<500){
if(x == 0){
noInterrupts();
count = 0;
interrupts();
x = 1;
}
digitalWrite(clutch1,HIGH);
servo_clutchOn();
}
if (y == 0) {
digitalWrite(clutch1,LOW);
servo_clutchOff();
x = 0;
}
if (count > 16){
y = 0;
}
}
else {
digitalWrite(motor1,LOW);
digitalWrite(motor2,LOW);
}
if (Reset){
if(digitalRead(reset_weight_sensor)==0){
servo_weight_reset(); x1 = 0;}
else if (digitalRead(reset_weight_sensor)==1)
{servo_weight_off(); x1 = 1;}
if(digitalRead(reset_press_sensor)== 0){
servo_press_reset(); x1 == 0;}
else if (digitalRead(reset_press_sensor)== 1)
{servo_press_off(); x2 = 1;}
if(digitalRead(reset_press_sensor)== 1 && digitalRead(reset_weight_sensor)==1 && x1 == 1 && x2 == 1)
{Reset = false;}
}
}
void trigger1(){
operating = true;
}
void trigger2(){
operating = false;
}
void trigger3(){
Reset = true;
}
void trigger4(){
number = myNex.readNumber("nNumber.val");
weight = myNex.readNumber("nWeight.val");
Press = myNex.readNumber("nPress.val");
Press_value = Press * 500;
if (weight == 25){
weight_value = 1500;
}
else if (weight == 30){
weight_value = 2000;
}
else if (weight == 35){
weight_value = 2600;
}
else if (weight == 40){
weight_value = 3250;
}
else if (weight == 45){
weight_value = 3900;
}
else if (weight == 50){
weight_value = 4600;
}
else if (weight == 55){
weight_value = 5200;
}
else if (weight == 60){
weight_value = 5850;
}
else if (weight == 65){
weight_value = 6500;
}
else if (weight == 70){
weight_value = 7150;
}
else if (weight == 75){
weight_value = 7800;
}
else if (weight == 80){
weight_value = 8450;
}
else if (weight == 85){
weight_value = 9100;
}
else if (weight == 90){
weight_value = 9750;
}
else if (weight == 95){
weight_value = 10400;
}
else if (weight == 100){
weight_value = 11050;
}
else if (weight == 105){
weight_value = 11700;
}
else if (weight == 110){
weight_value = 12350;
}
else if (weight == 115){
weight_value = 13000;
}
else if (weight == 120){
weight_value = 13650;
}
else if (weight == 125){
weight_value = 14300;
}
else if (weight == 130){
weight_value = 14950;
}
else if (weight == 135){
weight_value = 15600;
}
else if (weight == 140){
weight_value = 16250;
}
else if (weight == 145){
weight_value = 16900;
}
else if (weight == 150){
weight_value = 17550;
}
else if (weight == 155){
weight_value = 18200;
}
else if (weight == 160){
weight_value = 18850;
}
else {weight_value = 0;}
prepare = true;
}
void countingballs(){
Time1 = millis();
if(Time1 - lastTime1 > 250){
count_d++;
lastTime1 = Time1;
}
}
void counting(){
count++;
}
void stateInv(){
y = 1;
}
void servo_clutchOn(){
digitalWrite(motor1_run,LOW);
digitalWrite(motor2_run,LOW);
analogWrite(motor1_spd,100);
analogWrite(motor2_spd,100);
}
void servo_clutchOff(){
digitalWrite(motor1_run,HIGH);
digitalWrite(motor2_run,HIGH);
}
void servo_press(){
digitalWrite(motor3_run,LOW);
digitalWrite(motor3_dir,LOW); //LOW is Down && HIGH is Up && sensor3 is for top stop
analogWrite(motor3_spd,50);
delay(Press_value); //5000 milliseconds for 10%
digitalWrite(motor3_run,HIGH);
delay(1000);
}
void servo_press_reset(){
digitalWrite(motor3_run,LOW);
digitalWrite(motor3_dir,HIGH); //HIGH is for weight increase && LOW is for weight decrase
analogWrite(motor3_spd,50);
}
void servo_press_off(){
digitalWrite(motor3_run,HIGH);
digitalWrite(motor3_dir,HIGH); //HIGH is for weight increase && LOW is for weight decrase
analogWrite(motor3_spd,50);
}
void servo_weight(){
digitalWrite(motor4_run,LOW);
digitalWrite(motor4_dir,HIGH); //HIGH is for weight increase && LOW is for weight decrase
analogWrite(motor4_spd,20);
delay(weight_value); //130 milliseconds for 1 gram && starts with 15 gram
digitalWrite(motor4_run,HIGH);
delay(1000);
}
void servo_weight_reset(){
digitalWrite(motor4_run,LOW);
digitalWrite(motor4_dir,LOW); //HIGH is for weight increase && LOW is for weight decrase
analogWrite(motor4_spd,20);
}
void servo_weight_off(){
digitalWrite(motor4_run,HIGH);
digitalWrite(motor4_dir,LOW); //HIGH is for weight increase && LOW is for weight decrase
analogWrite(motor4_spd,20);
}