Trying to use an Adafruit 1188 capacitive sensor with a MCP2515 CAN board. I am sending the correct CAN messages but not see the cap sensor working correctly. it you hover over the pad I have connected to the sensor it will show touched but not when you actually touch it. I also noticed that the touched LED doesn't work when I add in the code for the CAN board. If I only have the part for the cap board it works as expected. Any suggestions on what might be causing this issue?
#include <Wire.h>
#include <SPI.h>
#include <mcp2515.h>
#include <Adafruit_CAP1188.h>
unsigned long DeadTimeSet;
unsigned long DeadFault;
unsigned long DeadDelaySet;
unsigned long DeadDelay;
int DeadSet;
int Deadman;
int DeadmanInput;
int DeadOffSet;
int Analog0=0;
int AIn0 = A0; //Joystick 1 to Analog Input 0
int AIn1 = A1; //Joystick 1 to Analog Input 0
int Joystick1AIn = 0; //Joystick 1 Analog Input Integer
int Joystick1AInR = 0; //Joystick 1 Analog Redundant Input Integer
float Joystick1PSN = 0; //Joystick 1 Position
bool Joystick1Neutral = 0; //Joystick 1 Neutral Position
bool Joystick1Down = 0; //Joystick 1 Down Position
bool Joystick1Up = 0; //Joystick 1 Up Position
byte PSN1[1]; //Joystick1 Position Bytes
bool PSN1_B0b[7]; //Joystick1 Position Byte0 Bit[x]
bool PSN1_B1b[7]; //Joystick1 Position Byte1 Bit[x]
int AIn2 = A2; //Joystick 2 to Analog Input 2
int AIn3 = A3; //Joystick 2 to Analog Input 3
int Joystick2AIn = 0; //Joystick 2 Analog Input Integer
int Joystick2AInR = 0; //Joystick 2 Analog Redundant Input Integer
float Joystick2PSN = 0; //Joystick 2 Position
bool Joystick2Neutral = 0; //Joystick 2 Neutral Position
bool Joystick2Down = 0; //Joystick 2 Down Position
bool Joystick2Up = 0; //Joystick 2 Up Position
byte PSN2[1]; //Joystick2 Position Bytes
bool PSN2_B0b[7]; //Joystick2 Position Byte0 Bit[x]
bool PSN2_B1b[7]; //Joystick2 Position Byte1 Bit[x]
int AIn4 = A4; //Joystick 3 to Analog Input 4
int AIn5 = A5; //Joystick 3 to Analog Input 5
int Joystick3AIn = 0; //Joystick 3 Analog Input Integer
int Joystick3AInR = 0; //Joystick 3 Analog Redundant Input Integer
float Joystick3PSN = 0; //Joystick 3 Position
bool Joystick3Neutral = 0; //Joystick 3 Neutral Position
bool Joystick3Down = 0; //Joystick 3 Down Position
bool Joystick3Up = 0; //Joystick 3 Up Position
byte PSN3[1]; //Joystick3 Position Bytes
bool PSN3_B0b[7]; //Joystick3 Position Byte0 Bit[x]
bool PSN3_B1b[7]; //Joystick3 Position Byte1 Bit[x]
byte CAN_B[7]; //CAN Message 8 Bytes
struct can_frame canMsg1;
const int numReadings = 1; // number of reading in average
int readings1[numReadings]; // the readings from the analog input
int readings1redone[numReadings]; // the readings from the analog redudant input
int readIndex = 0; // the index of the current reading
int total1 = 0; // the running total
int Diff1 = 0;
int readings2[numReadings]; // the readings from the analog input
int readings2redone[numReadings]; // the readings from the analog redudant input
int total2 = 0; // the running total
int Diff2 = 0;
int readings3[numReadings]; // the readings from the analog input
int readings3redone[numReadings]; // the readings from the analog redudant input
int total3 = 0; // the running total
int Diff3 = 0;
MCP2515 mcp2515(8); //set CS Pin to 8
// Reset Pin is used for I2C or SPI
#define CAP1188_RESET 9
// CS pin is used for software or hardware SPI
#define CAP1188_CS 10
// These are defined for software SPI, need to hardware SPI
#define CAP1188_MOSI 11
#define CAP1188_MISO 12
#define CAP1188_CLK 13
//Hardware SPI, CS pin & reset pin
Adafruit_CAP1188 cap = Adafruit_CAP1188(CAP1188_CS, CAP1188_RESET);
void setup() {
cap.writeRegister(0x1F, B01011111);
delay(200);
// CAN Message For Toggle Postions.
canMsg1.can_id = 0x0CFDD748 | CAN_EFF_FLAG;
canMsg1.can_dlc = 8;
//while (!Serial);
//Serial.begin(9600);
mcp2515.begin();
mcp2515.setBitrate(CAN_250KBPS, MCP_16MHZ);
mcp2515.setNormalMode();
pinMode(9, OUTPUT);
//Serial.println("Example: Write to CAN");
}
void loop() {
Joystick1AIn = analogRead(AIn0); // read from the sensor:
Joystick1AInR = analogRead(AIn1);
Diff1=abs(Joystick1AIn - Joystick1AInR);
Analog0 = analogRead(AIn0);
//Serial.println(String(Analog0) + " AIn0");
//Serial.println(String(Joystick1AIn) + " Joystick1AIn");
if (Diff1 > 100) { // if A0 and A1 are different
Joystick1AIn = 512; // Set total1 to 512 middle
}
Joystick2AIn = analogRead(AIn2);
Joystick2AInR = analogRead(AIn3);
Diff2=abs(Joystick2AIn - Joystick2AInR);
if (Diff2 > 100) { // if A2 and A3 are different
Joystick2AIn = 512; // Set total2 to 512 middle
}
Joystick3AIn = analogRead(AIn4);
Joystick3AInR = analogRead(AIn5);
Diff2=abs(Joystick3AIn - Joystick3AInR);
if (Diff3 > 100) { // if A4 and A5 are different
Joystick3AIn = 512; // Set total3 to 512 middle
}
if (Joystick1AIn > 571) {
Joystick1PSN = -(511.5- (float)Joystick1AIn) * 2.443793; // 2.443793 is calculated value
Joystick1Neutral = 0;
Joystick1Down = 1;
Joystick1Up = 0;
} else if (Joystick1AIn < 451){
Joystick1PSN = (511.5- (float)Joystick1AIn) * 2.443793;
Joystick1Neutral = 0;
Joystick1Down = 0;
Joystick1Up = 1;
} else {
Joystick1PSN =0;
Joystick1Neutral = 1;
Joystick1Down = 0;
Joystick1Up = 0;
}
//Serial.println(String((int)Joystick1PSN) + " PSN1");
if (Joystick2AIn > 571) {
Joystick2PSN = -(511.5 - (float)Joystick2AIn) * 2.443793; // 2.443793 is calculated value
Joystick2Neutral = 0;
Joystick2Down = 1;
Joystick2Up = 0;
} else if (Joystick2AIn < 451){
Joystick2PSN = (511.5 - (float)Joystick2AIn) * 2.443793;
Joystick2Neutral = 0;
Joystick2Down = 0;
Joystick2Up = 1;
} else {
Joystick2PSN =0;
Joystick2Neutral = 1;
Joystick2Down = 0;
Joystick2Up = 0;
}
if (Joystick3AIn > 571) {
Joystick3PSN = -(511.5 - (float)Joystick3AIn) * 2.443793; // 2.443793 is calculated value
Joystick3Neutral = 0;
Joystick3Down = 1;
Joystick3Up = 0;
} else if (Joystick3AIn < 451){
Joystick3PSN = (511.5 - (float)Joystick3AIn) * 2.443793;
Joystick3Neutral = 0;
Joystick3Down = 0;
Joystick3Up = 1;
} else {
Joystick3PSN =0;
Joystick3Neutral = 1;
Joystick3Down = 0;
Joystick3Up = 0;
}
PSN1[1] = ((int)Joystick1PSN >> 8) & 0xFF;
PSN1[0] = ((int)Joystick1PSN & 0xFF);
PSN1_B0b[0] = bitRead(PSN1[0], 0);
PSN1_B0b[1] = bitRead(PSN1[0], 1);
PSN1_B1b[0] = bitRead(PSN1[1], 0);
PSN1_B1b[1] = bitRead(PSN1[1], 1);
PSN2[1] = ((int)Joystick2PSN >> 8) & 0xFF;
PSN2[0] = ((int)Joystick2PSN & 0xFF);
PSN2_B0b[0] = bitRead(PSN2[0], 0);
PSN2_B0b[1] = bitRead(PSN2[0], 1);
PSN2_B1b[0] = bitRead(PSN2[1], 0);
PSN2_B1b[1] = bitRead(PSN2[1], 1);
PSN3[1] = ((int)Joystick3PSN >> 8) & 0xFF;
PSN3[0] = ((int)Joystick3PSN & 0xFF);
PSN3_B0b[0] = bitRead(PSN3[0], 0);
PSN3_B0b[1] = bitRead(PSN3[0], 1);
PSN3_B1b[0] = bitRead(PSN3[1], 0);
PSN3_B1b[1] = bitRead(PSN3[1], 1);
bitWrite(CAN_B[0], 0, Joystick1Neutral);
bitWrite(CAN_B[0], 2, Joystick1Down);
bitWrite(CAN_B[0], 4, Joystick1Up);
bitWrite(CAN_B[0], 6, PSN1_B0b[0]);
bitWrite(CAN_B[0], 7, PSN1_B0b[1]);
bitWrite(CAN_B[2], 0, Joystick2Neutral);
bitWrite(CAN_B[2], 2, Joystick2Down);
bitWrite(CAN_B[2], 4, Joystick2Up);
bitWrite(CAN_B[2], 6, PSN2_B0b[0]);
bitWrite(CAN_B[2], 7, PSN2_B0b[1]);
bitWrite(CAN_B[4], 0, Joystick3Neutral);
bitWrite(CAN_B[4], 2, Joystick3Down);
bitWrite(CAN_B[4], 4, Joystick3Up);
bitWrite(CAN_B[4], 6, PSN3_B0b[0]);
bitWrite(CAN_B[4], 7, PSN3_B0b[1]);
CAN_B[1] = (int)PSN1[0] >> 2;
bitWrite(CAN_B[1], 6, PSN1_B1b[0]);
bitWrite(CAN_B[1], 7, PSN1_B1b[1]);
CAN_B[3] = (int)PSN2[0] >> 2;
bitWrite(CAN_B[3], 6, PSN2_B1b[0]);
bitWrite(CAN_B[3], 7, PSN2_B1b[1]);
CAN_B[5] = (int)PSN3[0] >> 2;
bitWrite(CAN_B[5], 6, PSN3_B1b[0]);
bitWrite(CAN_B[5], 7, PSN3_B1b[1]);
uint8_t touched = cap.touched();
Deadman = bitRead(touched, 0);
/*DeadmanInput = bitRead(touched, 0);
if(DeadmanInput == 0 && DeadOffSet == 0) {
DeadDelaySet = millis();
DeadOffSet = 1;
}
DeadDelay = millis() - DeadDelaySet;
if(DeadDelay > 50) {
Deadman = 0;
}
if(DeadmanInput = 1){
Deadman = 1;
}
*/
if(Deadman == 1 && DeadSet == 0) {
DeadTimeSet = millis();
DeadSet = 1;
//Serial.println("DeadTimeSet "+String(DeadTimeSet));
}
else if(Deadman == 0) {
DeadTimeSet = millis();
DeadSet = 0;
DeadOffSet = 0;
}
DeadFault = millis() -DeadTimeSet;
//Serial.println("DeadFault "+String(DeadFault));
if(DeadFault > 95000){
while (Deadman == 1) { //Wait for Deadman to return to 0
digitalWrite(9, LOW);
delay(100);
digitalWrite(9, HIGH);
delay(100);
digitalWrite(9, LOW);
DeadSet = 0;
delay(3000);
//Serial.println("RESET");
uint8_t touched = cap.touched();
Deadman = bitRead(touched, 0);
canMsg1.data[0] = 0xFF;
canMsg1.data[1] = 0xFF;
canMsg1.data[2] = 0xFF;
canMsg1.data[3] = 0xFF;
canMsg1.data[4] = 0xFF;
canMsg1.data[5] = 0xFF;
canMsg1.data[6] = 0xFF;
canMsg1.data[7] = 0xFF;
mcp2515.sendMessage(MCP2515::TXB1,&canMsg1);
}
}
CAN_B[7] = 0xFF;
bitWrite(CAN_B[7], 6, Deadman);
bitWrite(CAN_B[7], 7, 0);
canMsg1.data[0] = CAN_B[0];
canMsg1.data[1] = CAN_B[1];
canMsg1.data[2] = CAN_B[2];
canMsg1.data[3] = CAN_B[3];
canMsg1.data[4] = CAN_B[4];
canMsg1.data[5] = CAN_B[5];
canMsg1.data[6] = 0xFF;
canMsg1.data[7] = CAN_B[7];
mcp2515.sendMessage(MCP2515::TXB1,&canMsg1);
//Serial.println("Messages sent");
delay(100);
}