Relay + interrupt pin

So long story short.....

I have a coin acceptor on interrupt pin 1 And A relay shield on pin 4

Whenever the relay clicks on or off. the interrupt pin fires a random amount of times. The relay shield i use is photocoupled type. Im just about to order a solid state relay to see if that helps.

Anyone else have any ideas on what could be causing the problem.

Thanks in advance

Is there a program to go with this long-short story?

Paul

#include <Adafruit_GFX.h>
#include <gfxfont.h>
#include <Adafruit_LEDBackpack.h>
#include <Wire.h>
Adafruit_7segment Display = Adafruit_7segment();

//AUDIO SETUP
#include <SoftwareSerial.h>
#define ARDUINO_RX 18
#define ARDUINO_TX 19
SoftwareSerial mySerial(ARDUINO_RX, ARDUINO_TX);
static int8_t Send_buf[8] = {0} ;
#define CMD_SEL_DEV 0X09
#define DEV_TF 0X02
#define CMD_PLAY_W_VOL 0X22
#define CMD_PAUSE 0X0E

//GAME STARTED STATS
int START_BUTTON = 26;
volatile int GAME_CREDITS = 0;
volatile int GAME_PART_CREDIT = 0;
int GAME_IN_PROGRESS = 0;

//COIN MECH PINS + STATE
int COIN_MECH_PIN = 0;

//MUSIC PLAYING STATE
int fb_music_playing = 0;
int lr_music_playing = 0;

//MOTOR PINS
int FB_PWM = 13;
int FB_DIR = 12;
int FB_BRK = 11;

int LR_PWM = 10;
int LR_DIR = 9;
int LR_BRK = 8;

int CL_PWM = 7;
int CL_DIR = 6;
int CL_BRK = 5;

//CLAW RELAY PIN
int CLAW_RELAY = 4;

//JOYSTICK PINS
int BACK = 1;
int BACK_BUTTON_STATE_1 = 0;
int FORWARD = 3;
int FORWARD_BUTTON_STATE_1 = 0;
int LEFT = 2;
int LEFT_BUTTON_STATE_1 = 0;
int RIGHT = 0;
int RIGHT_BUTTON_STATE_1 = 0;

//GANTRY SWITCH PINS
int FORWARD_LIMIT_SWITCH = 31;
int FORWARD_LIMIT_SWITCH_STATE = 0;
int BACK_LIMIT_SWITCH = 35;
int BACK_LIMIT_SWITCH_STATE = 0;
int LEFT_LIMIT_SWITCH = 33;
int LEFT_LIMIT_SWITCH_STATE = 0;
int RIGHT_LIMIT_SWITCH = 41;
int RIGHT_LIMIT_SWITCH_STATE = 0;
int CLAW_DOWN_LIMIT_SWITCH = 39;
int CLAW_DOWN_LIMIT_SWITCH_STATE = 0;
int CLAW_UP_LIMIT_SWITCH = 37;
int CLAW_UP_LIMIT_SWITCH_STATE = 0;

void setup() {
Display.begin(0x70);
mySerial.begin(9600);
delay(500);
sendCommand(CMD_SEL_DEV, DEV_TF);
Serial.begin(9600);

pinMode(FB_PWM,OUTPUT);
pinMode(FB_DIR,OUTPUT);
pinMode(FB_BRK,OUTPUT);

pinMode(LR_PWM,OUTPUT);
pinMode(LR_DIR,OUTPUT);
pinMode(LR_BRK,OUTPUT);

pinMode(CL_PWM,OUTPUT);
pinMode(CL_DIR,OUTPUT);
pinMode(CL_BRK,OUTPUT);

pinMode(START_BUTTON,INPUT);

pinMode(CLAW_RELAY,OUTPUT);

digitalWrite(FB_BRK,HIGH);
digitalWrite(FB_DIR,LOW);

digitalWrite(LR_BRK,HIGH);
digitalWrite(LR_DIR,LOW);

digitalWrite(CL_BRK,HIGH);
digitalWrite(CL_DIR,LOW);

attachInterrupt(COIN_MECH_PIN, CoinInserted, RISING);
}

