Help Combining Circuits

For makers who have purchased an Adafruit Starter Pack, get help with the tutorials here!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
pcook911
 
Posts: 3
Joined: Sun Jun 17, 2012 5:27 pm

Help Combining Circuits

Post by pcook911 »

I have had the Adafruit ARDX Experimentation Kit for Arduino (https://www.adafruit.com/products/170) for a while and have just now started to get beyond the basic included circuits included in the ARDX booklet. I am trying to combine circuits #3 and #7 into one so that I have buttons that will start and stop the rotation of an electric motor. I am having difficulty and the results aren't satisfactory. Any help would be appreciated.

I built circuit #7 on the top part of the breadboard and circuit #3 on the bottom part using this sketch

Code: Select all

#include <SoftwareSerial.h>

/*
  Button

 Turns on and off a light emitting diode(LED) connected to digital
 pin 13, when pressing a pushbutton attached to pin 2.


 The circuit:
 * LED attached from pin 13 to ground
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground

 http://www.arduino.cc/en/Tutorial/Button
 */

// constants won't change. They're used here to set pin numbers:
const int buttonPin1 = 2;     // the number of the pushbutton pin
const int buttonPin2 = 3;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
const int motorPin = 9;  // define the pin the motor is connected to (if you use pin 9,10,11 or 3 you can also control speed)
int buttonState = digitalRead(buttonPin1);

void setup() {
  pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
  pinMode(buttonPin1, INPUT); // initialize the pushbutton pin as an input
  pinMode(buttonPin2, INPUT); // initialize the pushbutton pin as an input
  pinMode(motorPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {

  
int buttonState = digitalRead(buttonPin1);
  
  if (digitalRead(buttonPin1) == LOW) { // check if the pushbutton is pressed. if it is, the buttonState is LOW
    digitalWrite(ledPin, HIGH); // turn LED on:
    digitalWrite(motorPin, HIGH); // turns the motor On
    
    Serial.println(buttonState);
  }
  else if(digitalRead(buttonPin2) == LOW) { // check if the pushbutton is pressed. if it is, the buttonState is LOW
    digitalWrite(ledPin, LOW); // turn LED off
    digitalWrite(motorPin, LOW);  // turns the motor Off
    
    Serial.println(buttonState);
  }
}
Frizting PNG of wiring layout
https://plus.google.com/photos/10699157 ... kY3jx8OPVg

When I press Button 1 (buttonPin1) the LED turns on and the motor starts turning and I get a series of 0s for the buttonState on the Serial Monitor only while I have the button pressed. As soon as I let off the button the LED turns off, the motor stops turning and I get a value of one for buttonState on the Serial Monitor. If I load the sketch for circuit #3 the motor functions as expected, turning on then off. If I load the sketch for circuit #7 with additional code for the Serial Monitor output everything functions as it should, when I press Button 1 the LED turns on and I get a value of 0 on the Serial Monitor and when I press Button 2 (buttonPin2) the LED turns off and I get a value of 1 on the Serial Monitor.

So why doesn't it work when I used the combined sketch? Does it have to do with button bounce? If not where am I going wrong?

TIA,

Paul

User avatar
Franklin97355
 
Posts: 23911
Joined: Mon Apr 21, 2008 2:33 pm

Re: Help Combining Circuits

Post by Franklin97355 »

So why doesn't it work when I used the combined sketch?
Is that the complete combined program and what does it do when they are combined? What do you expect it to do?

pcook911
 
Posts: 3
Joined: Sun Jun 17, 2012 5:27 pm

Re: Help Combining Circuits

Post by pcook911 »

Looking back at the post it doesn't seem as clear as it seemed in my head.

Yes that code is the code that I am using. My expected result is that I press button 1 and the LED lights up, the motor starts spinning and I get an output value of 0 on the Serial Monitor. I would then press button 2 and the LED would go dim, the motor would stop spinning and I get an output value of 1 on the Serial Monitor. My actual results are that the LED turns on, the motor runs and I get on output value of 0 on the Serial Monitor only when I press and hold button 1. As soon as I let off of button 1 the LED goes dim, the motor stops and I get an output value of 1 on the Serial Monitor.

If I load the sketch for Circ #3 the motor works as it should. If I load the sketch for Circ #7 the buttons and LED work as they should.

TIA,

Paul

User avatar
Franklin97355
 
Posts: 23911
Joined: Mon Apr 21, 2008 2:33 pm

Re: Help Combining Circuits

Post by Franklin97355 »

Nothing stands out as wrong. Try it without the motor and transistor to see what happens. You are powering the motor through the transistor but your power is still coming from the Arduino, this may be causing noise.

pcook911
 
Posts: 3
Joined: Sun Jun 17, 2012 5:27 pm

Re: Help Combining Circuits

Post by pcook911 »

franklin97355,

I removed the jumper from the collector of the transistor to the motor and the switches, LED and Serial Monitor behave as expected. I have noticed that sometimes the when I push button 1 the motor and LED will run and the Serial Monitor will show 0 for a few seconds and then show 1 for about 10 rows and finally the motor and LED will stop after a couple of seconds. What does this tell me and how can I avoid this? Or is this circuit just not practical? My overall goal is just to learn how to control components based on input from other components and I figured that it would be pretty easy to turn a motor on and off with two switches. Thanks for the help so far.

Paul

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

Return to “Arduino Starter Pack”