1

I'd like to preface this by saying I'm a complete beginner, but I have learned a ton already on this project, and I feel like I'm almost there.

I'm using an Arduino Uno and an Adafruit motor shield. I am working on a project for mechanical engineering and I need lots of amps for lots of torque for my mechanism. I'm including my code although I know it doesn't work, I haven't had the chance to debug it yet, and I'm really not sure if I'm on the right track.

So I know that it's possible to draw more amperage than is strictly allowed from the Arduino. What I don't know for sure is how to code it. Attached is my preliminary attempt.. I kind of assumed that the maximum speed would be the maximum allowable speed of the motor, regardless of the Arduino's capabilities, and scaled the output MAXampDrawSpeed as a function of that. To be specific, my motor can draw up to 20 amps, which would be too much for the Arduino to handle, so I scaled it down accordingly, with 255 being that maximum 20 amp output. I'm not sure if I'm on the right track there.

So the other problem is - the TA for my class made a simplified code reference file, sort of predefining a lot of the things that I manually defined in my code (or tried to at least). I didn't realize this, and when I asked him a question, he sort of shot me down and said "you should use my reference code" -- but his code doesn't compile for me. I had some problems with libraries (I fixed them!), but this isn't a library. It's a reference file, like the tabs up at the top. But it won't even compile for me. My feeling is that I may have annoyed him with my many many stupid questions up until this point and he's just kind of like "girl, get it together"... So the question there is -- am I almost there with my track or should I figure out what's wrong with his code and try to fix it his way so that I can ask him questions without being such a bother to him? And also is that likely to be a hindrance on drawing the extra amperage that I really really want? I am prepared to use heat dissipation methods as are necessary, and any advice you could give me there would also be appreciated.

If you need any more information, about my mechanism or anything I'd be glad to share that as well. I'm parallel mounting one motor to the four dc motor mounts on my Arduino in order to draw maximum current in that way as well. I have two switches controlling the output, one of the switches will engage the program for the maximum distance competition and the other for the accuracy competition.

So again, this code isn't right. I haven't even started on debugging it, because I didn't want to waste my time. Maybe you'll be able to follow my logic. I know there aren't many notes. If you need me to add notes for clarity I would also be glad to take the time to do that.

Thanks!

#include <Stepper.h>
#include <AccelStepper.h>
#include <Servo.h>
#include <Adafruit_MotorShield.h>
#include <Wire.h>
#include "Arduino.h"

#define STEP_TYPE_1   SINGLE
#define STEP_TYPE_2    SINGLE
#define STEPS_1    200
#define STEPS_2    200

Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *DC1 = AFMS.getMotor(1);
Adafruit_DCMotor *DC2 = AFMS.getMotor(2);
Adafruit_DCMotor *DC3 = AFMS.getMotor(3);
Adafruit_DCMotor *DC4 = AFMS.getMotor(4);
Servo Servo1;

void setup() {
  digitalWrite(A0, HIGH); //Turns on the pullup resistor for A0
  int i = analogRead(A0); //Reads the value on the analog pin and saves     this as variable "i"
  AFMS.begin(); //Initializes the motorshield board
  int motorPin1 = 1;
  int motorPin2 = 2;
  int motorPin3 = 3;

  pinMode(A0, INPUT); //Sets the Analog Pin 0 as an input
  digitalWrite(A0,HIGH);
  pinMode(A1, INPUT);
  digitalWrite(A1,HIGH);
  pinMode(A1, INPUT);
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  Servo1.attach(9);
}