void loop() {

//CHECK GAME CREDITS AND START GAME
if(digitalRead(START_BUTTON)==HIGH && GAME_IN_PROGRESS==0 && GAME_CREDITS>0)
{
GAME_IN_PROGRESS = 1;
GAME_CREDITS = GAME_CREDITS - 1;
Serial.println(“Starting Game…”);
delay(3000);
}
else if(digitalRead(START_BUTTON)==HIGH && GAME_IN_PROGRESS==1)
{
GAME_IN_PROGRESS = 2;
}

if(GAME_IN_PROGRESS==1)
{
//BACK AND FORWARD GANTRY MOVEMENTS
BACK_BUTTON_STATE_1 = analogRead(BACK);
BACK_LIMIT_SWITCH_STATE = digitalRead(BACK_LIMIT_SWITCH);
FORWARD_BUTTON_STATE_1 = analogRead(FORWARD);
FORWARD_LIMIT_SWITCH_STATE = digitalRead(FORWARD_LIMIT_SWITCH);
if(BACK_BUTTON_STATE_1 == LOW && BACK_LIMIT_SWITCH_STATE == 0)
{
if(fb_music_playing == 0)
{
sendCommand(CMD_PLAY_W_VOL, 0X0901);
fb_music_playing = 1;
}
Serial.println(“Moving Back…”);
digitalWrite(FB_DIR,HIGH);
analogWrite(FB_PWM,255);
digitalWrite(FB_BRK,LOW);
}
else if(FORWARD_BUTTON_STATE_1 == LOW && FORWARD_LIMIT_SWITCH_STATE == 0)
{
if(fb_music_playing == 0)
{
sendCommand(CMD_PLAY_W_VOL, 0X0901);
fb_music_playing = 1;
}
Serial.println(“Moving Forward…”);
digitalWrite(FB_DIR,LOW);
analogWrite(FB_PWM,255);
digitalWrite(FB_BRK,LOW);
}
else
{
if(fb_music_playing == 1)
{
sendCommand(CMD_PAUSE, 0X0E);
fb_music_playing = 0;
}
digitalWrite(FB_BRK,HIGH);
}
//BACK AND FORWARD GANTRY MOVEMENTS

//LEFT AND RIGHT GANTRY MOVEMENTS
LEFT_BUTTON_STATE_1 = analogRead(LEFT);
LEFT_LIMIT_SWITCH_STATE = digitalRead(LEFT_LIMIT_SWITCH);
RIGHT_BUTTON_STATE_1 = analogRead(RIGHT);
RIGHT_LIMIT_SWITCH_STATE = digitalRead(RIGHT_LIMIT_SWITCH);
if(LEFT_BUTTON_STATE_1 == LOW && LEFT_LIMIT_SWITCH_STATE == 0)
{
if(lr_music_playing == 0)
{
sendCommand(CMD_PLAY_W_VOL, 0X0901);
lr_music_playing = 1;
}
Serial.println(“Moving Left…”);
digitalWrite(LR_DIR,HIGH);
analogWrite(LR_PWM,255);
digitalWrite(LR_BRK,LOW);
}
else if(RIGHT_BUTTON_STATE_1 == LOW && RIGHT_LIMIT_SWITCH_STATE == 0)
{
if(lr_music_playing == 0)
{
sendCommand(CMD_PLAY_W_VOL, 0X0901);
lr_music_playing = 1;
}
Serial.println(“Moving Right…”);
digitalWrite(LR_DIR,LOW);
analogWrite(LR_PWM,255);
digitalWrite(LR_BRK,LOW);
}
else
{
if(lr_music_playing == 1)
{
sendCommand(CMD_PAUSE, 0X0E);
lr_music_playing = 0;
}
digitalWrite(LR_BRK,HIGH);
}
//LEFT AND RIGHT GANTRY MOVEMENTS
}

if(GAME_IN_PROGRESS==2)
{
Serial.println(“Dropping Claw…”);
if(digitalRead(CLAW_DOWN_LIMIT_SWITCH)==LOW)
{
digitalWrite(CL_DIR,LOW);
analogWrite(CL_PWM,255);
digitalWrite(CL_BRK,LOW);
}
else
{
GAME_IN_PROGRESS = 3;
digitalWrite(CL_BRK,HIGH);
}
}

if(GAME_IN_PROGRESS==3)
{
GAME_IN_PROGRESS = 4;
Serial.println(“Clamp Claw…”);
delay(500);
digitalWrite(CLAW_RELAY,HIGH);
delay(1000);
digitalWrite(CLAW_RELAY,LOW);
delay(500);
}

Display.writeDigitRaw(0, 57);
Display.writeDigitRaw(1, 80);
if(GAME_CREDITS<10)
{
Display.writeDigitNum(3, 0);
Display.writeDigitNum(4, GAME_CREDITS, ((GAME_PART_CREDIT==1)?true:false));
}
else
{
String NEW_GAME_CREDITS = String(GAME_CREDITS);
String GAME_CREDIT_DIGIT_1 = NEW_GAME_CREDITS.substring(0,1);
String GAME_CREDIT_DIGIT_2 = NEW_GAME_CREDITS.substring(1,2);
Display.writeDigitNum(3, GAME_CREDIT_DIGIT_1.toInt());
Display.writeDigitNum(4, GAME_CREDIT_DIGIT_2.toInt(), ((GAME_PART_CREDIT==1)?true:false));
}
Display.drawColon(true);
Display.writeDisplay();

}

