Hello everyone so I have a project I am working on the aim is to get the actuator to move to different positions at different temperatures below is my code and I haven’t gotten it to work correctly. I would like it to open to like 25 percent 50 , 75 and 100%. Forgive me the code formatting thing didn’t work.
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 8
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
const int Extend_pin = 10; //Connect to L298N Pin In1 or BTS7960 RPWM NOTE: You may have to switch pins 9 and 10 depending on your RC controller
const int Retract_pin = 9; //Connect to L298N Pin In2 or BTS7960 LPWM
int Position_pin = A0; //This pin will measure the position of the linear actuator
int actual_locationpot=0; //Used to keep track of the "actual" actuator position
int deadband = 10; //Used to prevent the actuator from constantly seeking (jittering) decrease number for greater accuracy, increase to reduce the jitter
float Celsius = 0;
float Fahrenheit = 0;
int outputValue = 0;
void setup() {
sensors.begin();
Serial.begin(9600);
pinMode(Extend_pin, OUTPUT); //Digital pin for extending the actuator
pinMode(Retract_pin, OUTPUT); //Digital pin for retracting the actuator
digitalWrite(Extend_pin, LOW); //Start with the actuator turned off until the loop below
digitalWrite(Retract_pin, LOW);
}
void loop() {
sensors.requestTemperatures();
Celsius = sensors.getTempCByIndex(0);
Fahrenheit = sensors.toFahrenheit(Celsius);
actual_locationpot = analogRead(Position_pin);
outputValue = map(actual_locationpot, 0, 1023, 0, 255);
Serial.println(actual_locationpot);
delay(1000);
Serial.print(Celsius);
Serial.print(" C ");
delay(100);
Serial.print(Fahrenheit);
Serial.println(" F");
delay(100);
if (Fahrenheit <=56.86) {
digitalWrite(Retract_pin, HIGH);
delay(2000);
}
else {
digitalWrite(Retract_pin, LOW);
}
}
I expected setup() would show code setting the linear actuator to a zero point so later on you could set the actuator to the percent you desire. But I see no code trying to discover where zero is located. Please describe the scheme to find the percent you are liking to use!
No! Read the pot value when the actuator is at zero, then you actually need to read the pot value when the actuator is at the extreme other end. Your % values will all be based on the percent of the difference in values. Simple math. The resistance from top to bottom is 100%, so you need to know what resistance value is at 25%, 50%, etc.
So actually when I tested it the 0 or home position when I ohmed across the pot leads I got 0.472 k ohm and 5.45 k ohm lower and upper limit respectively. I was just confused as to how I can incorporate this in my code since adruino gives a value from 0-255. I kinda get what you’re saying abit so if 0.472 k ohms = 0 and 5.45 k ohms = 100%
Therefore 25%= 1.362
50%=2.725
75%=4.0875
100%=5.45
So my question actually is how can I co relate these values to something adruino can interpret since adruino cannot read resistances
The pot should be wired as a voltage divider, between the Arduino 5V (on an Uno R3 or similar) and GND. Simply use analogRead() to read the pot output voltage to determine the zero position, the maximum extension and anything in between.
And internet search with the phrase "arduino read potentiometer voltage" will find tutorials.
So weirdly enough last night i extended the actuator to maximum and it. Reads 1023 on adruino then I retracted it to zero point it still reads 1023 and measured voltage from ground to analog output the middle from pot and I got the full blown 5 volts no matter the position when I switched the two end wires on pot to adruino it then read zero so said fine turns out after extending to maximum again it still reads zero I’m leaning towards something isn’t right with the feed back position pot within the actuator. Either that or my wiring just isn’t right. I doubt the code is the problem because I hooked a twist knob pot to it and it reads as I vary the resistance.
Here is an image of the circuit just learning how to navigate. As I’ve said I used the meter to check the resistance on the wiper from the actuator fully extended or retracted it reads the full voltage and I’ve swapped it with a known working potenti meter and it reads both on my meter and adruino.
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 8
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
const int Extend_pin = 10; //Connect to L298N Pin In1 or BTS7960 RPWM NOTE: You may have to switch pins 9 and 10 depending on your RC controller
const int Retract_pin = 9; //Connect to L298N Pin In2 or BTS7960 LPWM
int Position_pin = A1; //This pin will measure the position of the linear actuator
int actual_locationpot=0; //Used to keep track of the "actual" actuator position
int desired_position = 304; //Used to tell Actuator where to go
float Celsius = 0;
float Fahrenheit = 0;
int outputValue = 0;
void setup() {
sensors.begin();
Serial.begin(9600);
pinMode(Extend_pin, OUTPUT); //Digital pin for extending the actuator
pinMode(Retract_pin, OUTPUT); //Digital pin for retracting the actuator
digitalWrite(Extend_pin, LOW); //Start with the actuator turned off until the loop below
digitalWrite(Retract_pin, LOW);
}
void loop() {
sensors.requestTemperatures();
Celsius = sensors.getTempCByIndex(0);
Fahrenheit = sensors.toFahrenheit(Celsius);
actual_locationpot = analogRead(Position_pin);
Serial.println(actual_locationpot);
delay(1000);
Serial.print(Celsius);
Serial.print(" C ");
delay(100);
Serial.print(Fahrenheit);
Serial.println(" F");
delay(100);
if (Fahrenheit <=60.46) {
digitalWrite(Retract_pin, HIGH);
delay(1000);
}
else if (actual_locationpot== desired_position) {
digitalWrite(Retract_pin, LOW);
delay(100);
}
else {
// Stop motor when close to target position
digitalWrite(Retract_pin, LOW);
}
}
Hello again above is my updated code I got the pot to be reading on the adruino. The issue now is the actuator extends past my desired position without stopping any advise as to how I can tweak my code to achieve this stopping at the preset position in the code
Shall troubleshoot then not sure yet. Just needed you guys to verify that my code was ok and it wasn’t the problem. Will keep trying till I get it. Thank you for your input