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



