Yeh so I have a sign that is mounted in a wormgear that turns a sign. I have one limit switch for homing, and the second is just a out-of-bounds switch, incase something goes awry and it turns farther than intended. In any case my program control where the sign goes, and uses the homing switch to know it location when starting up. So basically the program selects a location. Based on this selection, the program first checks that 1) all limit switches are connected, then 2) that no limit switch is pressed, then 3) sends a command for the motor to move clockwise or counter clockwise. It continues to repeat this until the correct angle is reached.
So basically a motor call would not need to be interrupted as the limit switch is not checked again until the motor move that has been called is finished moving and the RPi determined if another movement is required. (The motor move only turns the output shaft on the wormgear a few degrees so it is done in very small increments.)
Here is the ardunio code I made to work via I2C. The RPi sends a command to check various limit switches. Based on the response, it sends a command to change a variable on the arduino, and sends a find command which is used in conjunction with the changed variable from the previous state. (I did it like this to minimize the data transferred, as I was concerned with the reliability of the I2C bus...unfortunately even that was too much.)
Anyways here is the Arduino code for the transfer of one setup (one thread). I haven't written the code for the 10 setups yet, but it will probably be just an extension of what I have already written, and changed to serial communication:
#include <Wire.h>
//Limit switch initialization
int h_no = 7;
int h_nc = 8;
int a_no = 10;
int a_nc = 11;
int arrival = 0;
int clicker = 0;
char h_no_h[] = "1";
char h_no_h_f[] = "2";
char a_no_h[] = "3";
char a_no_h_f[] = "4";
char a_no_l[] = "5";
char a_no_l_f[] = "6";
char h_no_l[] = "7";
char h_no_l_f[] = "8";
char all_ls[] = "9";
char all_ls_f[] = "a10";
char error[] = "b11";
int driverPUL = 13; // PUL- pin
int driverDIR = 12; // DIR- pin
int pd = 1000; // Pulse Delay period
void setup() {
Serial.begin(9600);
Wire.begin(0x8);
Wire.onReceive(receiveEvent);
Wire.onRequest(sendData);
pinMode(h_no, INPUT_PULLUP);
pinMode(h_nc, INPUT_PULLUP);
pinMode(a_no, INPUT_PULLUP);
pinMode(a_nc, INPUT_PULLUP);
pinMode (driverPUL, OUTPUT);
pinMode (driverDIR, OUTPUT);
}
void loop() {}
void motor_move_clockwise(){
for (int i = 0; i <= 1000; i++) {
digitalWrite(driverDIR,LOW);
digitalWrite(driverPUL,HIGH);
delayMicroseconds(pd);
digitalWrite(driverPUL,LOW);
delayMicroseconds(pd);
}
}
void motor_move_counterclockwise(){
for (int i = 0; i <= 1000; i++) {
digitalWrite(driverDIR,HIGH);
digitalWrite(driverPUL,HIGH);
delayMicroseconds(pd);
digitalWrite(driverPUL,LOW);
delayMicroseconds(pd);
}
}
bool check_switches(){
if((digitalRead(h_no) == LOW && digitalRead(h_nc) == LOW || digitalRead(h_no) == HIGH && digitalRead(h_nc) == HIGH) || (digitalRead(a_no) == LOW && digitalRead(a_nc) == LOW || digitalRead(a_no) == HIGH && digitalRead(a_nc) == HIGH))
return false;
else
return true;
}
void receiveEvent(int howMany) {
while (Wire.available()) {
arrival = Wire.read();
Serial.println("recieveEvent Function Called");
if(arrival == 1){
motor_move_clockwise();
Serial.println("Motor clockwise called");
}else if(arrival == 2){
motor_move_counterclockwise();
Serial.println("Motor counterclockwise called");
}else if(arrival == 3){
clicker = 1;
Serial.println("Clicker = 1, checking h_no_h");
}else if(arrival == 4){
clicker = 2;
Serial.println("Clicker = 2, checking a_no_h");
}else if(arrival == 5){
clicker = 3;
Serial.println("Clicker = 3, checking a_no_l");
}else if(arrival == 6){
clicker = 4;
Serial.println("Clicker = 4, checking h_no_l");
}else if(arrival == 7){
clicker = 5;
Serial.println("Clicker = 5, checking all_ls");
}else{
clicker = 6;
Serial.println("Clicker = 6, Error");}
}
}
void sendData() {
Serial.println("sendData Function Called");
if (clicker == 1){
if(digitalRead(h_no) == HIGH){
Wire.write(h_no_h[0]); // writing 1
Serial.println(" wiring - home NO high");
}else{
Wire.write(h_no_h_f[0]); // writing 2
Serial.println(" wiring - home NO not high");
}
}else if(clicker == 2){
if(digitalRead(a_no) == HIGH){
Wire.write(a_no_h[0]); // writing 3
Serial.println(" wiring - away NO high");
}else{
Wire.write(a_no_h_f[0]); // writing 4
Serial.println(" wiring - away NO not high");
}
}else if(clicker == 3){
if(digitalRead(a_no) == LOW){
Wire.write(a_no_l[0]); //writing 5
Serial.println(" wiring - away NO low");
}else{
Wire.write(a_no_l_f[0]); // writing 6
Serial.println(" wiring - away NO not low");
}
}else if(clicker == 4){
if(digitalRead(h_no) == LOW){
Wire.write(h_no_l[0]); // writing 7
Serial.println(" wiring - home NO low");
Serial.println(h_no_l[0]);
}else{
Wire.write(h_no_l_f[0]); // writing 8
Serial.println(" wiring - home NO not low");
}
}else if(clicker == 5){
if(check_switches()){
Wire.write(all_ls[0]); // writing 9 - swithes good
Serial.println(" wiring - limit switches connected");
}else{
Wire.write(all_ls_f[0]); // writing (a) 10 - switches bad
Serial.println(" wiring - limit switches Connection FAILURE");
}
}else if(clicker == 6){
Wire.write(error[0]); // writing (b) 11
Serial.println(" wiring - ERROR");
}
}
Just curious, would I just be able to use your library for just the moving of the stepper motors? Or does it require that all code is implemented under your library for the non-blocking property to work effectively? (Ideally, I would prefer to make small changes if possible, but whatever is required is required.)
That should work for me!