Program Slave
#include <mcp_can.h> //menggunakan library MCP untuk CAN bus
#include <SPI.h> //menggunakan library untuk sambungan SPI
//potensio for adjusting value
int Ping = 7;
int PingValue = 0;
int PingxValue = 0;
int potensioensio = A0;
int potensioensioValue = 0;
int potensioensioxValue = 0;
int ldr = A1;
int ldrValue = 0;
int ldrxValue = 0;
char data_input;
int PING = 2;
int potensio = 3;
int LDR = 6;
int button_1;
int button_2;
int button_3;
const int SPI_CS_PIN = 10; //pin SPI pada Shield v1.0
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup()
{
Serial.begin(115200); // menyesuaikan baudrate menjadi 115200
// init can bus, baudrate: 100k
if (CAN.begin(CAN_500KBPS) == CAN_OK) Serial.print("can init ok!!\r\n"); // init can bus : baudrate = 500k
else Serial.print("Can init fail!!\r\n");
pinMode(Ping, INPUT);
pinMode(potensio, INPUT);
pinMode(ldr, INPUT);
}
//Some sample CAN messages
unsigned char msg1[8] = {0, 1, 2, 3, 4, 5, 6, 7}; //deklarasi array msg1
unsigned char msg2[8] = {0xFF, 0x01, 0x10, 0x0A, 0x00, 0x00, 0x00, 0x00}; //deklarasi array msg2
unsigned char msg3[4] = {0xFF, 0x01, 0x10, 0x0A}; //deklarasi array msg3
void loop()
{
data_input = Serial.read();
button_1 = digitalRead(Ping);
button_2 = digitalRead(potensio);
button_3 = digitalRead(ldr);
// establish variables for duration of the Ping, and the distance result
// in inches and centimeters:
long duration, inches, cm;
// The Ping))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(Ping, OUTPUT);
digitalWrite(Ping, LOW);
delayMicroseconds(2);
digitalWrite(Ping, HIGH);
delayMicroseconds(5);
digitalWrite(Ping, LOW);
// The same pin is used to read the signal from the Ping))): a HIGH pulse
// whose duration is the time (in microseconds) from the sending of the Ping
// to the reception of its echo off of an object.
pinMode(Ping, INPUT);
duration = pulseIn(Ping, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
potensioensioValue = analogRead(potensioensio);
potensioensioxValue = potensioensioValue / 4; //membagi nilai sensor dengan 4 lalu menjadikannya nilai cantxvalue
ldrValue = analogRead(ldr);
ldrxValue = ldrValue / 4; //membagi nilai sensor dengan 4 lalu menjadikannya nilai cantxvalue
Serial.print("Nilai Ping(CM): "); //mencetak "cantxValue: "
Serial.print(cm); //mencetak nilai cantxValue
Serial.print("\tNilai potensioensio: "); //mencetak "cantxValue: "
Serial.print(potensioensioxValue); //mencetak nilai cantxValue
Serial.print(" \t");
Serial.print("Nilai ldr: "); //mencetak "cantxValue: "
Serial.print(ldrxValue); //mencetak nilai cantxValue
Serial.println();
delay(100);
//Create data packet for CAN message
unsigned char canMsg[3] = {cm, potensioensioxValue , ldrxValue};
// send data: id = 0x123, standrad frame, data len = 3, stmp: data buf
CAN.sendMsgBuf(0x07B, 0, 3, canMsg); //mengirim data
//ID CAN Bus di set menjadi 0x07B
//frame standar
//ukuran array len = 3
//data array dari canMsg
delay(50); // send data per 100ms
}
long microsecondsToCentimeters(long microseconds) {
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The Ping travels out and back, so to find the distance of the object we
// take half of the distance travelled.
return microseconds / 29 / 2;
}
Program Master
#include <mcp_can.h> //menggunakan library MCP untuk CAN bus
#include <SPI.h> //menggunakan library untuk sambungan SPI
#include <LiquidCrystal.h> //menggunakan library LiquidCrystal untuk LCD
#include <stdio.h>
#define INT8U unsigned char
#define INT32U unsigned char
char data_input;
int code;
INT8U Flag_Recv = 0; //deklarasi Flag_recv = 0 dengan tipe data unsigned int 8
INT8U len = 0; //deklarasi len = 0 dengan tipe data unsigned int 8
INT8U buf[8]; //deklarasi buf = 0 dengan tipe data unsigned int 8
INT32U canId = 0x000; //deklarasi canId = 0 dengan tipe data unsigned int 32
char str[20];
const int SPI_CS_PIN = 10; //pin SPI pada Shield v1.0
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup()
{
CAN.begin(CAN_500KBPS); // init can bus : baudrate = 100k
attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt
Serial.begin(115200); // menyesuaikan baudrate menjadi 115200
}
void MCP2515_ISR()
{
Flag_Recv = 1; //deklarasi nilai flag receiver
}
void loop()
{
if(Flag_Recv) // check if data was recieved
{
Flag_Recv = 0; // clear flag
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
canId = CAN.getCanId(); //mendapatkan id can bus
//Print data to the serial console
//and the LCD display
Serial.println("PING POT LDR");
//This loops through each byte of data and prints it
for(int i = 0; i<len; i++) // menampilkan data lalu data setelahnya dibaris selanjutnya
{
Serial.print(buf[i]);Serial.print("\t"); //mencetak nilai buf
}
Serial.println();
delay(100); //tunda waktu selama 50 milisekon
}
}