Wire library and stepper motors help

hello all hope for some help, more in my understanding of how to use the wire library. My project involves 3 stepper motors (28BYJ-48 and the ULN2003 Driver Boards for each) and 2 arduino nanos, plus a few other parts, a DHT11, 3 HC-SR04's, and a joystick w/push button. I have 2 step motors and the 2 ultrasonic sensors on the slave device. the master device has 1 step motor, the joystick, the DHT11, and 1 ultrasonic sensor. the code so far doesn't reflect the DHT11 or the sonars. my goal right now is to get the slave step motors to function properly. I am trying to replicate the step motor setup i am using in the master device on the slave device to align all 3 step motors, then let the loop run, which should just report back to the master device the step motor angle and any distance from the sonars...

ok, so in the code the master code seems to be working fine, but the slave is not... doesn't do anything. the slave code should just print the value being sent from the master.

MASTER

// Include Stepper Library
#include <Stepper.h>

// Include Wire Library
#include <Wire.h>

// Define Joystick Y-axis input Pin to Arduino
#define y_key A0

// Define Joystick Button input Pin to Arduino
#define buttonPin 8

// Define System Start Button
#define SystemStartButton 7

#define STEPS 12   // adjust motor step count i.e.(8 motor steps = 1 step)
// directly effects motor speed, if STEPS increased,
// than motor speed below should be decreased.

// Define Slave I2C Address
#define Center_Stepper_Address 8               // address set for center stepper motor
#define Left_Right_Stepper_Address 9              // address set for left stepper motor
//#define Right_Stepper_Address 10              // address set for right stepper motor

const int cw = (-STEPS);            // move stepper clockwise
const int ccw = (STEPS);            // move stepper counter-clockwise
int stepCount_C = 0;
//int stepCount_L = 0;                // counts steps moved
//int stepCount_R = 0;                // counts steps moved
int y_pos;                          // placeholder for Y-axis position
int buttonPushCounter = 0;          // counter for the number of button presses
int buttonState = 0;                // current state of the Joystick Button
int lastButtonState = 0;            // previous state of the Joystick Button
int SystemStartButtonState = 0;     // current state of the System Start Button
//int SystemStartButtonLastState = 0; // previous state of the System Start Button

// create an instance of the stepper class using the steps and pins
Stepper stepper_Center(STEPS, 9, 11, 10, 12);
// create an instance of the stepper class using the steps and pins
//Stepper stepper_Left(STEPS, 9, 11, 10, 12);
// create an instance of the stepper class using the steps and pins
//Stepper stepper_Right(STEPS, 5, 7, 6, 8);

void setup() {
  // put your setup code here, to run once:
  pinMode (y_key, INPUT) ;
  pinMode (buttonPin, INPUT_PULLUP) ;
  //pinMode (SystemStartButton, INPUT) ;
  Serial.begin(9600);         // set the baud rate for serial communication
  Wire.begin(Center_Stepper_Address);

  //dht.begin();
  stepper_Center.setSpeed(2400);       // Set motor speed pulse duration
  while (buttonPushCounter < 5) {
    y_pos = analogRead(y_key);
    buttonState = digitalRead(buttonPin);
    // Delay a little bit to avoid bouncing
    delay(30);
    // compare the buttonState to its previous state
    if (buttonState != lastButtonState) {
      // if the state has changed, increment the counter
      if (buttonState == LOW) {
        // if the current state is LOW then the button went from off to on:
        buttonPushCounter++;
        //      Serial.println("on");
        //      Serial.print("number of button pushes: ");
        Serial.println(buttonPushCounter);
      }
    }
    // save the current state as the last state, for next time through the loop
    lastButtonState = buttonState;

    switch (buttonPushCounter) {
      case 1:    // Center Radar
        if (y_pos >= 515) {
          stepper_Center.step(cw);
          //delay(10);
        }
        if (y_pos <= 505) {
          stepper_Center.step(ccw);
          //delay(10);
        }
        break;
      case 2:    // Left Radar
        if (y_pos <= 500) {
         
          Wire.beginTransmission(Left_Right_Stepper_Address);
          //Wire.write("Left");
          Wire.write("4");
          Wire.endTransmission();
          //delay(10);
        }
        if (y_pos >= 525) {
          Wire.beginTransmission(Left_Right_Stepper_Address);
          //Wire.write("Left ");
          Wire.write("5");
          Wire.endTransmission();
          //delay(10);
        }

        Wire.beginTransmission(Left_Right_Stepper_Address);
        //Wire.write("Left ");
        Wire.write("0");
        Wire.endTransmission();

        break;
      case 3:    // Right Radar
        if (y_pos <= 500) {
          int y_pos4 = 6;
          Wire.beginTransmission(Left_Right_Stepper_Address);
          //Wire.write("Right ");
          Wire.write("6");
          Wire.endTransmission();
          //delay(10);
        }
        if (y_pos >= 525) {
          int y_pos5 = 7;
          Wire.beginTransmission(Left_Right_Stepper_Address);
          //Wire.write("Right ");
          Wire.write("7");
          Wire.endTransmission();
          //delay(10);
        }

        Wire.beginTransmission(Left_Right_Stepper_Address);
        //Wire.write("Left ");
        Wire.write("0");
        Wire.endTransmission();
        //delay(10);

        break;
    }

  }
}

void loop() {

}

SLAVE

// Include Stepper Library
#include <Stepper.h>

// Include Wire Library
#include <Wire.h>