//AUDIO SEND COMMAND
void sendCommand(int8_t command, int16_t dat)
{
delay(20);
Send_buf[0] = 0x7e; //starting byte
Send_buf[1] = 0xff; //version
Send_buf[2] = 0x06; //the number of bytes of the command without starting byte and ending byte
Send_buf[3] = command; //
Send_buf[4] = 0x00;//0x00 = no feedback, 0x01 = feedback
Send_buf[5] = (int8_t)(dat >> 8);//datah
Send_buf[6] = (int8_t)(dat); //datal
Send_buf[7] = 0xef; //ending byte
for(uint8_t i=0; i<8; i++)//
{
mySerial.write(Send_buf*) ;*

  • }*
    }
    //AUDIO SEND COMMAND
    //CHECK COIN INSERTED
    void CoinInserted()
    {
    Serial.println(“coin!”);
    if(GAME_PART_CREDIT==1)
    {
    GAME_CREDITS = GAME_CREDITS + 1;
    *GAME_PART_CREDIT = 0; *
    }
    else
    {
    *GAME_PART_CREDIT = 1; *
    }
    }
    //CHECK COIN INSERTED

Any noise on pin 0 will trigger an interrupt, as it a level change. Try making the interrupt on "high". Then the interrupt will occur when pin 0 voltage goes to 5 volts.

Paul

Okay will try that. Would a pull down resistor help on pin 0

Any noise on pin 0 will trigger an interrupt, as it a level change. Try making the interrupt on "high". Then the interrupt will occur when pin 0 voltage goes to 5 volts.

Paul

Won't help, if you were to use interrupt on HIGH it will keep interrupting for as long at the pin is high. And anyway there is no such mode, you can only interrupt on LOW, CHANGE, RISING or FALLING.

The problem is lots of interference - that needs sorting.

We need to know what this opto-coupled relay actually is and how it has been wired up (they can be wired up wrong so that the opto-isolation is bypassed).

signal - pin 4 DC - Ground + = 12v supply

NO - 48v+ CO - Ground

the relay opens and closes the solonoid, it seems that if i unplug the solonoid, it works as it should and doesn't fire false interrupts

Sounds like your missing a reverse connected diode across the solenoid.

Try eliminating any ground loops by using a separate ground wire for each GND connection, all terminating at one point (the power supply -'ve terminal).

Try adding a 1µF capacitor across the solenoid.

Try adding a 100µF capacitor across your power supply.

Williamgentry: the relay opens and closes the solonoid, it seems that if i unplug the solonoid, it works as it should and doesn't fire false interrupts

I don't know what the solenoid does or when it is used, but are you able to disable the interrupt before triggering the relay/solenoid, and enabling the interrupt afterward?

Paul