I've a question about a program.
I'm programming a steering for a drawbar, but it have a zero stand.
when the drawbar comes to the zero stand, the drawbar will turn a little left and a little right, and so on.
But i thought there was something wrong in my code
Maybe someone can check my code and give me some advice.
this is the code:
Serial.begin(9600);
pinMode(hoeksensor1, INPUT);
pinMode(rechts, OUTPUT);
pinMode(links, OUTPUT);
pinMode(led, OUTPUT);
}
Sorry, but that is obviously not the code. Did you mess up copy/paste and why did you not put the code in code tags as requested in the stickies of this forum ?
In your Arduino IDE:
Select Tools->Auto Format
Do not proceed until Auto Format works without errors.
Click in the editor window
Press CTRL+A to select all text in the editor window
Press CTRL+C to copy the text
In the forum Editor
Click on the # icon above the window
Press CTRL-V to paste the text.
That being said, have you actually looked at the values returned from the analogRead()? If not, you might be surprised to see what sort of range you get when the drawbar is stationary. You will probably need to increase your deadband (the range of values for which you don't perform a links or rechts operation).
You're printing the return value from analogRead(). Yes it's normal for this to vary up and down a bit.
You haven't explained what the code is intended to achieve but from what you've said and based on what the code does currently I guess you're trying to use the input to decide whether to move something left or right, and then stop it when it reaches the correct position.
The logic isn't ideal for that. First, you have redundant if statements. Secondly, you don't apply any hysteresis or dead band.
I think some logic like this would work better:
if(hoeksensor1 > 576)
{
// too far left, so move right
}
else if (hoeksensor1 > 567)
{
// in the deadband, stop here
}
else
{
// too far right, move left
}
I'm not sure whether I have got the sense of left/right correct, and you have lots of magic values there which I don't understand what they mean, but hopefully this example gives you the idea how how you compare the value to work out which range it is in and take action based on that.