Here is the motor controller page: Arduino_Motor_Shield__L298N___SKU_DRI0009_-DFRobot (I don't fully understand how to correctly control this.)
Here is the linear actuator page: http://www.robotshop.com/media/files/pdf/l12-datasheet.pdf (the gist of this is that positive voltage makes it go forward, negative voltage makes it go back, and the value I feed to A0 corresponds to actuator position. Not really that important.)
So what I'm trying to do is make the linear actuator move forward when I enter a 0 into the serial monitor and move backward when I enter a 1, and stop at predefined limits, and I'm using an L298N motor shield to control it.
As I understand the motor controller page with PWM settings for motor1, setting pin 7 to HIGH makes it go forward and setting it LOW makes it go backwards, while setting 6 to a value between 0 and 255 determines the speed. I'm not sure on this because the truth table seems to imply that setting both of those pins to HIGH makes it go backwards rather than forwards at high speed.
Here's the code I've assembled. It's probably worth noting that I know having pins 9 and 10 control 12 and 13 is entirely worthless, but I'm intending to do something different later once I sort out the bugs here. I'm worried about just testing it because I'm concerned I'll damage the linear actuator.
//Arduino PWM Speed (Motor) Control:
int E1 = 6; //speed control (PWM)
int M1 = 7; //direction control
int input0 = 13; //forward input
int input1 = 12; //backward input
int output0 = 9; //will be connected to input0 (temp)
int output1 = 10; //will be connected t input1 (temp)
int sensorActivated = -1;
int value = 255; //currently max, later tied to tachometer
void setup() {
pinMode(M1, OUTPUT);
pinMode(input0, INPUT);
pinMode(input1, INPUT);
Serial.begin(9600);
}
void loop() {
int sensorValue0 = digitalRead(input0); //forward instruction
int sensorValue1 = digitalRead(input1); //backward instruction
int inputPosition = analogRead(A0); //position of linear actuator
if (Serial.available() > 0) {
// read the incoming byte (should be 0 or 1);
sensorActivated = Serial.read();
if (sensorActivated = 0){
digitalWrite(output0) = 1;
Serial.println("Going forward");
}
if (sensorActivated = 1){
digitalWrite(output1) = 1;
Serial.println("Going backward");
}
}
if ((sensorValue0 > 0) && (inputPosition < 666)) { //if told to go forward AND the linear actuator is not extended beyond 99mm
digitalWrite(M1,HIGH); //forward
analogWrite(E1, value); //PWM Speed Control
}
if ((sensorValue1 > 0) && (inputPosition > 8)){ //if told to go backward AND the linear actuator is not retracted below 1mm
digitalWrite(M1,LOW); //backward?
analogWrite(E1, HIGH); //PWM speed control; always goes backwards as quickly as possible
}
}
Will this work as I'm expecting? Do I need to change anything? Thanks for your help.