Declaring digital signal

It wouldn't know that anyway. This is a receiveEvent not a requestEvent. It gets what it gets, it can't know in advance how much that will be.

Not trying to argue. I'm just asking. The Wire.send() function buffer the data. Nothing gets sent until Wire,endTransmission gets called. At that time, the Wire instance knows how much data there is in the buffer, so the receive event could get called as soon as data starts coming in, and the function could be told how much data to expect. Your other post indicates that this is not the case, though.

If not, though, I wonder what the purpose of the Wire.available() function is. If the receive event handler is told how much data to receive, why is there another function to determine how much data there is to read?

At that time, the Wire instance knows how much data there is in the buffer, so the receive event could get called as soon as data starts coming in ...

Well they are at different ends. The sending end knows how much is in the buffer, the receiving end doesn't know how much to expect.

And in fact with the Wire.requestFrom the receiving end knows how much it requested, that won't necessarily be how much it gets (however it NAKs once the correct amount is received so it won't get more, but it might get less).

I wonder what the purpose of the Wire.available() function is.

I suppose it is really a helper function. You pull out a byte at a time with Wire.receive, so it tells you how much is left in the internal buffer.

Perhaps more importantly, the onRequest event doesn't supply howMany (unlike the onReceive event). So without Wire.available you don't know how much data you actually got when you requested it. Maybe you requested 10 bytes but got 5.

Thanks a bunch for this useful information :slight_smile:

I wasn't around as the Z-axis of the Reprap got jammed and had to disassemble almost half the machine (not that anyone asked).

I used the code with Master in Reprap and Slave in Arduino (without any change). Yes, I can see the LED flash every second. But

1- A single command is taking a lot of time to execute. I put in
G28 X0 Y0
G1 X50 Y50
and it took round about 1min 40 sec for the first command and almost the same for the second (give or take 5 sec).
Clearly, it is not good scenario for printing.

2- And the LED is not in sync with the commands. It is just blinking. Whether I give some commands or not, it just keeps on blinking :~
According to the coding, it should work properly with any string starting with G1 (or G28) regardless of what follows it.

I removed the delays in the Reprap (Master) code and now the commands are getting executed at normal speed. I do understand why the delays were there but because of them the commands were taking a lot of time to execute. That is why I removed them.

But now the LED is continuously ON and whenever I send G1 or G28, it turns OFF and then ON back again after the execution. It does flicker very quickly after the execution of each command, understandably as there is no delay.

Is it possible to keep the LED in ON state as long as the G1 command is executed. In short, if I want the LED to remain ON as long as I am sending G1 and only to turn OFF (send 0) when it receives G28, how to do that?

Or let's say
G28 X0 Y0 (remain OFF as long as this is executed)
G1 X10 Y10 (remain ON as long as this is executed)

Wait for a string with G28 to turn OFF otherwise if a string of G1 is being received, stay ON.

You guys have been extremely patient with somebody like me who has the programming skills of a bag of rocks and I really appreciate that. But I am still not there yet, so please keep posting your valuable comments :slight_smile:

I posted code that did exactly what you wanted (from the test Arduino). When it sent G1 it turned the LED on and when it sent G28 it turned it off.

Now you want help with code that you haven't posted. Sorry that's impossible.

I uploaded the same code that you posted without any change but as I mentioned in a previous post, with delays in the Master code, the commands are taking too much time to execute. I have to wait for round about 1 min 40 sec for one command (G28 X0 Y0) to execute.

Then, I removed the delays (same code that you posted). After their removal, the commands were executing at normal speed.
But now with delays removed, the LED remains ON and turns OFF when it gets G1 or G28. It flickers very quickly when one command is executed and turns back ON.

I hope I am making sense here :relaxed:

Not really. Perhaps someone else can explain better than I can.

The code (the master code) was purely to test the slave code (the one that lights the LED). Of course if you just upload the master code the LED will flicker on and off every second. I don't understand what you are doing.

If you have somehow integrated my code with the reprap code, can you post that?

I did integrate the Master code with the Reprap code. The Reprap code is extremely long, so I will just put in the bits where I added the Master code.

........

#define reprap 6
#define slave 5


void setup()
{
  disableTimerInterrupt();
  setupTimerInterrupt();
  interruptBlink = 0;
  pinMode(DEBUG_PIN, OUTPUT);
  debugstring[0] = 0;
  led = false;
  
  setupGcodeProcessor();
  
  ex[0] = &ex0;
#if EXTRUDER_COUNT == 2  
  ex[1] = &ex1;
#endif  
  extruder_in_use = 0; 
  
  head = 0;
  tail = 0;
  
  cdda[0] = &cdda0;
  cdda[1] = &cdda1;  
  cdda[2] = &cdda2;  
  cdda[3] = &cdda3;
  
  for(byte i = 0; i < 4; i++)
    cdda[i]->set_units(true);
  
  //setExtruder();
  
  init_process_string();
  

  Serial.begin(HOST_BAUD);
  Serial.println("start");
  
 Wire.begin(reprap);
  
  
#if MOTHERBOARD == 2
    #ifndef GEN6 //Not used in generation 6 electronics
      pinMode(PS_ON_PIN, OUTPUT);  // add to run G3 as built by makerbot
      digitalWrite(PS_ON_PIN, LOW);   // ditto
    #endif
    delay(2000);    
    rs485Interface.begin(RS485_BAUD);  
#endif

  setTimer(DEFAULT_TICK);
  enableTimerInterrupt();
}

// This does a hard stop.  It disables interrupts, turns off all the motors 
// (regardless of DISABLE_X etc), and calls extruder.shutdown() for each
// extruder.  It then spins in an endless loop, never returning.  The only
// way out is to press the reset button.

void shutdown()
{
  // No more stepping
  
  disableTimerInterrupt();
  
  // Delete everything in the ring buffer
  
  cancelAndClearQueue();
  
  // LED off
  
  digitalWrite(DEBUG_PIN, 0);

  // Motors off
  
#if MOTHERBOARD > 0  
  digitalWrite(X_ENABLE_PIN, !ENABLE_ON);
  digitalWrite(Y_ENABLE_PIN, !ENABLE_ON);
  digitalWrite(Z_ENABLE_PIN, !ENABLE_ON);
#endif

  // Stop the extruders
  
  for(byte i = 0; i < EXTRUDER_COUNT; i++)
    ex[i]->shutdown();
  
  // Till the end of time...
  
  for(;;); 
}

//long count = 0;
//int ct1 = 0;

#define I2CMODE 1
#define SERIALMODE 0

int inputMode = SERIALMODE;

void loop()
{
  
Wire.beginTransmission (slave);
  Wire.send ("G1"); 
  Wire.endTransmission();

  delay (1000);
  
  Wire.beginTransmission (slave);
  Wire.send ("G28");
  Wire.endTransmission();

  delay (1000);

 
  manageAllExtruders();
   
   if (inputMode == SERIALMODE)
     get_and_do_command();

     get_and_do_command_i2c();
     
#if MOTHERBOARD == 2
   talker.tick();
#endif
}
........

If you want to have a look at the code yourself, I have attached it here.

FiveD_GCode_Interpreter_With_i2c_and_M117.zip (36.6 KB)

Javaid:
I did integrate the Master code with the Reprap code. The Reprap code is extremely long, so I will just put in the bits where I added the Master code.

...

void loop()
{
 
Wire.beginTransmission (slave);
 Wire.send ("G1");
 Wire.endTransmission();

delay (1000);
 
 Wire.beginTransmission (slave);
 Wire.send ("G28");
 Wire.endTransmission();

delay (1000);

manageAllExtruders();

........

Your thought processes confuse me. How is this, in any way, going to be anything to do with whether or not the reprap is printing?

And in the end, I am messing up everything (so stupid of me)

...

You guys have been extremely patient with somebody like me who has the programming skills of a bag of rocks ...

Describing yourself as stupid isn't really helping. Perhaps explain what you are thinking.

Why, for example, did you sound really surprised that "A single command is taking a lot of time to execute." - after you put a 2 second delay into loop?

There is a software called Repsnapper that is used to print parts using Reprap machine.
A USB cable is attached to the Reprap (Gen 6 board to be exact) and the other end is connected to computer.
In the computer, Repsnapper is opened and anything that needs to be printed or any movement of any axes, is made by that software.
The software controls the printing. So if I am to send any command with G1 or G28, I will write a g-code and then upload it in the repsnapper and repsnapper will execute the commands.

This has been the confusion all along, I guess.
Remember the times, I keep asking how to tell when the Reprap is printing. Every command is being send through Repsnapper.
I am attaching an image for you.

......whether or not the reprap is printing?

The Reprap will print when I will click PRINT on the Repsnapper software.

You could see the options to loadg-code, stl file etc.
Everything goes through this software.

I should put up the image back again of the whole setup.

Computer is connected to Reprap via USB but Reprap is controlled by Repsnapper which is the host software. One can't print without a host software. The coding in the Reprap doesn't print by itself. If I want to print say 4 squares in a row. I have to write a g-code, upload it in Repsnapper, click PRINT, then Reprap will print.

I hope everything is clear now.

Repsnapper uses the code in Reprap. If there is no code in reprap, then repsnapper can't do anything.

Every command is sent by Repsnapper. Since Arduino is connected to Reprap via I2C, the goal was to tell the Arduino when a command with G1 or G28 is sent by Repsnapper, so that it can turn the led ON/OFF.

I know I am emphasizing this point again and again but I have to make it clear that the code in Reprap can't do printing itself. It needs a host software that is Repsnapper. Since Repsnapper uses the code in Reprap, I made changes to that code, so that its effect can be seen via I2C at Arduino.