Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Can anyone find a problem in this code? on: March 07, 2014, 01:39:37 pm
The system has 1 button and 3 LEDs. Each time the button is pressed, a new LED state/blinking pattern is initiated, and after the third press the cycle continues. Apparently, my code works for the first button press. After the button was pressed the second time, nothing seemed to happen. I can't for the life of me find where the problem is.

Code:
const int light1 = 11; 
const int light2 = 12;
const int light3 = 13;

const int buttonpin = 2;   //
int currentstate = 0;     //current state of button
int previousstate = 0;    //previous state of button
int buttoncounter = 0;

const int interval1 = 500;
const int interval2 = 200;
const int interval3 = 300;
unsigned long startpoint = 0;




void setup() {
  pinMode(light1, OUTPUT);
  pinMode(light2, OUTPUT);
  pinMode(light3, OUTPUT);
 
  pinMode(buttonpin, INPUT);
  Serial.begin(9600);
 
 
}

void loop(){
  currentstate = digitalRead(buttonpin);  //store input value in "currentstate"
 
  delay(10);                              //debounce
  int val2 = digitalRead(buttonpin);      //debounce
  if (currentstate == val2){              //debounce
 
  //compare currentstate to previoustate
  if (currentstate != previousstate) {
      buttoncounter++;
      Serial.print("Current state:  ");  //this will print the current state
      Serial.println(buttoncounter);     
     
      }
    }
    previousstate = currentstate;
   
    if (buttoncounter == 0){        //state 0
      digitalWrite(light1, LOW);
      digitalWrite(light2, LOW);
      digitalWrite(light3, LOW);
    }
   
    else if (buttoncounter == 1){    //state 1
      digitalWrite(light1, HIGH);
      digitalWrite(light2, LOW);
      digitalWrite(light3, LOW);
    }
   
    else if (buttoncounter == 2){
       digitalWrite(light1, LOW);   //state 2
       startpoint = millis();        //set startpoint = the current time in ms
       if (millis() - startpoint >= interval1){   //at 0.5 s
          digitalWrite(light1, HIGH);
          digitalWrite(light2, HIGH);
          digitalWrite(light3, HIGH);
          startpoint = millis();
          if (millis() - startpoint >= interval1){  //at 1.0 s
              digitalWrite(light1, LOW);
              digitalWrite(light2, LOW);
              startpoint = millis();
              if (millis() - startpoint >= interval2){  //at 1.2 s
                digitalWrite(light1, HIGH);
                digitalWrite(light2, HIGH);
                digitalWrite(light3, LOW);
                startpoint = millis();
                if (millis() - startpoint >= interval3){ //at 1.5 s
                  digitalWrite(light1, LOW);
                  digitalWrite(light3, LOW);
                  startpoint = millis();
                  if (millis() - startpoint >= interval3){  //at 1.8 s
                    digitalWrite(light1, HIGH);
                    digitalWrite(light3, HIGH);
                    startpoint = millis();
                    if (millis() - startpoint >= interval2){  //at 2 s
                       digitalWrite(light1, LOW);
                       digitalWrite(light2, LOW);
                       digitalWrite(light3, LOW);
                    }
                  }
                }
             }
          }
       }
     }
   
    else if (buttoncounter >= 3){  //if button is pressed more than two times
      buttoncounter = 0;           //buttoncounter is reset to 0 to repeat the cycle
  }
}
2  Topics / Robotics / Re: Help with design project. (arduino timing) on: November 11, 2012, 01:54:37 am
I switched the signal wires so that the pin 9 controls myotherservo and pin 10 controls myservo. Doing this, I was able to see that the code DOES work fine. It is the servo (myotherservo) that does not want to work right. The motor works fine. I don't know what the problem could be here. I tried three different size resistors. The way I hooked them up was by wiring the ground of the battery through the resistor into the ground of the servo. Is this correct? Doing this was as if the servo wasn't even attached at all.
3  Topics / Robotics / Re: Help with design project. (arduino timing) on: November 11, 2012, 12:48:48 am

please use the # button to apply proper code tags for your code

something like this ?
Code:
#include <Servo.h>

Servo myservo;
Servo myotherservo;

unsigned long start = 0;
bool flag = false;

void setup()
{
  myservo.attach(9);
  myotherservo.attach(10);
  start = millis();
  flag = false;
}

