And here is the CODE! Remember use at your own risk this is NOT advised to use on any automobile on or off road and I take NO liability for ANY damages that may occur
/*
This code is to used to move an actuator to 7 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 pos7 3900
#define pos6 3300
#define pos5 2800
#define pos4 2300
#define pos3 1900
#define pos2 1600
#define pos1 1400
int currentPos = 5;
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);
pinMode(34, INPUT);
pinMode(38, INPUT);
pinMode(42, INPUT);
}
void loop() {
// Debuging code for looping through the digital ports.
//for (int i = 0; i < 50; i++) {
//place actuator in position 7
if (digitalRead(22) == 1) {
currentPos = 7;
changePosition(currentPos);
delay(1000);
// break;
}
//place actuator in position 6
if (digitalRead(26) == 1) {
currentPos = 6;
changePosition(currentPos);
delay(1000);
//break;
}
//place actuator in position 5
if (digitalRead(30) == 1) {
currentPos = 5;
changePosition(currentPos);
delay(1000);
//break;
}
//place actuator in position 4
if (digitalRead(34) == 1) {
currentPos = 4;
changePosition(currentPos);
delay(1000);
//break;
}
if (currentPos <= 4) {
//move up
if (digitalRead(38) == 1 && currentPos < 4) {
Serial.println("Move up");
currentPos = currentPos + 1;
changePosition(currentPos);
delay(1000);
//break;
}
//move down
if (digitalRead(42) == 1 && currentPos > 1) {
Serial.println("Move down");
currentPos = 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 7:
moveMotor(pos7);
break;
case 6:
moveMotor(pos6);
break;
case 5:
moveMotor(pos5);
break;
case 4:
moveMotor(pos4);
break;
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);
}