i've been working on this program and now i'm struggling to make it work with two potentiometers.
does anyone have idea how to do it?
program is made to control stepper motor.. i need it to control two stepper motors,
i'm using arduino UNO,
potentiometer is actually ps2 Joystick
but now i only have X-axis without Y-axis,
here's the code:
const int stepPin = 9;
const int dirPin = 8;
int limitS1 = 2;
int limitS2 = 3;
int customDelay,customDelayMapped;
void setup() {
Serial.begin(9600);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(limitS1,INPUT);
pinMode(limitS2,INPUT);
}
void loop() {
{
if (digitalRead(limitS1) == HIGH)
return;
if (digitalRead(limitS2) == HIGH)
return;
}
int val = analogRead(A5);
if((val > 490) && (val < 530))
{
digitalWrite(dirPin,LOW);
return;
}
if((val > -1) && (val < 511))
{
digitalWrite(dirPin,LOW);
}
else
{
digitalWrite(dirPin,HIGH);
}
customDelayMapped = speedUp();
if (customDelayMapped < 0)
{ // this part changes negative values from "int speedUp()"
(customDelayMapped = -customDelayMapped);
}
Serial.println(customDelayMapped);
digitalWrite(stepPin, HIGH);
delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, LOW);
delayMicroseconds(customDelayMapped);
}
int speedUp() {
int customDelay = analogRead(A5);
int newCustom
= map(customDelay, 0, 511, 4000, 0); // here i get center pozition of joystick ( if it goes down is positive value if it goes up is negative value)
return newCustom;
}
limitS1 and S2 are limit stwiches, no need to change..
second potenciometer outputs i want to control with second potenciometer on arduino:
each iteration thru loop, use analogRead() to read the value of each pot and process both values.
separate reading input from processing it. I don't understand why you "return" if the value from A5 is near the center, aren't there other things to do and it doesn't allow the other pot to be read
the reason for this part is that i don't want stepper motors to move when potenciometer(Joystick) is near center..
int val = analogRead(A5);
if((val > 490) && (val < 530))
{
digitalWrite(dirPin,LOW);
return;
}
the same it would be for second potentiometer(joystick):
int val = analogRead(A4);
if((val > 490) && (val < 530))
{
digitalWrite(dirPin,LOW);
return;
}
Joystick is made of two potentiometers now with the program in first post i have it perfectly working just with X-axis now i would need it to work with Y-axis(second potentiometer)
l_nem:
the reason for this part is that i don't want stepper motors to move when potenciometer(Joystick) is near center..
Rather than use return to prevent things happening I suggest you move into a separate function the code that actually moves the motor and only call that function when you want the motor to move.
I believe it will make the logic of the program easier to follow.
it makes more sense to step the motor based on the value of "val". However, i don't understand what is different between the two cases.
but looking at the section above that set the direction pin suggests that you want to go in one direction when the joystick in on one side and the reverse direction on the other. And the problem you have is you now step for two cases when "val" is above or below a value instead of testing for both ranges in one condition
i decided to just use two separate arduinos with same code...
I hope that you are joking !
Use a single Arduino. Read the two pots at the start of loop(). Write a block of code to move stepper 1 (or not) based on the reading from pot 1. Follow that with a block of code to move stepper 2 (or not) based on the reading from pot 2. Do NOT return if either of the pots is in the "do not move" zone, simply do not move the associated stepper
The two blocks of code (or a single block used twice) can conveniently be put in a function but if you are not sure how to do that simply put them in line in loop()
look i tried my best to make it work on one platform but i don't have knowledge how to do it
this is project i need to make for school and i need it tomorrow in class so that was best solution for me
l_nem:
look i tried my best to make it work on one platform but i don't have knowledge how to do it
this is project i need to make for school and i need it tomorrow in class so that was best solution for me
I can understand that. Trying to meet a deadline and learn new techniques can be too stressful.
But now that you have a solution for your school project may I suggest that you take time over the next week to figure out how to do it with a single Arduino. That way you will significantly improve your programming expertise.