void loop()
{
  myservo.write(120);

  unsigned long now = millis();
  if (now - start > 12500UL && flag == false)
  {
     flag = true; // do it only once
     myotherservo.write(120);
  }
}
This seems to be doing the exact same thing as the code I had. The servo, "myotherservo", turns on immediately. It does rotate again after 12.5 seconds, but the first rotation is what I need to NOT happen. I don't see why the code you gave me shouldn't work though. Of course, I am not familiar with the boolean constants true and false. I don't really get how they work. Also, it seems to me like (now - start) should always equal 0 since they are both assigned to millis(). Perhaps I am misunderstanding how millis() works? I am thinking that it keeps a constant count of how much time has elapsed and continuously stores it into the variable you've assigned to it (in this case start or now). This must be wrong though. I read the info on it on this website.

Thank you so much for your help!

Quote
The motor drives the robot forward in a straight line.
If it really does, your damned lucky, then.


It does drive in a straight line. It might not be exactly straight, but it works well enough for our purposes. This robot uses a single motor drive, so it isn't as hard to keep straight as a differential drive system.
4  Topics / Robotics / Help with design project. (arduino timing) on: November 10, 2012, 04:48:57 pm
This is for a design project. I need to program my robot to drive forward 10 feet, drop a wooden block, and drive forward 10 more feet.

What I need help with:

The robot uses one motor (actually a continuous rotation servo) and one servo. The motor drives the robot forward in a straight line. The servo controls the dropping of the block. Basically there is a bent shaft that the block rests on that will rotate up and out of the way to drop the block.

So I know that the robot takes 12.5 seconds to reach 10 feet. My idea was to program the robot to just keep driving until switched off, and to turn the servo on after the system has been turned on for 12.5 seconds. Sounds like it would be simple enough to program... I tried using an If statement:

myservo is the motor for the drive system. myotherservo is the servo for the release mechanism.

#include <Servo.h>

Servo myservo;
Servo myotherservo;
int time = millis();

void setup()
{
  myservo.attach(9);
  myotherservo.attach(10);
 
}

void loop()
{
  myservo.write(120);
 
  if(time > 12500)
  {
  myotherservo.write(120);
  }
}


When I use this code, the motor (myservo) runs continuously as it should, but the servo (myotherservo) just runs immediately as the system is switched on. After 12.5 seconds, it runs again. I have tried the idea of this code in different ways, but no matter what I do, it behaves the same way.

I am new to programming with an Arduino (and programming in general) so I am having a lot of trouble finding out what I'm doing wrong. If anyone could help lead me in the right direction it would greatly appreciated. I need this thing running perfectly by Tuesday.

Thanks in advance for your help.
5  Topics / Robotics / How to modify "sweep" code for straight line drive on: November 04, 2012, 03:32:36 pm
My overall goal is to use an arduino uno to control a servo to rotate continuously in order to drive my robot in a straight line. First of all, when I use the regular, unedited Sweep code with my servo, it rotates one nearly full rotation, stops, then rotates again in the same direction. It should be rotating back and forth, not in one direction. However, I only need to go in one direction anyways, so I don't necessarily care to know how to fix this.

My main problem is the motor stopping. I tried taking the delay out, or setting it to 0, and it still did not rotate continuously. How can I modify this code, or is there any other code out there that I could use to make my servo rotate continuously without stopping?

The "servo" I'm using is actually a Vex 2 wire motor with a 3 wire motor controller 29. The motor controller 29 is supposed to make it able to be programmed as if it were a servo. It's actually a dc gear motor. This motor is made specifically to drive robots, so I assume I shouldn't have to take any sort of mechanical stop out of it like I've seen in some continuous drive servo guides.
6  Using Arduino / Project Guidance / Re: *Total noob working on school project*- How to know what I need? on: September 27, 2012, 09:17:55 am
Ok first let's get some corrections of very fundamental concepts out of the way...
We are sending 9V through the Arduino, which takes up 5V, so we now have 4V going to the transistor. So would 4V be the forward Voltage to the transistor?
The way you have this wired up the motor is being supplied off the 5V regulator on the Uno board.  This is not generally a good idea for motors and other actuators, becase the Uno is limited to supplying a maximum 200 mA through the 5V pin.  You may have been lucky and not needed to draw that much current for your motor, but exceeding this can damage the Uno.  So when you do get a new motor, make sure it can function at the voltage of whatever power supply you are using (e.g. if you continue to use a 9 V battery, get a motor with a maximum voltage rating higher than 9 VDC).  That way it can be directly connected to the power supply and not go through the power loop on the Uno.

