Pages: 1 [2]   Go Down
Author Topic: I2C slave reply  (Read 2197 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46029
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

// 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

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46029
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please SHOW me the serial output.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

Here is my output for a delay of 100

Code:


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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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 .
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

Just did some further tests ...

i am able to get the motor running if I remove
Code:
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
« Last Edit: July 25, 2012, 11:27:57 am by anishh2003 » Logged

Pages: 1 [2]   Go Up
Jump to: