I am working on a 3D printer having a Generation 6 electronics board (atmega644). I have connected it with Arduino Uno and established communication via I2C. I need to send a signal from the printer to a pin on Arduino say pin 2. Whenever a g code is executed, the printer will send a signal to Arduino. So when G1 is executed, I want LED connected to pin 2 of Arduino to light up and when G28 is executed, I want it to be off. Currently, I have this in printer
#include <Wire.h>
//i2c address of the gen 6
int REP_RAP_ADDR = 4;
//my address
int CP_ADDR = 5;
void setup()
{
Wire.begin(4);
}
void loop
{
Wire.beginTransmission (5);
Wire.send ("G1");
Wire.send ("G28");
}
And this in Arduino.
#include <Wire.h>
//i2c address of the gen 6
int REP_RAP_ADDR = 4;
//my address
int CP_ADDR = 5;
int ledPin = 2;
void receiveEvent(int howMany)
{
while(0 < Wire.available()) // loop through all
Wire.receive(); // receive byte as a character
}
void setup()
{
Wire.begin(CP_ADDR);
Wire.onReceive(receiveEvent); // register event so that anything received on i2c is sent to the serial
pinMode(ledPin, OUTPUT);
}
void loop()
{
char str1[] = "G1";
char str2[] = "G28";
if (strcmp(str1, ____) == 0){
//turn dispenser on
digitalWrite(2, HIGH); }
if (strcmp(str2,_____ ) == 0) {
//turn dispenser off
digitalWrite(2, LOW);}
}
_____ is the string of incoming data which I am unable to store.
I should be comparing the received data with str1 or str2 but I am unable to figure out how to store the incoming data and then comparing step will come afterwards.
First a little help on storing the incoming data into an array.
to get your wire buffer into an array you can try this
char incoming[4]; // Set this to the maximum expected string length +1 for a null char
int length;
void Loop(){
if(Wire.available > 0){
delay(100); // The arduino is faster than serial, wait to make sure all the data is there, otherwise you'll end up with only half your data in the array
length = Wire.available()
for (int i = 0; i < length; i++) {
if(i > 4){ //prevent data corruption in case you accidentally receive more than the array can hold
incoming[4] = "\0"; //put a null char at the end of the array
break;
}
incoming[i] = Wire.receive();
}
}
// Compare function goes here
}
Shouldn’t it go into the void receiveEvent, like this
void receiveEvent(int howMany)
{
char incoming[4]; // Set this to the maximum expected string length +1 for a null char
int length;
while(0 < Wire.available()) // loop through all
{
delay(100);
length = Wire.available();
for (int i = 0; i < length; i++) {
if(i > 4){ //prevent data corruption in case you accidentally receive more than the array can hold
incoming[4] = '\0'; //put a null char at the end of the array
break;
}
incoming[i] = Wire.receive();
}
}
char str1[] = "G1";
char str2[] = "G28";
if (strcmp(str1, incoming) == 0){
//turn dispenser on
digitalWrite(2, HIGH); }
if (strcmp(str2, incoming) == 0) {
//turn dispenser off
digitalWrite(2, LOW);}
}
This doesn’t seem very robust to me. What you should be doing is sending a null ‘\0’ at the end of your string to the Arduino (signifying a stop byte or something) and doing something like this on the receiving end
char buff[MAX_BUFF_SIZE];
void receive_data(void)
{
static int i = 0;
while(data_available())
{
buff[i] = received_byte();
if (buff[i]=='\0')
{
do_compare_strings_thingy(buff);
i = 0;
} else {
i++;
if (i > MAX_BUFF_SIZE) {i = 0;}
}
}
}
#include <Wire.h>
#define slave 5
#define LED 13
void receiveEvent (int howMany)
{
char buf [10];
byte i = 0;
while (Wire.available () > 0)
{
char c = Wire.receive ();
if (i < sizeof (buf) - 1) // check for overflow
buf [i++] = c;
} // end of while
buf [i] = 0; // terminating null
if (memcmp (buf, "G1", 2) == 0)
digitalWrite (LED, HIGH);
else if (memcmp (buf, "G28", 3) == 0)
digitalWrite (LED, LOW);
} // end of receiveEvent
void setup ()
{
Wire.begin (slave);
Wire.onReceive (receiveEvent);
pinMode (LED, OUTPUT);
} // end of setup
void loop()
{
// nothing in main loop
}
The Reprap works through a software Repsnapper that sends G-Codes in this format:
G28 X0 Y0
G1 X10 Y10
According to the coding, Arduino should recognize when a command with G1 or G28 is executed but currently the LED is not turning ON and OFF according to the commands given. It just keeps on blinking every second. What could be the problem here :?