The collector has a current of 600mA or .006A. Correct?
No, 1 A = 1000 mA, therefore 600 mA = 0.6 A.  Furthermore the 600 mA is the maximum the current that the collector can handle for sustained periods, and more than that will likely damage the transistor.  The actual current at any given moment depends upon the voltage and current at the transistor's base.  We'll get back to that below...

So do I just add that current to the current coming from the battery? What would the current from the battery be?
The current from the battery will be the total current for the entire circuit.  If you hook the motor directly to the battery and have another wire going to the VIN on the Uno board, it will be the sum of the currents drawn by the motor and the Uno (per Kirchhoff's Current Law ).  Becasue this isn't a purely resistive circuit, and you don't know a lot of electrical theory, the easiest way to determine the currents present in the circuit is to actually measure them with your multimeter (remember to do it with the meter in series with the point you are measuring).  

We do know how to put a resistor on this setup, we just haven't done it yet. We have 10kohm, 220ohm, and 270ohm resistors (these came in our Arduino kit).
When you are using an arduino pin as an output you always want at least a couple hundred ohms on the pin to limit the current to below 40 mA!  Otherwise you could permenantly damage the pin on the microcontroller IC.  If that happens at best you won't be able to use it reliably in the future.

My program is just a modified version of the "blink" program, which is what our proffessor reccomended in class. I have pin 2 coded to wait 32 seconds then turn the motor on, "digitalWrite(pin 2, HIGH)", then stop after 6 seconds (which should be enough time to tip the can over, but this would be no problem to edit if need be). You can see the red wire going from pin 2 to the base of the transistor. The black wire connects the emitter to the blue wire which connects to the pin labeled (GND). The red wire connected to the collector is what we connect to the positive part of the motor. The green wire, which is in the 5V pin on the power strip connects to the negative part of the motor. The motor DID turn on with this setup, and seemed as if it would have enough power. The motor did NOT turn on without the transistor.
OK, it seems like you have everything connected like a basic low side switch.  Which is all well and good, except you don't have a resistor between pin 2 and the transistor's base.  This is not a good idea; (as mentioned previously) you always need to make sure the current from an Arduino pin being used as an output is below 40 mA and the amount of current necessary to use this transistor as switch is quite a bit less than 40 mA.  I would put at the very least the 220 Ω resistor, though you could probably use 10K Ω and that would be closer to the optimal biasing.

The motor says "SuperJack" and "R-3" on the bottom. I can't find any info on it on the web. I found SuperJack's company website, but it wasn't much help. We are probably going to buy a different motor anyways, since we want one that has a gear on the shaft rather than just a smooth cylindrical shaft.
Well that sometimes happens, but at least you tried.  If you do get a different motor be sure that information about the normal operating values and maximum ratings for both voltage and current are available, either directly from the seller or on the manufacturer's website, before you purchase anything.

Basically, I want the motor to be able to get enough power for at most maybe 10 seconds, and I want nothing to be fried/ruined in the process.
With a few changes to your current design that is certainly possible.  First, see if you can wire the positive lead of your motor directly to the battery, not the 5V pin.  Second, you probably want to have a diode, a 1N4004 will do, wired in parallel with the motor.  Have the end with the end with the light colored band (the cathode) connected to the positive terminal of the motor, and the other (the anode) to the motor's negative terminal.  Third, you really should replace the 9V battery with a number of alkaline AA batteries in series.  The exact number will depend on if you get a new motor and what type, but 4 to 6 will probably work.  Battery holders wired this way should be easy to find, especially for AAs.      
Thank you so much!!! That was extremely helpful. Last night, we had a guy that did this project last semester come to help us with the wiring. He didn't seem to know all that much either, but he told us to get 2 9V batteries and use one to power the arduino and the other to power the motor. We attached a fishing weight to our motor, and it wasn't strong enough to spin it fast (using just the 1 battery). So I'm headed out to Radioshack now (bringing some notes I've jotted down about what you've told me), and I'll see if I can find some more appropriate equipment.
7  Using Arduino / Project Guidance / Re: *Total noob working on school project*- How to know what I need? on: September 26, 2012, 01:32:03 pm
The basic approach might work, but I suggest if you're trying to make the can fall over then 'really fast' is not a good way to do it. You want as much weight as you can get, as high up and as far away from the central axis of the can as possible, and then spin it around that axis at about the same frequency that the can will naturally wobble if you put it on end and then spin it so the base rolls around in a small circle. That way your weight will drive the natural resonant frequency of the motion and tend to amplify it. If you try to drive it much faster, it's likely to just sit there rattling. Once you have the hardware in place you'll be able to experiment and see what speed works best. I would have thought that a rigid rotor would work a lot better than a weight on a string.
hmmm, this is very insightful. I don't have a great understanding of physics (currently in Physics 1), but I can see what you are saying. A rigid rotor might actually work better. I really don't know. We'll probably go with whichever way is easier/cheaper for us to put together.
8  Using Arduino / Project Guidance / Re: *Total noob working on school project*- How to know what I need? on: September 26, 2012, 01:27:41 pm
First a schematic or other diagram of how everything is wired would be very helpful in most cases when you are asking for advice on this forum.

So we bought a small DC motor (not sure of the specs)

If it has any markings, look them up on-line.  You might be able to find a data sheet for it.  After all this is the Information Age... smiley-razz

I have a 9V battery which is to power an Arduino Uno AND a DC motor.

Regardless of your other problems if it's the standard rectangular 9V battery, you want a different power supply.  A 9V battery usually has less than an 700 mAh,(or it can also be expressed as  0.7 Ah), of charge capacity.  The units "mAh" or "Ah" stand for "milliAmpere-hours" or "Ampere-hours", in other words how much current the battery can supply for one full hour.  This is enough to power an Arduino and a few other things like LEDs for several hours, but it wouldn't power most DC motors for long and at least some it couldn't supply enough current to get them moving (though it might not be the case with your motor).

On the other hand, common alkaline AA usually have a capacity of over 2200 mAh.  They have a nominal voltage of 1.5 VDC (nominal because the exact voltage of any battery varies a bit based on how much charge is remaining), but six of them can be put in series to get a nominal 9 VDC.  By the way, all the standard "letter" sized alkaline batteries have nominal voltages of 1.5 VDC.  The only difference is the maximum charge capacity and as you might guess the capacity increases with the battery size.

What kind of DC motor do I need?
The primary requirement will be "Can the motor do what I want it to do?", in this case spin your weight reliably with enough force to tip over the can.  Any other requirements will be imposed by limitations like volume, weight, and power constraints (which in this case shouldn't be a problem).  With a few extra components and the right programming an Arduino can control almost any DC motor.

What kind of transistor do I need? What kind of resistors do I need, if any?
The transitor will have to be able to handle the voltage and the current levels necessary to power your DC motor, while still being able to be controlled by the output of an Arduino pin, which can only output 0 or 5 VDC  @ a maximum current of 40 mA.  The number and rating of resistors will be determined by what's necessary to properly bias the transistor.

You should also have a couple of diodes as well.

Here's a link to a tutorial about working with small DC motors and Arduinos.  It's written by an active forum member that goes by "Grumpy_Mike".

Here are some pictures.

This is everything


Here is a close up of the wiring setup.


My program is just a modified version of the "blink" program, which is what our proffessor reccomended in class. I have pin 2 coded to wait 32 seconds then turn the motor on, "digitalWrite(pin 2, HIGH)", then stop after 6 seconds (which should be enough time to tip the can over, but this would be no problem to edit if need be). You can see the red wire going from pin 2 to the base of the transistor. The black wire connects the emitter to the blue wire which connects to the pin labeled (GND). The red wire connected to the collector is what we connect to the positive part of the motor. The green wire, which is in the 5V pin on the power strip connects to the negative part of the motor. The motor DID turn on with this setup, and seemed as if it would have enough power. The motor did NOT turn on without the transistor.

Our transistor is an NPN transistor purchased from RadioShack. Here is a data sheet for it:  http://rocky.digikey.com/WebLib/On-Semi/Web%20Data/MPS2222,%20MPS2222A.pdf

I do not understand much of what it's saying. The equations we've been given in class are I=V/R, which I understand to be Current=Voltage/Resistance. According to the notes from the lecture, the easy way to calculate the resistance you need is to use R=(Source Voltage - Forward Voltage)/Forward Current. Honestly, I haven't thought about it too much, but I don't understand how to determine my forward voltage or forward current. I assume the source voltage is 9V (if I use my 9V battery).

The motor says "SuperJack" and "R-3" on the bottom. I can't find any info on it on the web. I found SuperJack's company website, but it wasn't much help. We are probably going to buy a different motor anyways, since we want one that has a gear on the shaft rather than just a smooth cylindrical shaft.

We do know how to put a resistor on this setup, we just haven't done it yet. We have 10kohm, 220ohm, and 270ohm resistors (these came in our Arduino kit).

The reason we are using an Arduino for this project is because we will most likely need to use one for our final design project coming up, and we wanted to get some practice with how it works.

So from what you have said about the 9V batter, I think that this setup does NOT need a resistor. Is this correct?

We are sending 9V through the Arduino, which takes up 5V, so we now have 4V going to the transistor. So would 4V be the forward Voltage to the transistor?

The collector has a current of 600mA or .006A. Correct? So do I just add that current to the current coming from the battery? What would the current from the battery be?

I know I am all over the place with these questions, I just don't know what's going on. I haven't looked at your tutorial yet, so I apologize if anything I've asked is answered there.

Basically, I want the motor to be able to get enough power for at most maybe 10 seconds, and I want nothing to be fried/ruined in the process.
9  Using Arduino / General Electronics / *new to electronics* - How to know what types of transistors/resistors/motor on: September 26, 2012, 10:52:30 am
skip to the bottom to see my question in bold

I am working on a design project for school. We have to build a setup inside a can that will make the can tip over. We have our basic design figured out, and I have programmed my Arduino Uno to do what it needs to do. Let me start by giving you an inventory of what I know I need:

- Arduino Uno
- Dc motor (to spin a weight attached to a wire around really fast)
- transistor
- resistor???
- wires, etc.
- battery
 
So we bought a small DC motor (not sure of the specs) and an Arduino kit just to play with and try to figure it out. We started by testing our system with an LED in place of the motor, and it worked. Then we used the motor instead, and it did not work. So we eventually (after roughly 2 hours) figured out how to use a transistor to make the motor run in our system.

We were not very careful in what kind of motor, transistor, and resistors we purchased. Off the top of my head, I am not sure of the specs of any of these things, but I just assume we got all the wrong stuff.

So that should be enough of an introduction. Let me try to put my question in a concise format:

I have a 9V battery which is to power an Arduino Uno AND a DC motor. What kind of DC motor do I need? What kind of transistor do I need? What kind of resistors do I need, if any?

Nobody on my team has any electrical background. We are mechanical engineering students. We do not understand voltage and current very well. Personally, I know nothing about any of this. Thank you for your help!
10  Using Arduino / Project Guidance / *Total noob working on school project*- How to know what I need? on: September 26, 2012, 10:21:05 am
skip to the bottom to see my question in bold

I am working on a design project for school. We have to build a setup inside a can that will make the can tip over. We have our basic design figured out, and I have programmed my Arduino Uno to do what it needs to do. Let me start by giving you an inventory of what I know I need:

- Arduino Uno
- Dc motor (to spin a weight attached to a wire around really fast)
- transistor
- resistor???
- wires, etc.
- battery
 
So we bought a small DC motor (not sure of the specs) and an Arduino kit just to play with and try to figure it out. We started by testing our system with an LED in place of the motor, and it worked. Then we used the motor instead, and it did not work. So we eventually (after roughly 2 hours) figured out how to use a transistor to make the motor run in our system.

We were not very careful in what kind of motor, transistor, and resistors we purchased. Off the top of my head, I am not sure of the specs of any of these things, but I just assume we got all the wrong stuff.

So that should be enough of an introduction. Let me try to put my question in a concise format:

I have a 9V battery which is to power an Arduino Uno AND a DC motor. What kind of DC motor do I need? What kind of transistor do I need? What kind of resistors do I need, if any?

Nobody on my team has any electrical background. We are mechanical engineering students. We do not understand voltage and current very well. Personally, I know nothing about any of this. Thank you for your help!
Pages: [1]