This is what I have now:
There are two pushButton with an red LED each connected to the first arduino. When I press them the arduino serial.prints a message to the serial pins 1 and 0. The message is received from the second arduino that decides how to move the stepper motor, if I press forward the motor goes forward for 5 steps, If I press reverse the motor reverses 5 steps.
Video here Serial connection between two Arduino UNO - YouTube
Code for the Arduino n° 1 with L293D Motor shield and Stepper:
/*
Invio di comandi tramite serial monitor
trovato su web + modifiche
Giacomo Marelli
06.08.2012
Controlla uno stepper.
Scrivere nel serial Monitor: "<stepper,+>" o "<stepper,->".
Ogni comando sposta di 5 passi (9°) in avanti (+) o indietro(-).
*/
//inserisce le due libreri necessarie,
//la prima per comunicare, la seconda per lo stepper
#include <avr/pgmspace.h>
#include <AFMotor.h>
//variabili per comunicazione seriale
#define SOP '<'
#define EOP '>'
bool started = false;
bool ended = false;
char inData[80];
byte index;
// Connect a stepper motor with 53 steps per revolution
// to motor port #2 (M3 and M4)
AF_Stepper motor(53, 2);
void setup() {
Serial.begin(9600);
motor.setSpeed(300); // 50 rpm
}
void loop() {
// Read all serial data available, as fast as possible
while (Serial.available() > 0) {
char inChar = Serial.read();
if (inChar == SOP) {
index = 0;
inData[index] = '\0';
started = true;
ended = false;
} else if (inChar == EOP) {
ended = true;
break;
} else {
if (index < 79) {
inData[index] = inChar;
index++;
inData[index] = '\0';
}
}
}
// We are here either because all pending serial
// data has been read OR because an end of
// packet marker arrived. Which is it?
if (started && ended) {
// The end of packet marker arrived. Process the packet
char *cmd = strtok(inData, ",");
if (cmd) {
char *val = strtok(NULL, ",");
if (val) {
sendCommand(cmd, val);
}
}
// Reset for the next packet
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
}
// controllo dello stepper
// usare linguaggio specifico del motorshield di ADaFRUIT
void sendCommand(char *command, char *value) {
if (strcmp(command,"stepper") == 0) {
stepper(value);
}
}
void stepper(char *value) {
if (strcmp(value, "+") == 0) {
motor.step(5, FORWARD, MICROSTEP);
delay(100);
}
else if (strcmp(value, "-") == 0) {
motor.step(5, BACKWARD, MICROSTEP);
delay(100);
}
}
And this is the code for Arduino n° 2, with 2 pushButtons.
/*
Giacomo Marelli
06.08.2012
sketch per arduino di comando
invia tramite seriale il comando "<stepper,+>" o "<stepper,->"
il secondo arduino riceve i comandi e li esegue
muovendo il motore stepper
*/
int ledForw = 6;
int ledRev = 19;
int buttonForw = 4;
int buttonRev = 5;
int time = 200;
void setup()
{
Serial.begin(9600);
pinMode( ledForw, OUTPUT);
pinMode( ledRev, OUTPUT);
pinMode( buttonForw, INPUT);
pinMode( buttonRev, INPUT);
Serial.print("<stepper,+>"); // tests the stepper
delay(200);
Serial.print("<stepper,->");
delay(1000);
}
void loop()
{
if (digitalRead(buttonForw) == HIGH)
{
digitalWrite(ledForw, HIGH);
Serial.print("<stepper,+>");
delay(time);
}
else (digitalRead(buttonForw) == LOW);
{
digitalWrite(ledForw, LOW);
}
if (digitalRead(buttonRev) == HIGH)
{
digitalWrite(ledRev, HIGH);
Serial.print("<stepper,->");
delay(time);
}
else (digitalRead(buttonRev) == LOW);
{
digitalWrite(ledRev, LOW);
}
}