// Define System Start Button
#define SystemStartButton 4

#define STEPS 8   // adjust motor step count i.e.(8 motor steps = 1 step)
// directly effects motor speed, if STEPS increased,
// than motor speed below should be decreased.

// Define Slave I2C Address
#define Center_Stepper_Address 8               // address set for center stepper motor
#define Left_Right_Stepper_Address 9              // address set for left stepper motor
//#define Right_Stepper_Address 10              // address set for right stepper motor

const int cw = (-STEPS);            // move stepper clockwise
const int ccw = (STEPS);            // move stepper counter-clockwise
int stepCount_L = 0;                // counts steps moved
int stepCount_R = 0;                // counts steps moved
int SystemStartButtonCounter = 0;
int SystemStartButtonState = 0;     // current state of the System Start Button
int SystemStartButtonLastState = 0; // previous state of the System Start Button


Stepper stepper_Left(STEPS, 9, 11, 10, 12);    // create an instance of the stepper class using the steps and pins

Stepper stepper_Right(STEPS, 5, 7, 6, 8);      // create an instance of the stepper class using the steps and pins

void setup() {
  // put your setup code here, to run once:
  //  pinMode (SystemStartButton, INPUT) ;
  stepper_Left.setSpeed(400);       // Set motor speed pulse duration
  stepper_Right.setSpeed(400);
  Serial.begin(2000000);         // set the baud rate for serial communication
  // Set motor speed pulse duration
  Wire.begin(Left_Right_Stepper_Address);
  Wire.onReceive (receiveEvent0);
  //Serial.println(data);        // print the integer
}


void loop() {
  delay(100);
}
byte x = 0;
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent0(int howMany) {
  while (1 < Wire.available()) { // loop through all but the last

    int x = Wire.read();    // receive byte as an integer
    Serial.println(x);         // print the integer
  }
}

If you want to send the number 4 to the other Arduino then this line

          Wire.write("4");

should be

          Wire.write(4);

And if you really want to send the character '4' (which has the ASCII value 52) you should use single quotation marks.

Why not have all the stepper motors on the same Arduino?

...R

so thank you for the input, as far as the steppers all on one arduino, I don't think i have enough I/O pins to pull it off, only 12 digital pin's 2-13 and pin 13 is the built in led so i just don't ever use it. But i like knowing a better way for sure. going to clear up the code and see if i can get it working properly. My end goal is a motion detection setup with the sonars also object detection/2Dmapping that can be mounted, i'm going to use it on my balancing bot, so i still have motor drivers and other stuff to connect to the arduino Nano's. also need to learn about ASCII. lack of knowledge prevents progress. thanks again

D14, 15, 16, 17 are also available with D18, D19 in use for I2C.
(aka A0 to A3, and A4/A5)

And if you go 1/2 way down the page here Stepper Motors – code, circuits, & construction
you can see a way to use just 2 control wires per stepper.

awesome, Thank you very much. some learning to do now...

CrossRoads:
And if you go 1/2 way down the page here http://www.tigoe.com/pcomp/code/circuits/motors/stepper-motors
you can see a way to use just 2 control wires per stepper.

Thanks for the info, Great read and a massive help for me i have a few more ULN2003 driver boards that i can use to build my own custom driver board. reduce the I/O usage from 12 pins to 6 pins leaving plenty of room for the 3 HC-SR04 sonars in one wire setup. Add a DHT11 and A push button joystick. for a total of 11 digital pins and 1 analog.

so the info was great from , http://www.tigoe.com/pcomp/code/circuits/motors/stepper-motors , the circuit works only my stepper motors get stupid hot after a few minutes so i have decided to go about it with the four wire boards that the steppers came with. anyhoo just an update on my project

tinkerinman:
so the info was great from , http://www.tigoe.com/pcomp/code/circuits/motors/stepper-motors , the circuit works only my stepper motors get stupid hot after a few minutes

Interesting.

I must try it myself in the near future.

...R

here are a couple of hand drawn works of art ... :o :o the picture of the single ULN2003 shows how each pin on the chip is connected, the second one is a basic idea of how i have all 3(three) chips connected in my circuit, and the 3rd picture is of my physical circuit... it all works, only as i said before my stepper motors get stupid hot(burnt my fingers hot) and only the steppers, the ULN2003 chips aren't getting hot not even warm, same with the 5V regulator, cold to the touch. everything works, 3x hc-sr04, a DHT11, a joystick w/pshbtn all run from the 5V regulated supply.

just to be clear, the picture of the single ULN2003 is exactly how all 3 chips are connected except for the diode, there is only 1 diode that feeds all 3 chips.. and all the resistors are 1K ohm

Images from Reply #9 so we don't have to download them. See this Simple Image Posting Guide

Smaller image files (640 x 480 should be fine) are kinder to those of us with slow or expensive internet connections.

...R

Sorry, but those images are not readable.

...R

Hi,
You should be able to save those images rather than do screen captures.
In fact the top line of your screen shows that they are already saved files.

Tom... :slight_smile:

Sorry for the bad posting of pics, but i do hope the general idea of how the chips are connected in the circuit has been made.
going to take some time and breadboard up a Single ULN2003. I'm thinking the steppers are getting hot because of the diode, so going to change it out for a capacitor to ground. i don't even understand why the diode is even in the circuit provided at http://www.tigoe.com/pcomp/code/circuits/motors/stepper-motors , i copied the circuit only i used 3 chips.

has me scratching my head, but learning takes time, so pushing forward see what happens..