Go Down

Topic: I2C slave reply (Read 3079 times) previous topic - next topic

PaulS

Quote
I used

Where? I need to see where you used that.

In the loop() method on the slave, print the position of the stepper.

In the requestEvent callback, print when the callback is entered and when it is ended.

It may be that requestEvent needs to behave like an interrupt service request handler, in that it can not block. If that is the case, then I don't know what you will have to do. Perhaps you could just connect two other pins between the two Arduinos. On the "master" poll the pin of interest. On the "slave", set the pin LOW when the stepper is running. Set it HIGH when the stepper is done.

Hi Paul

Please check if this slave code is what you are looking for :
Basically ,

for delay of 100   - the current position of motor (when it stopped) is 7 and the request is called when the motor is at position 7 .
for delay of 4000 - the current position of motor is 2547 and the request is called when the motor is at position 2547 .

So is the request being called after the motor stops ? or is the delay interfering in some way ?



Code: [Select]


// I2C communication  1 Master- 1 Slave

// program for slave 4



#include <Wire.h>

#include <AccelStepper.h>

int incomingByte = 0;


// timer variables
long millisec = 0;
long microsec = 0;



// stepper motor pins: STEP, DIRECTION, STEPPER ON/OFF
AccelStepper Stepper1(1,2,3);



void setup() {
 
  Wire.begin(4);
  Wire.onReceive(receiveEvent);
  Wire.onRequest(requestEvent);

 
   
//Start Serial for debuging purposes
  Serial.begin(115200); // USB comm with PC

for(int i = 0;i <54;i++) {
    pinMode(i,OUTPUT);
    digitalWrite(i,LOW);
  }

// switch stepper chips off
  digitalWrite(4,HIGH);

Stepper1.setMaxSpeed(1000);

}


void receiveEvent(int howMany)
{
     if (Wire.available() > 0) {
     incomingByte = Wire.receive();
     }

   


     

void requestEvent()
{
   Serial.println(" Request is called when the motor is at position");
   Serial.println(Stepper1.currentPosition());
   
   while(Stepper1.currentPosition() != Stepper1.targetPosition())
   { //do nothing
   }
   digitalWrite(4,HIGH);
   Wire.send("Action complete");
 
  Serial.println("Request send when motor is at position");             
  Serial.println(Stepper1.currentPosition());
}


void loop()
{
     
   Stepper1.run();
 
   Serial.println(Stepper1.currentPosition());
 
 
        //Serial.print("  Selection:");Serial.println(incomingByte);
        switch(incomingByte) {
          case 1: digitalWrite(4,LOW); digitalWrite(3,HIGH); digitalWrite(2,HIGH); //Serial.println("Stepper1: forward");
                 
                  Serial.println("forward");
                  Stepper1.setCurrentPosition(0);
                  Stepper1.setAcceleration(500.0);
                  Stepper1.moveTo(4000);
                 
                  break;
         }
     // switch loop
     incomingByte = 0;   
} // main loop



PaulS

Please SHOW me the serial output.

Nick Gammon

@anishh2003 - please post your output. Don't just interpret it, or comment on it. Post it. For example, does the stuff below appear?

Code: [Select]
void requestEvent()
{
  Serial.println(" Request is called when the motor is at position");
  Serial.println(Stepper1.currentPosition());
 
  while(Stepper1.currentPosition() != Stepper1.targetPosition())
  { //do nothing
  }
  digitalWrite(4,HIGH);
  Wire.send("Action complete");
 
 Serial.println("Request send when motor is at position");              
 Serial.println(Stepper1.currentPosition());
}


Doing serial prints inside an ISR (which effectively is what requestEvent is) is a very bad idea. It is likely to block and hang. Does your code hang?

Quote
... it is just stuck.


Looks like it does.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Hi

Here is my output for a delay of 100

Code: [Select]



0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
forward
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7 Request is called when the motor is at position
7



@Nick :
The code hangs irrespective of whether the serial.println statments are there in the requestEvent() for me .

#20
Jul 25, 2012, 12:35 pm Last Edit: Jul 25, 2012, 06:27 pm by anishh2003 Reason: 1
Hi

Just did some further tests ...

i am able to get the motor running if I remove
Code: [Select]

while(Stepper1.currentPosition()!=Stepper1.targetPosition())
{ // do nothing
}



I think this code blocks the slave operation . Any idea what else I can replace this with .

Thanks

Go Up