Sounds like your input is 'floating'. You need either a pullup or a pulldown. Whatever you have isn't working. Post a photo of your wiring.When I opened the serial monitor and just let it sit, the state would go from 0 to 1 and back to 0. The whole time the switch is depressed (open) and should read 0.
Motor Shield v2 kills Mega 2560
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- adafruit_support_bill
- Posts: 88090
- Joined: Sat Feb 07, 2009 10:11 am
Re: Motor Shield v2 kills Mega 2560
- jndipworm
- Posts: 46
- Joined: Mon Apr 29, 2013 1:06 pm
Re: Motor Shield v2 kills Mega 2560
Button setup
- Attachments
-
- pull down button.jpg (113.9 KiB) Viewed 270 times
- adafruit_support_bill
- Posts: 88090
- Joined: Sat Feb 07, 2009 10:11 am
Re: Motor Shield v2 kills Mega 2560
It looks like you have a resistor connected from the +5v rail to the GND rail. That does not do anything.
You need to connect your pulldown between the digital pin and GND.
You need to connect your pulldown between the digital pin and GND.
- jndipworm
- Posts: 46
- Joined: Mon Apr 29, 2013 1:06 pm
Re: Motor Shield v2 kills Mega 2560
I know it's hard to see, I need to take better pictures. The resistor is plugged into the negative rail on the breadboard, the red wire comes from the positive rail to the other side of the button. The yellow wire is on the side of the button with the resistor and goes to pin 40
- adafruit_support_bill
- Posts: 88090
- Joined: Sat Feb 07, 2009 10:11 am
Re: Motor Shield v2 kills Mega 2560
There mus be a bad connection there somewhere if your input is floating.
- jndipworm
- Posts: 46
- Joined: Mon Apr 29, 2013 1:06 pm
Re: Motor Shield v2 kills Mega 2560
I moved the signal wire (yellow) to a crew terminal and that fixed the button state to a constant 0. I checked the state of the micro switch and it is now floating, 0 to 1 while sitting idle, it should be 0. Do I need to add a resistor to this circuit to stabilize it?
- adafruit_support_bill
- Posts: 88090
- Joined: Sat Feb 07, 2009 10:11 am
Re: Motor Shield v2 kills Mega 2560
Which leg of the microswitch did you test?I checked the state of the micro switch and it is now floating, 0 to 1 while sitting idle, it should be 0. Do I need to add a resistor to this circuit to stabilize it?
If you have a pulldown to GND and it is still floating, then your pulldown is not working. You have either a bad ground, a bad resistor or a bad connection somewhere in between.
- jndipworm
- Posts: 46
- Joined: Mon Apr 29, 2013 1:06 pm
Re: Motor Shield v2 kills Mega 2560
When I checked the state of the micro switch I did it with digitalRead(TtableSwitch) in the code. The micro switch has 5v fed in from the arduino mega and a line coming out to pin 23, I do not have a ground in this circuit.
- adafruit_support_bill
- Posts: 88090
- Joined: Sat Feb 07, 2009 10:11 am
Re: Motor Shield v2 kills Mega 2560
That would explain why your pulldown is not working. But what did you mean by this statement in your previous post?I do not have a ground in this circuit.
Looking at the photo in that post, it does seem that your negative rail is (indirectly) connected to ground.The resistor is plugged into the negative rail on the breadboard
- jndipworm
- Posts: 46
- Joined: Mon Apr 29, 2013 1:06 pm
Re: Motor Shield v2 kills Mega 2560
In your first post after the picture of the button setup you said:
I replied:It looks like you have a resistor connected from the +5v rail to the GND rail. That does not do anything.
You need to connect your pulldown between the digital pin and GND.
After finding out the button was floating you suggested there was probably a bad connection. After messing with it to find the bad connection I moved the yellow signal wire coming from the button to a screw terminal and that fixed the button. The digitalRead showed a constant 0, no more floating. I then changed the sketch to check the state of the micro switch to see if it was stable. It showed the switch was floating as well. That's where we currently are. The micro switch has only 1 wire (5v) going to it and 1 wire coming back to pin 23.I know it's hard to see, I need to take better pictures. The resistor is plugged into the negative rail on the breadboard, the red wire comes from the positive rail to the other side of the button. The yellow wire is on the side of the button with the resistor and goes to pin 40
- adafruit_support_bill
- Posts: 88090
- Joined: Sat Feb 07, 2009 10:11 am
Re: Motor Shield v2 kills Mega 2560
Wired that way with no pulldown or pullup, you should expect it to float.I then changed the sketch to check the state of the micro switch to see if it was stable. It showed the switch was floating as well. That's where we currently are. The micro switch has only 1 wire (5v) going to it and 1 wire coming back to pin 23.
The easiest and most reliable way to wire a switch is to use the internal pullup resistors that are built into the processor. Just add a "pinMode(pin, INPUT_PULLUP);" command in your setup() function to enable them.
Then wire your switch between the signal pin and GND.
In the open state, your pin will read HIGH. When the button is pressed, it will read LOW.
- jndipworm
- Posts: 46
- Joined: Mon Apr 29, 2013 1:06 pm
Re: Motor Shield v2 kills Mega 2560
Made the changes you suggested, TtableSwitch is now INPUT_PULLUP and changed the logic from HIGH to LOW, while(digitalRead(TtableSwitch)==LOW); . The TtableSwitch digitalRead is now constant 1. I did the same with TtableButton. Wired it with wire to gnd and one wire to pin 39. TtableButton in now INPUT_PULLUP and changed the logic from HIGH to LOW, if (TtableVal == LOW); . The TtableButton digitalRead in now constant 1. I still have the issue with it ignoring the while statement, while(digitalRead(TtableSwitch)==LOW)
{ //waiting for switch
}
The motor runs till it times out, I've changed the delay just to check that it is reading it and it does react to the delay. Here's the code as it now.
{ //waiting for switch
}
The motor runs till it times out, I've changed the delay just to check that it is reading it and it does react to the delay. Here's the code as it now.
Code: Select all
/*
For use with the Adafruit Motor Shield v2
Connects a dc motor to motor port #1 (M1)
*/
#include <Bounce.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
int TtableButton = 39; //choose the input pin for the pushbutton
int TtableVal = 0; //variable for reading the pushbutton
int TtableSwitch = 23; //declares the table switch on pin 23
// 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 *TtableMotor = AFMS.getMotor(1);
// You can also make another motor on port M2
//Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);
// Instantiate a Bounce object with a 500 millisecond debounce time
Bounce bouncer = Bounce( TtableButton,500 );
void setup() {
pinMode(TtableButton, INPUT_PULLUP); //declares pushbutton as input
pinMode(TtableSwitch, INPUT_PULLUP); //declares tutntable switch as input
Serial.begin(9600); // set up Serial library at 9600 bps
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)
TtableMotor->setSpeed(65);
TtableMotor->run(FORWARD); // turn motor on
TtableMotor->run(RELEASE); // turn motor off
}
void loop() {
Serial.print("TtableButton State: ");
Serial.println(digitalRead(TtableButton)); // Read the pin and display the value
delay(1000);
bouncer.update ( );// Update the debouncer
int value = bouncer.read();// Get the update value
TtableVal = digitalRead(TtableButton); //reads value of input
if (TtableVal == LOW) //check if the input is LOW
{
// Serial.println("Button was pushed");
TtableMotor->run(FORWARD);
delay(750);
//Serial.println("Waiting...");
while(digitalRead(TtableSwitch)==LOW)
{ //waiting for switch
}
TtableMotor->run(RELEASE); //releases motor
Serial.println("Motor released");
}
}
- adafruit_support_bill
- Posts: 88090
- Joined: Sat Feb 07, 2009 10:11 am
Re: Motor Shield v2 kills Mega 2560
Have you tested the switch to verify that it makes contact when you press it?
- jndipworm
- Posts: 46
- Joined: Mon Apr 29, 2013 1:06 pm
Re: Motor Shield v2 kills Mega 2560
How would I do that?
- adafruit_support_bill
- Posts: 88090
- Joined: Sat Feb 07, 2009 10:11 am
Re: Motor Shield v2 kills Mega 2560
If you have a multimeter, check that there is continuity through the switch when it is pressed.
https://learn.adafruit.com/multimeters/continuity
https://learn.adafruit.com/multimeters/continuity
Please be positive and constructive with your questions and comments.