am finally starting to experiment with stepper motors and have got the basics of moving them, but i must have missed some fundamental working because i don't know how to stop the motor (or more accurately, how to restart it from a 'stopped' position ?)
i started off with the thread by celem;
https://forum.arduino.cc/index.php?topic=85335
and then repurposed sample code from the tronix webpage;
http://www.4tronix.co.uk/arduino/Stepper-Motors.php
i now have a two-wheel stepperbot and am trying to control it with a Wii-Nunchuck.
my stumbling block is making it stop !
with the code below, when the joystick is in neutral position, the driver still functions and the wheels turn (slowly).
/*
* Nunchuck functions -- Talk to a Wii Nunchuck
* 2007-11 Tod E. Kurt, http://todbot.com/blog/
*
* The reading code originally from Windmeadow Labs
* http://www.windmeadow.com/node/42
*/
#include <Wire.h> //Include the Wire.h library so we can communicate with the Nunchuck
static uint8_t nunchuck_buf[6]; // array to store nunchuck data,
// initialize the I2C system, join the I2C bus,
// and tell the nunchuck we're talking to it
static void nunchuck_init() {
Wire.begin(); // join i2c bus as master
Wire.beginTransmission(0x52);// transmit to device 0x52
Wire.write((uint8_t)0x40);// sends memory address
Wire.write((uint8_t)0x00);// sends sent a zero.
Wire.endTransmission();// stop transmitting
}
// Send a request for data to the nunchuck
static void nunchuck_send_request() {
Wire.beginTransmission(0x52);// transmit to device 0x52
Wire.write((uint8_t)0x00);// sends one byte
Wire.endTransmission();// stop transmitting
}
// Encode data to format that most wiimote drivers except?ACCEPT
// only needed if you use one of the regular wiimote drivers
static char nunchuk_decode_byte (char x) {
x = (x ^ 0x17) + 0x17;
return x;
}
// Receive data back from the nunchuck,
// returns 1 on successful read. returns 0 on failure
static int nunchuck_get_data() {
int cnt = 0;
Wire.requestFrom (0x52, 6);// request data from nunchuck
while (Wire.available ()) {
// receive byte as an integer
nunchuck_buf[cnt] = nunchuk_decode_byte( Wire.read() );
cnt++;
}
nunchuck_send_request(); // send request for next data payload
// If we recieved the 6 bytes, then go print them
if (cnt >= 5) {
return 1; // success
}
return 0; //failure
}
// returns value of x-axis joystick
static int nunchuck_joyx() {
return nunchuck_buf[0];
}
// returns value of y-axis joystick
static int nunchuck_joyy() {
return nunchuck_buf[1];
}
/// ABV=nunchuck_funcs.h
byte joyx, joyy, zbut, cbut;
////////////////////////////////////////////////
//declare variables for the motor pins
const byte motRGpin1 = 2; // Blue - 28BYJ48 pin 1
const byte motRGpin2 = 3; // Pink - 28BYJ48 pin 2
const byte motRGpin3 = 4; // Yellow - 28BYJ48 pin 3
const byte motRGpin4 = 5; // Orange - 28BYJ48 pin 4
// Red - 28BYJ48 pin 5 (VCC)
const byte motLFpin1 = 9; // Blue - 28BYJ48 pin 1
const byte motLFpin2 = 8; // Pink - 28BYJ48 pin 2
const byte motLFpin3 = 7; // Yellow - 28BYJ48 pin 3
const byte motLFpin4 = 6; // Orange - 28BYJ48 pin 4
int motorSpeed = 1200; //variable to set stepper speed
byte lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};
byte count = 0;
///////////////////////////////////////////////////////////////////////////
void setup() {
//declare the motor pins as outputs
pinMode(motRGpin1, OUTPUT);
pinMode(motRGpin2, OUTPUT);
pinMode(motRGpin3, OUTPUT);
pinMode(motRGpin4, OUTPUT);
pinMode(motLFpin1, OUTPUT);
pinMode(motLFpin2, OUTPUT);
pinMode(motLFpin3, OUTPUT);
pinMode(motLFpin4, OUTPUT);
Serial.begin(9600);
nunchuck_init();
}
void loop() {
nunchuck_get_data();
joyx = nunchuck_joyx(); // ranges from approx 36 - 233 : MDL 137-8
joyy = nunchuck_joyy(); // ranges from approx 33 - 215 : MDL 131
if (((joyy < 200)&&(joyy > 50)) && ((joyx < 200)&&(joyx > 50))) {
// Serial.println("joystick neutral");
// BOTHoff();
/*
DBLoutputDIFF(count);
delay(50); // pseudoSTOP...
count++;
if (count > 7) count = 0;
*/
} else {
if (joyy >= 200) {
// Serial.println("joyy UP");
DBLoutputSAME(count);
delayMicroseconds(1200);
count++;
if (count > 7) count = 0;
}
if (joyy <= 50) {
// Serial.println("joyy DOWN");
DBLoutputSAME(7-count);
delayMicroseconds(1200);
count++;
if (count > 7) count = 0;
}
if (joyx >= 200) {
// Serial.println("joyx UP");
DBLoutputDIFF(count);
delayMicroseconds(1200);
count++;
if (count > 7) count = 0;
}
if (joyx <= 50) {
// Serial.println("joyx DOWN");
DBLoutputDIFF(7-count);
delayMicroseconds(1200);
count++;
if (count > 7) count = 0;
}
}
}
//////////////////////////////////////////////////////////////////////////////
void DBLoutputSAME(byte out) {
digitalWrite(motRGpin1, bitRead(lookup[out], 0));
digitalWrite(motLFpin1, bitRead(lookup[out], 3));
digitalWrite(motRGpin2, bitRead(lookup[out], 1));
digitalWrite(motLFpin2, bitRead(lookup[out], 2));
digitalWrite(motRGpin3, bitRead(lookup[out], 2));
digitalWrite(motLFpin3, bitRead(lookup[out], 1));
digitalWrite(motRGpin4, bitRead(lookup[out], 3));
digitalWrite(motLFpin4, bitRead(lookup[out], 0));
}
void DBLoutputDIFF(byte out) {
digitalWrite(motRGpin1, bitRead(lookup[out], 0));
digitalWrite(motLFpin1, bitRead(lookup[out], 0));
digitalWrite(motRGpin2, bitRead(lookup[out], 1));
digitalWrite(motLFpin2, bitRead(lookup[out], 1));
digitalWrite(motRGpin3, bitRead(lookup[out], 2));
digitalWrite(motLFpin3, bitRead(lookup[out], 2));
digitalWrite(motRGpin4, bitRead(lookup[out], 3));
digitalWrite(motLFpin4, bitRead(lookup[out], 3));
}
void BOTHoff() {
digitalWrite(motRGpin1, LOW);
digitalWrite(motLFpin1, LOW);
digitalWrite(motRGpin2, LOW);
digitalWrite(motLFpin2, LOW);
digitalWrite(motRGpin3, LOW);
digitalWrite(motLFpin3, LOW);
digitalWrite(motRGpin4, LOW);
digitalWrite(motLFpin4, LOW);
}
but if i activate (uncomment) the BOTHoff() function, nothing happens - presumably it is in the 'off' state, but then the 'move' functions don't work.
using just the Serial.println lines for debugging confirms the logic works as far as the joystick goes.
Thanks for anyone pointing what might be painfully obvious in hindsight...
EDIT:
FWIW-
this is the motor and driver being used;