Relay not behaving..

Hi All,
I am trying to make a keyboard activated solenoid using an MPR121 touch panel, and a solenoid lock which is operated via a relay using a separate power supply.
The other components are a RED led for incorrect inputs, a green one for correct input and a piezo buzzer alongside the LEDs.

I have adapted some code which was on Github.

Everything works perfectly and I was all set to install the system, but thought I would try with the solenoid lock on the bench first, as up until now I only had the relay hooked up bare.

Now the problems started.

Basically sometimes the Relay operates kicking the solenoid whilst other times it stalls as soon as it attempts to operate.

As soon as I remove the power to the 'heavy side' of the solenoid (18V and 2.0 amps) all is well again.?

I have written a simple 'blinky' to operate the relay with solenoid and that works perfectly, so its not a problem with the hardware.

Thanks for taking the time to look, and any help would be appreciated..

/* 
 Touch Sense Shield Example using the MPR121 touch sensor IC 

 by: Aaron Weiss, based on the MPR121 Keypad Example by Jim Lindblom
 further modified by Jim again! - 6/22/11
 
 SparkFun Electronics
 created on: 6/22/11
 license: OSHW 1.0, http://freedomdefined.org/OSHW
 
 Pressing a pad will print the corresponding number.
 
 Hardware: 3.3V or 5V Arduino

 Notes: The Wiring library is not used for I2C, a default atmel I2C lib
        is used. Be sure to keep the .h files with the project. 
*/

// include the atmel I2C libs Edited by BANANA
#include "mpr121.h"
#include "i2c.h"

// 11 max digits used
#define DIGITS 11
// Match key inputs with electrode numbers
#define ONE 8
#define TWO 5
#define THREE 2
#define FOUR 7
#define FIVE 4
#define SIX 1
#define SEVEN 6
#define EIGHT 3
#define NINE 0

//extras (not connected to button)
#define ELE9 9
#define ELE10 10
#define ELE11 11

//interupt pin
int irqPin = 2;  // D2
char MyCode[] = {'1', '5', '1', '1'}; //<===================================PASSWORD
char Pressed[] = {'15', '15', '15', '15'};
int touchcount = 0; //a count for how many buttons have been pressed
boolean Code = false;
boolean flashing = false;
const int KeyLEDGreen = 10;
const int KeyLEDRed = 11;
const int buzzer = 9;








