Hi All,

A newbie question and a really stupid one indeed:$

I want to store some incoming data into an array and then compare it with an array that I have defined. I am posting the program here but it is Arduino based and uses the Wire library. But I don't want you to look into that, just have a look at the receive event handler, whatever I receive there, I want to store it in an array. And then compare it with G1 or G28. I am pretty sure the code is not complete in anyway and there is a lot missing and a lot wrong as well. I am still trying to learn arrays and I would really appreciate if someone could help me out as well as explain a bit, if that is not too much to ask.

#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
   {
    char inData[512];
    byte index = 0; 
    char javaid = Wire.receive();
    Serial.print(javaid);         
    if(index < 512)
  {
      inData[index++] = javaid;
      inData[index] = '\0'; 
    
  }  
   }
}

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, "G1") == 0){
  
    
      //turn dispenser on
      digitalWrite(2, HIGH); }
    
    if (strcmp(str2, "G28") == 0) {
    
      //turn dispenser off
      digitalWrite(2, LOW);}  
}

Thank you in advance.

void loop()
{
 char str1[] = "G1";
 char str2[] = "G28"; 
  
  if (strcmp(str1, "G1") == 0){
  
    
      //turn dispenser on
      digitalWrite(2, HIGH); }
    
    if (strcmp(str2, "G28") == 0) {
    
      //turn dispenser off
      digitalWrite(2, LOW);}  
}

Could you explain to us when will these IF statements be false?

These IF statements, at the moment, are wrong because I am comparing str1 with G1 which will always be TRUE. I need to compare the incoming data with str1 (G1) which I am unable to do so far. The incoming data will be compared to str1 which is G1 and if the incoming data is a command executing G1, then the dispenser will be ON. I hope I am making some sense. This is what I am after.

P.S: G1 and G28 are g codes like in a CNC machine.

These IF statements, at the moment, are wrong because I am comparing str1 with G1 which will always be TRUE. I need to compare the incoming data with str1 (G1) which I am unable to do so far. The incoming data will be compared to str1 which is G1 and if the incoming data is a command executing G1, then the dispenser will be ON. I hope I am making some sense. This is what I am after.

P.S: G1 and G28 are g codes like in a CNC machine.

I can't tell if
1) you know what you want to do but just haven't done it yet. It's a later modification.
2) you have no idea how to do what you want to do.

It would help if you explain clearly what you need help with in detail.

I will go with option 1.

I want to send data from a 3D printer that has atmega644 on it. The printer works by receiving g codes. I want to send the g codes from the printer to Arduino Uno (having atmega328) via I2C protocol.
Therefore, when I receive G1 (which is a g-code) from printer, LED on pin 2 of Arduino should light up and when I receive G28, LED should turn off.

I am unable to
1- Put the incoming data into an array and save it
2- Compare the incoming data to see whether it is G1 or G28 to switch the LED on or off.

I hope it is clear enough. If not, do let me know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.