Here is a wire diagram for a 3 position switch and code. Did this for Tallon
/*
This code is to used to move an actuator to 3 different positions.
This code is provided as is with no warranty.
*/
#include <SoftwareSerial.h>
#define rxPin 3 // pin 3 connects to SMC TX
#define txPin 4 // pin 4 connects to SMC RX
#define resetPin 5 // pin 5 connects to SMC nRST
#define errPin 6 // pin 6 connects to SMC ERR
SoftwareSerial smcSerial = SoftwareSerial(rxPin, txPin);
// some variable IDs
#define ERROR_STATUS 0
// define our stop points
#define pos3 1900
#define pos2 1600
#define pos1 1400
int currentPos = 1;
void setup() {
Serial.begin(9600); // for debugging (optional)
smcSerial.begin(9600);
//On first power move the selected value.
changePosition(currentPos);
// clear the safe-start violation and let the motor run
exitSafeStart();
//Setup our digital pins for input mode.
pinMode(22, INPUT);
pinMode(26, INPUT);
pinMode(30, INPUT);
}
void loop() {
// Debuging code for looping through the digital ports.
//for (int i = 0; i < 50; i++) {
//place actuator in position 3
if (digitalRead(22) == 1) {
currentPos = 3;
changePosition(currentPos);
delay(1000);
// break;
}
//place actuator in position 2
if (digitalRead(26) == 1) {
currentPos = 2;
changePosition(currentPos);
delay(1000);
//break;
}
//place actuator in position 1
if (digitalRead(30) == 1) {
currentPos = 1;
changePosition(currentPos);
delay(1000);
//break;
}
/*
//Debugging code to read digital ports. Used with the for loop above.
Serial.print("Digital port: ");
Serial.print(i);
Serial.print(":");
Serial.println(digitalRead(i));
delay(10);
}
*/
/*
Serial.print("Current Position: ");
Serial.println(currentPos);
Serial.println("--------------------");
*/
//Pause the loop for a moment.
delay(100);
//check for any errors on the error pin.
if (digitalRead(errPin) == HIGH) {
Serial.print("Error Status: 0x");
Serial.println(getVariable(ERROR_STATUS), HEX);
// once all other errors have been fixed,
// this lets the motors run again
exitSafeStart();
}
}
//moveMotor based on number.
void changePosition(int positionNum) {
switch (positionNum) {
case 3:
moveMotor(pos3);
break;
case 2:
moveMotor(pos2);
break;
case 1:
moveMotor(pos1);
break;
}
}
// returns the specified variable as an unsigned integer.
// if the requested variable is signed, the value returned by this function
// should be typecast as an int.
unsigned int getVariable(unsigned char variableID){
smcSerial.write(0xA1);
smcSerial.write(variableID);
return readByte() + 256 * readByte();
}
// required to allow motors to move
// must be called when controller restarts and after any error
void exitSafeStart(){
smcSerial.write(0x83);
}
// read a serial byte (returns -1 if nothing received after the timeout expires)
int readByte(){
char c;
if(smcSerial.readBytes(&c, 1) == 0){ return -1; }
return (byte)c;
}
//Move the actuator to a target value
void moveMotor(int motorTarget) {
word target = motorTarget;
smcSerial.write(0xAA);
smcSerial.write(0x0B);
smcSerial.write(0xC0 + (target & 0x1F));
smcSerial.write((target >> 5) & 0x7F);
}