void setup()
{
  //make sure the interrupt pin is an input and pulled high
  pinMode(irqPin, INPUT);
  pinMode(KeyLEDGreen, OUTPUT);
  pinMode(KeyLEDRed, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(6, OUTPUT);
  digitalWrite(irqPin, HIGH);
  
  //configure serial out
  Serial.begin(9600);

 
  
  // initalize I2C bus. Wiring lib not used. 
  i2cInit();
  
  // initialize mpr121
  mpr121QuickConfig();
  
  // Create and interrupt to trigger when a button
  // is hit, the IRQ pin goes low, and the function getNumber is run. 
  attachInterrupt(0, getNumber,LOW);
  
  // prints 'Ready...' when you can start hitting numbers
  Serial.println("Ready...");


//  pinMode(KeyLEDRed, OUTPUT);
//  digitalWrite(KeyLEDRed, LOW);
//  pinMode(KeyLEDGreen, OUTPUT);
//  digitalWrite(KeyLEDGreen, LOW);
  digitalWrite(6,LOW);
}


  


void loop(){
         

  
if ((MyCode[0] == Pressed[0]) && (MyCode[1] == Pressed[1]) && (MyCode[2] == Pressed[2]) && (MyCode[3] == Pressed[3]) && (touchcount % 4 == 0)) {
    Code = true;
  }
  else {
    Code = false;
  }
  //checks to see if password is correct after every 4 keys entered
  if ((touchcount % 4 == 0) && (Code == false) && (touchcount > 0)) {   ///if password is incorrect
      flashing = true;
      for (int i = 0; i < 4; i++) {                                       //flash led on/off 4 times
       digitalWrite(KeyLEDRed, HIGH);
       delay(200);
       digitalWrite(KeyLEDRed, LOW);
       delay(200);
       
        
  } 
        
        digitalWrite(buzzer, HIGH);
        delay(300);
        digitalWrite(buzzer, LOW);
        delay(300);
        digitalWrite(buzzer, HIGH);
        delay(300);
        digitalWrite(buzzer, LOW);


       
  touchcount = 0;  
  flashing = false;  
  }  
  
 else if ((touchcount % 4 == 0) && (Code == true)) {                  //if the password is correct
    digitalWrite(KeyLEDGreen, HIGH);  
    digitalWrite(6, HIGH);
    digitalWrite(buzzer, HIGH); 
        

}
    
  else                                     {                       //if no code was entered
    digitalWrite(KeyLEDGreen, LOW);
    digitalWrite(6, LOW);
    digitalWrite(buzzer, LOW);   
  }   

}
  





void getNumber()
{
 if (flashing == false) {
    int i = 0;
    int touchNumber = 0;
    uint16_t touchstatus;
    char digits[DIGITS];

    touchstatus = mpr121Read(0x01) << 8;
    touchstatus |= mpr121Read(0x00);

    for (int j = 0; j < 12; j++) // Check how many electrodes were pressed
    {
      if ((touchstatus & (1 << j)))
        touchNumber++;
    }

    if (touchNumber == 1)
    {
      touchcount++;
      if (touchstatus & (1 << SEVEN)) {
        for (int i = 0; i < 3; i++) {
          Pressed[i] = Pressed[i + 1];
        }
        Pressed[3] = '7';
        digits[i] = '7';
      }
      else if (touchstatus & (1 << FOUR)) {
        for (int i = 0; i < 3; i++) {
          Pressed[i] = Pressed[i + 1];
        }
        Pressed[3] = '4';
        digits[i] = '4';
      }
      else if (touchstatus & (1 << ONE)) {
        for (int i = 0; i < 3; i++) {
          Pressed[i] = Pressed[i + 1];
        }
        Pressed[3] = '1';
        digits[i] = '1';
      }
      else if (touchstatus & (1 << EIGHT)) {
        for (int i = 0; i < 3; i++) {
          Pressed[i] = Pressed[i + 1];
        }
        Pressed[3] = '8';
        digits[i] = '8';
      }
      else if (touchstatus & (1 << FIVE)) {
        for (int i = 0; i < 3; i++) {
          Pressed[i] = Pressed[i + 1];
        }
        Pressed[3] = '5';
        digits[i] = '5';
      }
      else if (touchstatus & (1 << TWO)) {
        for (int i = 0; i < 3; i++) {
          Pressed[i] = Pressed[i + 1];
        }
        Pressed[3] = '2';
        digits[i] = '2';
      }
      else if (touchstatus & (1 << NINE)) {
        for (int i = 0; i < 3; i++) {
          Pressed[i] = Pressed[i + 1];
        }
        Pressed[3] = '9';;
        digits[i] = '9';
      }
      else if (touchstatus & (1 << SIX)) {
        for (int i = 0; i < 3; i++) {
          Pressed[i] = Pressed[i + 1];
        }
        Pressed[3] = '6';
        digits[i] = '6';
      }
      else if (touchstatus & (1 << THREE)) {
        for (int i = 0; i < 3; i++) {
          Pressed[i] = Pressed[i + 1];
        }
        Pressed[3] = '3';
        digits[i] = '3';
      }


      Serial.print(digits[i]);

      i++;
    }
    //do nothing if more than one button is pressed
    else if (touchNumber == 0)

      ;
    else
      ;
  }
}


  
  /code]

a RED led for incorrect inputs, a green one for correct input

Not a GREEN one? Why not?

char Pressed[] = {'15', '15', '15', '15'};

That's stupid. Single quotes are for SINGLE characters.

    else if (touchNumber == 0)

      ;
    else
      ;

WTF?

Why are you using an interrupt to handle pressing on the touch panel? Why are none of the variables shared between the interrupt service routine and loop() volatile?

When you remove power from the solenoid, the code and relay work fine. When you apply power to the solenoid, you have problems. Why would you even suspect that the code is the problem? You have a hardware problem, plain and simple.

"When you remove power from the solenoid, the code and relay work fine. When you apply power to the solenoid, you have problems. Why would you even suspect that the code is the problem? You have a hardware problem, plain and simple."

because as i mentioned in the original post -

"I have written a simple 'blinky' to operate the relay with solenoid and that works perfectly, so its not a problem with the hardware."

But that code is significantly different, because '15' will fit in an int, but not a char.

Thanks, I must admit that the

char Pressed[] = {'15', '15', '15', '15'};/code] was in the original Sketch that I copied from github and amended to suit my application. As such I have no idea what it actually does....

Should I change it to something else ?

In the meantime i'll put some time in to researching that line of code.

Should I change it to something else ?

Yes.

char Pressed[] = {'S', 'F', 'S', 'F'};