Motor Shield V2 and DC Motors use LIMIT switches

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
xian5v
 
Posts: 16
Joined: Fri Feb 07, 2014 2:01 pm

Motor Shield V2 and DC Motors use LIMIT switches

Post by xian5v »

I spent a few weeks getting familiar with sparkfun H-bride, which use enable pins and direction pins to move a DC motor. This makes it easy to add a limit switch, because you can just interrupt the directional pin the motor is movie toward. Saves a newbie on de_bounce and coding a switch. so, I need to make 4 of these moving panel. On some recommendations, I ordered up the 2 Adafruit Motor Shield V2! o yeah, "Lookout linear motion" Rad, FOUR motors!! "I am gonna rule the world!!"

not so much!!{

Yikes!! I can't even get the things to start and stop without 21 lines of code. And debounce code does turns my limit switch into a digital on off.
Can someone help me with some simple code that moves a DC (all the forum stuff is on stepper)motor in both directions and stops when it hits a limit switch? Don't really wanna rule the world, just add a limit switch to it.

thank you !!

User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: Motor Shield V2 and DC Motors use LIMIT switches

Post by adafruit_support_bill »

Post the code you have so far.

User avatar
xian5v
 
Posts: 16
Joined: Fri Feb 07, 2014 2:01 pm

Re: Motor Shield V2 and DC Motors use LIMIT switches

Post by xian5v »

This is what I have so far. I think I just need to tell the switches to stop(release) the motor when the are low.
Thanks again Adafruit, for taking care of us non-engineer folks along for the ride!!

Code: Select all

 
// These constants won't change:
const int analogPin = A0;    // pin that the sensor is attached to
const int ledPin = 13;       // pin that the LED is attached to
const int threshold = 400;   // an arbitrary threshold level that's in the range of the analog input

//limitswitches
const int bottomSwitchPin = 2;              // bottom switch is connected to pin2 
const int topSwitchPin = 3;                 // top switch is connected to pin3 
 
 

//motor code
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 

// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
// You can also make another motor on port M2
//Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);


//
void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communications:
  Serial.begin(9600);
  //motor setup
  Serial.println("Adafruit Motorshield v2 - DC Motor test!");

  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
  
  // Set the speed to start, from 0 (off) to 255 (max speed)
  myMotor->setSpeed(200);
  myMotor->run(FORWARD);
  // turn on motor
  myMotor->run(RELEASE);
  
  
  //limit switch
  
  // bottom switch
  pinMode(bottomSwitchPin, INPUT);                  // set bottom switch pin as input
  digitalWrite(bottomSwitchPin, HIGH);              // activate bottom switch resistor
 
// top switch
  pinMode(topSwitchPin, INPUT);                     // set top switch pin as input
  digitalWrite(topSwitchPin, HIGH); 
}

void loop() {
  // read the value of the potentiometer:
  int analogValue = analogRead(analogPin);
  
  // if the analog value is high enough, turn on the LED:
  if (analogValue > threshold) {
    digitalWrite(ledPin, HIGH);
     myMotor->run(FORWARD);
     
     // debounce top limit switch
  void debounceTopReedSwitch(); 
 
    topSwitchPinVal = digitalRead(topSwitchPin);               // read input value and store it in val
    delay(10);
    topSwitchPinVal2 = digitalRead(topSwitchPin);              // read input value again to check or bounce
    
      if (topSwitchPinVal == topSwitchPinVal2) {               // make sure we got 2 consistant readings!
        if (topSwitchPinVal != topSwitchState) {               // the button state has changed!
          topSwitchState = topSwitchPinVal;
        }
        Serial.print (" Top Switch Value: ");                // display "Bottom Switch Value:" 
        Serial.println(digitalRead(topSwitchPin));           // display current value of bottom switch;
        
        
  } 
  else {
    digitalWrite(ledPin,LOW);
     myMotor->run(BACKWARD);
     
     
     //debounce bottom limit switch
 
  void debounceBottomReedSwitch() { 
 
    //debounce bottom reed switch
    bottomSwitchPinVal = digitalRead(bottomSwitchPin);        // read input value and store it in val
    delay(10);
    bottomSwitchPinVal2 = digitalRead(bottomSwitchPin);       // read input value again to check or bounce
    
      if (bottomSwitchPinVal == bottomSwitchPinVal2) {        // make sure we got 2 consistant readings!
        if (bottomSwitchPinVal != bottomSwitchState) {        // the switch state has changed!
          bottomSwitchState = bottomSwitchPinVal;
        }
        Serial.print (" Bottom Switch Value: ");             // display "Bottom Switch Value:" 
        Serial.println(digitalRead(bottomSwitchPin));        // display current value of bottom switch; 
  }

  // print the analog value:
  Serial.println(analogValue);
  delay(1);        // delay in between reads for stability
}

User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: Motor Shield V2 and DC Motors use LIMIT switches

Post by adafruit_support_bill »

You don't need any of that debouncing. Something like this should do it.

Code: Select all

void loop() 
{
  // read the value of the potentiometer:
    int analogValue = analogRead(analogPin);
    topSwitchPinVal = digitalRead(topSwitchPin);               // read input value and store it in val
    bottomSwitchPinVal = digitalRead(bottomSwitchPin);        // read input value and store it in val

	// if the analog value is high enough, turn on the LED:
	if ((analogValue > threshold) && (topSwitchPinVal != LOW))  // assuming pullups on open circuit
	{
		digitalWrite(ledPin, HIGH);
		myMotor->run(FORWARD);
	}
	else if (bottomSwitchPinVal != LOW)
	{
	        digitalWrite(ledPin,LOW);
		myMotor->run(BACKWARD);
	}
	else
	{
		myMotor->run(RELEASE);
	}
 } 

User avatar
xian5v
 
Posts: 16
Joined: Fri Feb 07, 2014 2:01 pm

Re: Motor Shield V2 and DC Motors use LIMIT switches

Post by xian5v »

**SOLVED, THANK YOU ADA_BILL && !!!
I added a a few lines of code for RGD LED, temp sensor, and photocell. Things seem to be working
linear motion meet your ruler!

The fallen fruit, THANK YOU!

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

Return to “Arduino Shields from Adafruit”