Servo motor with push button start and proximity sensor

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
adafruit_support_bill
 
Posts: 88096
Joined: Sat Feb 07, 2009 10:11 am

Re: Servo motor with push button start and proximity sensor

Post by adafruit_support_bill »

As C++ compilers go GCC's error messages are not the easiest to understand. I don't know of any comprehensive reference. This page covers some of the more common ones: http://www.network-theory.co.uk/docs/gc ... ro_94.html If you get stuck, post them here we might be able to help decode them. Usually the first few lines are the most relevant.

User avatar
jndipworm
 
Posts: 46
Joined: Mon Apr 29, 2013 1:06 pm

Re: Servo motor with push button start and proximity sensor

Post by jndipworm »

I tried the input_Pullup and it didn't seem to make a difference. I did discover that it will work if I put a delay(10) in just in front of the code that looks for the proximity sensor to return to HIGH. I tested this several times and is working. I had some trouble with the button bouncing and giving me double hits with one button push. I looked at t one of the first sketches I posted here with a servo that had the debounce code in it and add it to this sketch. Time will tell if the debounce code is working, I just now got it uploaded and will be running the machine this afternoon. Here is the new and improved code.

Code: Select all

// Controls 2 relays with a pushbutton and proximity switch, runs the cycle once

/*-----( Declare Constants )-----*/
#define RELAY_ON 0
#define RELAY_OFF 1
/*-----( Declare objects )-----*/
/*-----( Declare Variables )-----*/

int hor = 7;         //choose the output pin for the horizontal relay
int ver = 8;         //choose the output pin for the vertical relay
int buttonPin = 2;    //choose the input pin for the pushbutton
int buttonVal = 0;         //variable for reading the pushbutton
int inputProx = 4;   //choose the input pin for proximity switch
int horextled = 5;   //output for horizontal extend LED
int horretled = 6;   //output for horizontal retract LED
int verextled = 10;  //output for vertical extend LED
int verretled = 11;  //output for vertical retract LED

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()    /****** SETUP: RUNS ONCE ******/
{
//-------( Initialize Pins so relays are inactive at reset)----
  digitalWrite(hor, RELAY_OFF);
  digitalWrite(ver, RELAY_OFF);
  
//---( THEN set pins as outputs )----
  pinMode(buttonPin, INPUT);  //declare pushbutton as input
  pinMode(inputProx, INPUT_PULLUP); //declare proximity switch as input
  pinMode(hor, OUTPUT); //declare hor pin as output
  pinMode(ver, OUTPUT); //declare ver pin as output
  pinMode(horextled, OUTPUT);//declare horizontal extend LED
  pinMode(horretled, OUTPUT);//declare horizontal retract LED
  pinMode(verextled, OUTPUT);//declare veritcal extend LED
  pinMode(verretled, OUTPUT);//declare vertical retract LED
}
void loop()
{
  reading = digitalRead(buttonPin);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();
  }
    
  digitalWrite(hor,RELAY_OFF); //turns horizontal relay off
  digitalWrite(ver,RELAY_OFF); //turns vertical relay off
  digitalWrite(horretled, HIGH); //lights horizontal retract LED
  digitalWrite(verretled, HIGH);//lights vertical retract LED
  buttonVal = digitalRead(buttonPin);  //reads value of input
  if (buttonVal == HIGH)        //check if the input is HIGH  
  {
   digitalWrite(horretled, LOW);//turn off horizontal retract LED
   digitalWrite(horextled, HIGH);//turns on horizontal extend LED
   digitalWrite(hor,RELAY_ON); //turns horizontal relay on
    delay(300);
   digitalWrite(hor,RELAY_OFF); //turns horizontal relay off
    delay(10);
  //wait for a proximity sensor to detect a block has returned to it's position
      while (digitalRead(inputProx) == HIGH)
      {
  //waiting for return
      }
   digitalWrite(horextled, LOW);//turns off horizontal extend LED
   digitalWrite(horretled, HIGH);//turns on horizontal retract LED
   digitalWrite(verretled, LOW);//turns off vertical retract LED
   digitalWrite(verextled, HIGH);//turn on vertical extend LED
   digitalWrite(ver,RELAY_ON); //turns vertical relay on
    delay(300);
   digitalWrite(ver,RELAY_OFF); //turns vertical relay off
   digitalWrite(verextled, LOW);//turns off vertical exttend LED
  }
 }

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”