Hi,
for a project I have 2 arduino UNO's. The first on eis reading out an SD card the second one has to execute the commands (comming from the SD card)
This is the code for the "MASTER" (with sd card):
#include <SD.h>
//CONNECTIONS
File myFile; // instance of a file
// WORKING VALUES
char inputString [100];
char inputChar;
long randNumber;
String filename;
int stringIndex = 0; // String stringIndexing int;
int lineIndex = 1;
void setup(){
// Open serial communications and wait for port to open:
Serial.begin(57600);
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
// see if the card is present and can be initialized:
if (!SD.begin(4)){
Serial.println("Initialization failed: card failed, or not present");
// don't do anything more:
while (1) ;
}
Serial.println("Initialization done.");
OpenFile();
}
void OpenFile() {
filename ="TEST1.txt";
// Open up the file we're going to log to!
myFile = SD.open(filename);
if (! myFile){
if (DEBUGGING == 1){
Serial.print("error opening File: ");
Serial.println(filename);
Serial.println("No such file in directory");
}
// Wait forever since we cant write data
while (1) ;
}
}
void loop() {
inputChar = myFile.read(); // Gets one byte from serial buffer
if (inputChar != '\n')
{ // define breaking char here
if (inputChar == 'Q') //end of the file
{
Serial.println("---END OF FILE---");
myFile.close();
}
else
{
inputString[stringIndex] = inputChar; // Store it
stringIndex++; // Increment where to write next
}
}
else
{
Serial.print("line ");
Serial.print(lineIndex);
Serial.print(": "); // shows that the program is cycling, for debugging only
}
//Serial.println(inputString);
stringIndex = 0; // clear the value for the next cycle
lineIndex++;
memset(inputString, 0, sizeof(inputString));
//Wait until the other UNO repsonse with "1" (1=ok, 0=nok)
while(Serial.available() == 0) { } // There really is nothing between the {} braces
char x = Serial.read();
}
}
The code above is working, he send the first line of the file TEST1.txt to the serial monitor
#define DEBUGGING (0) // 0 = no serial output, 1 = serial output for debugging
#define VERBOSE (0) // add to get a lot more serial output.
#define VERSION ("3-rc2") // firmware version
#define BAUD (57600) // How fast is the Arduino talking?
#define MAX_BUF (64) // What is the longest message Arduino can store?
#define STEPS_PER_TURN (200) // depends on your stepper motor. most are 200.
#define MIN_STEP_DELAY (50)
#define MAX_FEEDRATE (500)
#define MIN_FEEDRATE (1)
#define StepsPerUnit (56) // how many steps per in/mm
#define DimensionX (4000) // traverse path for X in mm/in (not steps!) //400
#define DimensionY (6000) // traverse path for Y in mm/in (not steps!) //600
#define SendPosAfterMove (0)
#define SendPosWhileMove (1)
#define hwCNC (0) // accept commands from and send infos to hwCNC (PC-Software)
#define SendCommandBack (0) // Send command back to console so you know the Arduino got the message
#define HardwareEndSwitches (1) // did we have hardware end limit switches?
#define LimitSwitchHomeX (2) // Pins of
#define LimitSwitchHomeY (4) // the
#define LimitSwitchEndX (7) // limit
#define LimitSwitchEndY (8) // switches
boolean has_origin = false;
float act_pos_x = 0.0;
float act_pos_y = 0.0;
#include <Wire.h>
#include <Adafruit_MotorShield.h>
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *mX = AFMS.getStepper(STEPS_PER_TURN, 1); // to motor port #1 (M1 and M2)
Adafruit_StepperMotor *mY = AFMS.getStepper(STEPS_PER_TURN, 2); // to motor port #2 (M3 and M4)
char buffer[MAX_BUF]; // where we store the message until we get a ';'
int sofar; // how much is in the buffer
float px, py; // location X/Y
// speeds
float fr=0; // human version
long step_delay; // machine version
// settings
char mode_abs=1; // absolute mode?
void position(float npx,float npy) {
// here is a good place to add sanity tests
px=npx;
py=npy;
}
void release() {
mX->release();
mY->release();
}
void output(char *code,float val) {
Serial.print(code);
Serial.println(val);
}
void where() {
output("X",px);
output("Y",py);
output("F",fr);
if (DEBUGGING == 1){ Serial.println(mode_abs?"ABS":"REL");}
}
void help() {
Serial.println(F("GcodeAFMotorV2"));
Serial.print("Version ");
Serial.println(VERSION);
Serial.println(F("Commands:"));
Serial.println(F("G00 [X(steps)] [Y(steps)] [F(feedrate)]; - linear move"));
Serial.println(F("G01 [X(steps)] [Y(steps)] [F(feedrate)]; - linear move"));
Serial.println(F("G04 P[seconds]; - delay"));
Serial.println(F("G28; - move to Home-Position/Origin"));
Serial.println(F("G90; - absolute mode"));
Serial.println(F("G91; - relative mode"));
Serial.println(F("G92 [X(steps)] [Y(steps)]; - change logical position"));
Serial.println(F("M18; - release motors"));
Serial.println(F("M100; - this help message"));
Serial.println(F("M114; - report position and feedrate"));
}
void ready() {
sofar=0; // clear input buffer
if (DEBUGGING == 1){ Serial.print(F(">")); }// signal ready to receive input
Serial.print(F("1")); //OK, send next command
}
void setup() {
Serial.begin(BAUD); // open coms
AFMS.begin(); // create with the default frequency 1.6KHz
if (DEBUGGING == 1){help();} // say hello
position(0,0); // set staring position
feedrate(500); // set default speed
if (HardwareEndSwitches) {
// limit switches defined at the beginning
pinMode(LimitSwitchHomeX, INPUT);
pinMode(LimitSwitchHomeY, INPUT);
pinMode(LimitSwitchEndX, INPUT);
pinMode(LimitSwitchEndY, INPUT);
}
ready();
}
void loop() {
// listen for serial commands
while(Serial.available() > 0) { // if something is available
char c=Serial.read(); // get it
if (DEBUGGING == 1){ Serial.print(c);} // repeat it back so I know you got the message
if(sofar<MAX_BUF) buffer[sofar++]=c; // store it
if(buffer[sofar-1]==';') break; // entire message received
}
if(sofar>0 && buffer[sofar-1]==';') {
// we got a message and it ends with a semicolon
buffer[sofar]=0; // end the buffer so string functions work right
if (DEBUGGING == 1){Serial.print(F("\r\n")); }// echo a return character for humans
processCommand(); // do something with the command
ready();
}
}
This code sends a "1" back to ask for the next command, but there is no next command comping.. the serial output shows only 11111111111111111111111...
How can I solve this problem? How can I send back a trigger to ask for the next command? Maybe not through serial, but a digital output (And if so, how do I connect + code?)??