#include <Servo.h>
Servo servoMotor1;
int posServo1 = 0;
int channel2;
void setup() {
servoMotor1.attach(9, 900, 2000);
pinMode(7, INPUT);
Serial.begin(115200);
}
void loop() {
channel2 = pulseIn(7, HIGH);
Serial.print("Channel 2:");
Serial.println(channel2);
if (channel2>=1550) {
for(int i = 1500; posServo1 < 1750; posServo1 ++){
servoMotor1.writeMicroseconds(posServo1);
i = posServo1;
if(i>=1501);
}}
else { for(int i = 1500; posServo1 > 1000; posServo1 --){
servoMotor1.writeMicroseconds(posServo1);
i = posServo1;
if(i>=1499);
}}
}
Welcome to the forum
Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'
Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination
When you posted your code without code tags did you receive a warning message ?
@nizam398f It can be deceptive but Installation and Troubleshooting is for Problems with the Arduino IDE itself NOT your project. It says so in the description of the section. Therefore I have moved your post here. Please be more careful where you post in future.
You may want to read this before you proceed:-
how to get the best out of this forum
What sort of limiter do you want to add ?
Please describe what the code does now and what you want it to do
my codes now run a motor in forward direction when the throttle is up and when throttle is letgo (middle) the motor run in a reverse direction. but i want the motor to have a limit to forward and backward at certain angle or revolution.
thank you so much my bad on this would definitely do better in the future.
Im sorry i thought that the suggestion would make the code be different. Would do better next time thank you for the suggestion!
You will need some way to determine that the motor has reached its end position and you cannot do this in software without some kind of position feedback. The normal way to do this is to use switches triggered by the movement reaching its end positions. The code can monitor the switches and stop the motor from moving any further. Google or search the forum for "limit switch" for more details
Is there any reason why you can't use a servo rather than a brushless motor ?
okay another question can i use a time for the limit and adjust it to perfection other than revolution or angle?... Im using a high torque motor for a cutter machine.
You could run the motor for a time but the final position is not likely to be accurate over a period and there is the risk of stalling the motor at one end or the other if it overruns.
You will also have no way of getting the motor movement back in synch if it needs it as you have no absolute position measurement available
hi there, after considering your suggestion I had bought myself a magnetic sensor.. but cant manage to do both conditions for one output could you help me with that ?
#include <Servo.h>
Servo servoMotor1;
#define Reed 10
int posServo1 = 0;
int channel2;
void setup() {
Serial.begin(115200);
pinMode(Reed, INPUT);
servoMotor1.attach(9, 900, 2000);
pinMode(7, INPUT);
Serial.begin(115200);
}
void loop() {
Serial.println(digitalRead(Reed));
channel2 = pulseIn(7, HIGH);
Serial.print("Channel 2:");
Serial.println(channel2);
delay(500);
if ((channel2>=1550)&& (Reed, 1)) {
for(int i = 1500; posServo1 < 1750; posServo1 ++){
servoMotor1.writeMicroseconds(posServo1);
i = posServo1;
if(i>=1501);
}}
else if ((channel2<=1550)&& (Reed, 1))
{ for(int i = 1500; posServo1 > 1000; posServo1 --){
servoMotor1.writeMicroseconds(posServo1);
i = posServo1;
if(i>=1499);
}}
}
How is your reed switch wired ?
Serial.println(digitalRead(Reed));
You seem to be reading the state of the reed switch input, printing it but not saving it to a variable
if ((channel2 >= 1550) && (Reed, 1))
(Reed,1) is not the state of the Reed pin. To get that you need to use digitalRead() again or use the value of a variable containing the result of digitalRead(Reed)
There maybe other things wrong with the code but fix this fundamental mistake first
after changing it to this
if ((channel2>=1550)&& (digitalRead(Reed)==1))
i manage to work the code but cant stop the code when the reed is 0 is that because im using a servo library ?
Dis you change all of the lines where you had made the same mistake and where do you check that the reed switch is not activated ?
#include <Servo.h>
Servo servoMotor1;
#define Reed 10
int posServo1 = 0;
int channel2;
void setup() {
Serial.begin(115200);
pinMode(Reed, INPUT);
servoMotor1.attach(9, 900, 2000);
pinMode(7, INPUT);
Serial.begin(115200);
}
void loop() {
Serial.println(digitalRead(Reed));
channel2 = pulseIn(7, HIGH);
Serial.print("Channel 2:");
Serial.println(channel2);
delay(500);
if ((channel2>=1550)&& (digitalRead(Reed)==1)) {
for(int i = 1500; posServo1 < 1750; posServo1 ++){
servoMotor1.writeMicroseconds(posServo1);
i = posServo1;
if(i>=1501);
}}
else if ((channel2<=1550)&& (digitalRead(Reed)==1))
{ for(int i = 1500; posServo1 > 1000; posServo1 --){
servoMotor1.writeMicroseconds(posServo1);
i = posServo1;
if(i>=1499);
}}
if ((digitalRead(Reed)==0))
{
servoMotor1.write(LOW);
}}
maybe my command for it to end is wrong ?
Exactly what code do you want to stop when teh reed switch reads LOW ?
How is the reed switch wired ?
Does it have pulldown resistor to keep its pin LOW when not active or is it floating at an unknown voltage until activated ?
i want the motor to stop turning(servomotor1) when the reed switch LOW.
the reed switch is just a normal magnetic sensor wired through ground, 5v and pin10.
doesnt have a resistor.
But with this command i can observe that with and without magnet the serial.print the correct value.
Serial.println(digitalRead(Reed));
Exacly what type of servo do you have ?
Is it actually a servo that you can position at at angle and have it stay there by using write(value), of is it a continuous rotation "servo" that you can only control the direction and speed of using write(value) ?
Actually im using a brushless motor. and trying to use servo library to make the codes work. Im not on the right track i guess?