Afternoon all,
I have come to the conclusion that this kind of thing is one of those talents I may never even get a basic grasp of .
Would you be able to assist? I have no doubt that what I Have written in code terms is probably a bit of ham fisted junk so forgive my rather poor attempt and for making your eyes bleed..
it should be very easy but I'm afraid i don't seem to be grasping this very well.... and the more i look at the examples the worse it seems to get
here is what i am attempting and failing miserably at.
I should add that i do have 10k resistors on the switches and they do seem to function well.
1 simple dc motor (not a servo) with a single limit switch. i would like to use a single point in its rotation as a reference/calibration point.
2 it will rotate in one direction until it hits the microswitch and this will be the reference point - if its not already hitting the switch that is....
3 using another 2nd switch i would like to advance the motor for 2 seconds or there about and then stop the motor.
4 i would then like to use the same 2nd switch to reverse the motor until.... yup you guessed it. until it reaches the microswitch.
that's it. thus far....
here is my mess of a code.... go easy on me i just started a few days ago
                       // constants wont change there set here and show the pin numbers
const int buttonPin = 2;Â Â Â Â Â Â Â Â Â Â Â // the number of the internal microswitch digital pin
int buttonState = 0;Â Â Â Â Â Â Â Â Â Â Â Â Â // the variable for reading the button state. this variable will change
const int buttonPin2 = 3;Â Â Â Â Â Â Â Â Â Â // the number of the single hand operation switch open/close
int buttonState2 = 0;
const int Ena = 9;Â Â Â Â Â Â Â Â Â Â Â // the enable input on the digital pin
const int in1 = 5;Â Â Â Â Â Â Â Â Â Â Â // the motor direction control pin 1
const int in2 = 4;Â Â Â Â Â Â Â Â Â Â Â // the motor direction control pin 2
                    // the following variables are unsigned long's because the time, measured in miliseconds,
                    // will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;Â Â // the last time the output pin was toggled
unsigned long debounceDelay = 50;Â Â Â // the debounce time. increase if jittery
void setup() {
                    // put your setup code here, to run once:
 pinMode (buttonPin, INPUT);     // microswitch pin input
 pinMode (buttonPin2, INPUT);     // Microswitch button
 pinMode (Ena, OUTPUT);        // motor enable pin
 pinMode (in1, OUTPUT);        // motor direction control pin in1
 pinMode (in2, OUTPUT);        // motor direction control pin in2
                  Â
}
void loop() {
 buttonState = digitalRead(buttonPin);   // read the push-button input pin to set the microswitch for callibration/reference of the drive motor
 buttonState2 = digitalRead(buttonPin2);  // read the operation switch to determine its instruction.
  if(buttonState == LOW){         // If button state is HIGH (5v) then the motor should do nothing as itr has already moved to the wheel to the microswitch and set it to +5v
  digitalWrite (Ena, HIGH);
  digitalWrite (in1, HIGH);
  digitalWrite (in2, LOW);
  } else {                // if the button is NOT HIGH or in a LOW state then rotate the motor (check physical direction) until the microswitch contacts and goes high
  digitalWrite (Ena, HIGH);
  digitalWrite (in1, LOW);
  digitalWrite (in2, LOW);
 Â
 Â
  }
  if (buttonState2 == HIGH){       // If button 2 is pressed then this should operate the motor for 2 seconds in one direction and then stop
    digitalWrite (Ena, HIGH);
    digitalWrite (in1, LOW);
    digitalWrite (in2, HIGH);
    delay(2000);
     digitalWrite (Ena, HIGH);
     digitalWrite (in1, LOW);
     digitalWrite (in2, LOW);
      }
      if (buttonState2 == HIGH){     // If button 2 is pressed again then this should operate the motor in reverse until it hits microswitch
    digitalWrite (Ena, HIGH);
    digitalWrite (in1, HIGH);
    digitalWrite (in2, LOW);
      }
}
I should add that in its present form it does the correct function to begin with i.e. it rotates the motor until it hits the switch - if i press the second switch it advances for 2 seconds but then just returns to the looking for the switch function. if i hold the microswitch and press the second switch it works how i would like but doesn't switch direction... . i think it has to do with the fact that the 2nd button being pressed while the first is still high is not playing well... as well as the rest of my junk code
please for the love of god point me in the right direction....
please also keep this so a chump like me might ba able to grasp it a bit.
cheers lads n lassies