void loop()  { 
  int distancepin = 0; //Just integer quantities, i like to have too     many of them 
  int accuracypin = 0;
  int inPin1 = 0; //switch pin values
  int inPin2 = 0; 
  int MAXampDrawTime = 75;
  int MAXampDrawSpeed = 178;
  int NORMampDrawTime = 225;
  int NORMampDrawSpeed = 61.2;
  int DrawTime = MAXampDrawTime+NORMampDrawTime;
  int ServoTime = 450;
  long previousMillis =0;
  long intervalMAXamps = 100;
  long intervalNORMamps = 400;
  DC1->setSpeed(MAXampDrawSpeed);
  DC2->setSpeed(MAXampDrawSpeed);
  DC3->setSpeed(MAXampDrawSpeed);
  DC4->setSpeed(MAXampDrawSpeed);
  inPin1 = digitalRead(A0);
  inPin2 = digitalRead(A1);
  if (inPin1 == HIGH){
    unsigned long time_zero = millis();
    unsigned long current_time = millis();
    digitalWrite(1,HIGH); //turn motor on to maximum draw amperage
    digitalWrite(2,HIGH);
    digitalWrite(3,HIGH);
    digitalWrite(4,HIGH);
    while((millis()-time_zero)<(400000)){
      if( millis() >= (time_zero+NORMampDrawTime)){
      DC1->setSpeed(NORMampDrawSpeed); //set motor on to normal draw     amperage
      DC2->setSpeed(NORMampDrawSpeed);
      DC3->setSpeed(NORMampDrawSpeed);
      DC4->setSpeed(NORMampDrawSpeed);
      digitalWrite(1,HIGH); //turn motor on to normal draw amperage  
      digitalWrite(2,HIGH);
      digitalWrite(3,HIGH);
      digitalWrite(4,HIGH);
    }
    if ( millis() >= (time_zero+DrawTime)); {
      digitalWrite(1,0); //turn off motors
      digitalWrite(2,0);
      digitalWrite(3,0);
      digitalWrite(4,0);
    }
    if (millis() >= (time_zero+ServoTime));{
      Servo1.write(0);
      delay(400);
      Servo1.write(90);
    }
  }
}
if (inPin2 = HIGH);{
  int MAXampDrawTime = 75;
  int MAXampDrawSpeed = 59;
  int NORMampDrawTime = 225;
  int NORMampDrawSpeed = 20;
  int DrawTime = MAXampDrawTime+NORMampDrawTime;
  int ServoTime = 250;
  unsigned long previousMillis =0;
  unsigned long intervalHIamps = 100;
  unsigned long intervalNORMALamps = 400;
  DC1->setSpeed(MAXampDrawSpeed);
  DC2->setSpeed(MAXampDrawSpeed);
  DC3->setSpeed(MAXampDrawSpeed);
  DC4->setSpeed(MAXampDrawSpeed);

  unsigned long time_zero = millis();
  unsigned long current_time = millis();
  digitalWrite(1,HIGH);
  digitalWrite(2,HIGH);
  digitalWrite(3,HIGH);
  digitalWrite(4,HIGH);
  while((millis()-time_zero)<(500)){
  if( millis() >= (time_zero+MAXampDrawTime)){
    DC1->setSpeed(NORMampDrawSpeed);
    DC2->setSpeed(NORMampDrawSpeed);
    DC3->setSpeed(NORMampDrawSpeed);
    DC4->setSpeed(NORMampDrawSpeed);
    digitalWrite(1,HIGH);
    digitalWrite(2,HIGH);
    digitalWrite(3,HIGH);
    digitalWrite(4,HIGH);
  } else( millis() >= (time_zero+DrawTime)); {
    digitalWrite(1,0);
    digitalWrite(2,0);
    digitalWrite(3,0);
    digitalWrite(4,0);
  }
  if (millis() >= (time_zero+ServoTime));{
    int  time_check = millis();
    while (millis() - time_check < 400000) {
      inPin1 = digitalRead(A0);
      if (inPin1 == HIGH ) {
        Servo1.write(0);
        delay(400);
        Servo1.write(90);
      }
    }
  }
}
}}
  • If you exceed the limits of the equipment you run the risk of damaging it. Is it not possible to use an alternative power supply for the motor, and use the Arduino just as a controller. – Code Gorilla Nov 17 '15 at 3:30
1

Firstly your formatting is horrible, but that's probably just the this editor, if it isn't please fix it.

In loop() you are wasting time if you are re declaring constants, move them outside the loop.

Why have you got 4 on switches? Could you not gang them altogether? Join the wires and use just one pin?

you are repeating blocks of code, break these out into functions, this means less code to debug. I.e. SetMotorSpeed.

If (inPin2 = HIGH)

This will always be true, because an assignment is C\C++ always returns true, you meant ==

You TA is being paid to help you, don't be afraid to p##s them off, it's there job to get p'd off at students, and their choice. At least you are doing it by asking questions not d##king around. That said the most likely cause of you problems with his example is missing libraries or IDE version differences. Or even library versions, if you add the first 5 errors as a comment I might be able to point you in the right direction.

0

I'm going to echo what Matt said but you have way too many constants declared like variables.

A constant in C should be done like this

#define MYCONSTANT 666

right at the top of your code and outside of any setup loop or main function

Also think about the integer types you are using remember that Arduinos are 8 bit processors only use longs (32bit) where you really really have too and especially do not declare a constant as a long or int the way you have inside your loop.

This

int NORMampDrawSpeed = 61.2;

is just wrong you are saying that an int is a float, which it cannot be.

I personally avoid using floats altogether in Arduinos but if you must then declare it as a float or a constant (if it is a constant)

float NORMampDrawSpeed = 61.2;

or

#define NORMampDrawSpeed 61.2

be very careful when doing arithmetic with floats and ints because it will always return a float unless specifically typecast

better still since your new to programming just don't use them

And yes as Matt said write your own functions for stuff you need to do a lot such as

void setMotors( byte on_off ){
    for( uint8_t i=1; i <= 4; i++){
        digitalWrite(i,on_off);
    }
}

declare this function above the setup function an use it to turn all your motors on or off (1 or 0) like

setMotors(1);
  • Sorry but I have to disagree, constants should be done as static const int MyConstant = 666; And they should have the smallest scope possible, i.e. as close to the point of use as you can. But that's just style, so it doesn't matter which way Team One goes as long as she is consistent. – Code Gorilla Nov 17 '15 at 13:33
  • Well Ok what you call style is just wasteful IMO. Do it like that and it uses up 1 or more RAM locations which cannot be optimised use a define and the pre-processor inserts a value into the program memory which would have been taken anyway by your const. So do it that way and you lose RAM and program memory. – crowie Nov 17 '15 at 14:10
  • You don't have to take my word for it either see here link – crowie Nov 17 '15 at 14:23
  • No, your right, to much time spent on Window programming with unlimited resources. (I still like the look of my way better though :) ) – Code Gorilla Nov 17 '15 at 15:45
0

You reeeally don't want to draw current through your controller board. It's best to use a transistor power circuit, or 741 op-amp, or relay. Keep your power delivery separate from your control system.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.