SparkFun CAN-BUS Shield

Hello,

Thanks again for all your help, after doing a bit more hunting online i found a sketch that would compile.
I have done my best to pull that one apart and add it to the one i wanted to work.

This might be a little crude and might not even work, but it dose compile and up load and the serial monitor looks good.
I am at work now and can not do a full test on the can bus outputs but fingers crossed:

See what you think (feel free to point out any really stupid mistakes)

#include <SoftwareSerial.h>
#include <mcp_can.h>
#include <SPI.h>

SoftwareSerial sLCD =  SoftwareSerial(3, 6); /* Serial LCD is connected on pin 14 (Analog input 0) */
#define COMMAND 0xFE
#define CLEAR   0x01
#define LINE0   0x80
#define LINE1   0xC0

/* Define Joystick connection */
#define UP     A1
#define RIGHT  A2
#define DOWN   A3
#define CLICK  A4
#define LEFT   A5

//#define CANint 2
#define LED2 8
#define LED3 7
MCP_CAN CAN0(10);                                      

#define NOP __asm__ ("nop\n\t")

void setup() {
  Serial.begin(115200);
  Serial.println("Init...");

  sLCD.begin(9600);
  clearLCD();
  sLCD.print("RX-8 CAN control");
  sLCD.write(COMMAND);
  sLCD.write(LINE1);
  sLCD.print("www.cantanko.com");

  Serial.println("Setup pins");
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
//  pinMode(CAN0, INPUT);
  pinMode(UP,INPUT);
  pinMode(DOWN,INPUT);
  pinMode(LEFT,INPUT);
  pinMode(RIGHT,INPUT);
  pinMode(CLICK,INPUT);

  Serial.println("Enable pullups");
  digitalWrite(LED2, LOW);
  digitalWrite(UP, HIGH);       /* Enable internal pull-ups */
  digitalWrite(DOWN, HIGH);
  digitalWrite(LEFT, HIGH);
  digitalWrite(RIGHT, HIGH);
  digitalWrite(CLICK, HIGH);

  Serial.println("CAN init:");
  if (CAN0.begin(CAN_500KBPS) == CAN_OK) {
    Serial.println("OK!");
  } else {
    Serial.println("fail :-(");
    while (1) {
      Serial.print("Zzz... ");
      delay(1000);
    }
  }
  // Debug: iu

  //sLCD.begin(9600);
  //clearLCD();
  Serial.println("Good to go!");
}

unsigned char stmp[8] = {0, 0, 0, 0, 0, 0, 0, 0};
unsigned char otmp[8] = {255,255,255,255,255,255,255,255};
unsigned char statusPCM[8] = {125,0,0,0,156,0,0,0};
unsigned char statusMIL[8] = {140,0,0,0,0,0,0,0};
unsigned char statusDSC[8] = {0,0,0,0,0,0,0,0};
char message[17];

unsigned int i=0;
unsigned int j=0;
unsigned int pid=0;

void loop() {
  if(digitalRead(UP)==0) {
    Serial.println("PID cycling");
    for (pid=0x4B0;pid<0x500;pid++) {
      Serial.print("PID ");
      Serial.print(pid, HEX);
      Serial.println(" 0x00...");
      CAN0.sendMsgBuf(pid, 0, 8, stmp);
      delay(500);
      Serial.print("PID ");
      Serial.print(pid, HEX);
      Serial.println(" 0xFF...");
      CAN0.sendMsgBuf(pid, 0, 8, otmp);
      delay(500);
      Serial.print("PID ");
      Serial.print(pid, HEX);
      Serial.println(" 0x00...");
      CAN0.sendMsgBuf(pid, 0, 8, stmp);
      delay(500);
    }
  }

  if(digitalRead(DOWN)==0) {
    Serial.println("Down");
    while (1) {
      if (i && 0x1) CAN0.sendMsgBuf(0x201,0,8,statusPCM);
      if (i && 0xF) CAN0.sendMsgBuf(0x212,0,8,statusDSC);
      if (i && 0xF) CAN0.sendMsgBuf(0x420,0,8,statusMIL);
      if (i && 0xFFFF) {
          statusPCM[0]++;
          if (j==13) statusPCM[4]=statusPCM[0];
          if (j==14) statusPCM[4]=statusPCM[0];
          if (statusPCM[0] > 120) {
              statusPCM[0]=0;
              j++;
              switch (j) {
                case (5):
                  statusMIL[4]=1;
                  clearAndPrint("   oil pressure");
                  break;
                case (6):
                  statusMIL[4]=0;
                  clearAndPrint("No oil pressure");
                  break;
                case (7):
                  statusMIL[4]=1;
                  clearAndPrint("   oil pressure");
                  break;
                case (8):
                  statusMIL[6]=0b00000010;
                  clearAndPrint("Low water MIL");
                  break;
                case (9):
                  statusMIL[6]=0b10000000;
                  clearAndPrint("Oil presure MIL");
                  break;
                case (10):
                  statusMIL[6]=0b01000000;
                  clearAndPrint("Bat charge MIL");
                  break;
                case (11):
                  statusMIL[6]=0b00000000;
                  statusMIL[5]=0b01000000;
                  clearAndPrint("Check engine MIL");
                  break;
                case (12):
                  statusMIL[5]=0b10000000;
                  clearAndPrint("Check engine -_-");
                  break;
                case (13):
                  statusMIL[5]=0b00000000;
                  clearAndPrint("Speedo ramp");
                  break;
                case (15):
                  statusDSC[6]=0b00000100;
                  clearAndPrint("ETC Disabled");
                  break;
                case (16):
                  statusDSC[6]=0b00001000;
                  clearAndPrint("ETC Active -_-_-");
                  break;
                case (17):
                  statusDSC[5]=0b00000000;
                  statusDSC[4]=0b01000000;
                  clearAndPrint("Brake fail MIL");
                  break;
                case (18):
                  statusDSC[4]=0b00001000;
                  clearAndPrint("ABS MIL");
                  break;
                case (19):
                  statusDSC[4]=0b00000000;
                  statusDSC[3]=0b00000100;
                  clearAndPrint("DSC off");
                  break;
                case (20):
                  statusDSC[3]=0b00000000;
                  clearLCD;
                  break;
                case (21):
                  clearLCD();
                  sLCD.print("RX-8 CAN control");
                  sLCD.write(COMMAND);
                  sLCD.write(LINE1);
                  sLCD.print("www.cantanko.com");
                  break;
              }
          }
      }
      delay(30);
      i++;
    }      
  }

  if(digitalRead(LEFT)==0) {
    Serial.println("Left: RPM sweep");
    for (i=0;i<129;i++) {
      stmp[0]=i;
      stmp[4]=i;
      Serial.println(i);
      for (j=0;j<10;j++){
        CAN0.sendMsgBuf(0x201, 0, 8, stmp);
        delay(20);
      }
    }
  }

  if(digitalRead(RIGHT)==0) {
    Serial.println("Right: Fuel sweep");
    for (j=5;j<8;j++) {
      stmp[3]=0b11111111;
      for (i=0;i<256;i++) {
        stmp[j]=i;
        Serial.print("Byte ");
        Serial.print(j);
        Serial.print(" bits ");
        Serial.print(i, BIN);
        Serial.print(" value ");
        Serial.println(i);
        CAN0.sendMsgBuf(0x212, 0, 8, stmp);
        delay(500);
      }
    }
  }
}

void clearLCD(void) {
  sLCD.write(COMMAND);
  sLCD.write(CLEAR);
}

void clearAndPrint(String text) {
  clearLCD();
  sLCD.print(text);
}

Thanks for the tip on posting code too! :smiley: