Please help me with the code to control a motor using arduino mega and megamoto

I know nothing about code or how to start but am eager to learn. I purchased an Arduino Mega 2560 R3, SainSmart sensor shield, and a Megamoto to control a large geared motor. I don't know if I need the sensor shield but I figured I needed somewhere to connect my end limit switches and start program switch. There are limit switches at each end, and I'd like to ramp up starting at 20% and taking 5 seconds to get to 100%. If possible id like the program to watch amp draw so it will reverse if gets in a bind. A simple momentary contact will start the program.

Someone please help me put this together but then explain what does what and why

Your idea is simple to do, but we are not going to write it for you. If you want someone to write the code for you, then you need go to Arduino's Gigs and Collaborations section and pay someone to do it.

However, if you are willing to learn some C based code and follow all the examples the Arduino software provided, then we can help you. But first, you need to try and write a code on your own, and show us that you willing to learn.

If your code does not work, but put some effort into it not just a mish mash of lines, we will look at it and tell you whats wrong and how to go about fixing it.

I will tell you this, you will need to learn about the analog*Write* function, and the corresponding PWM ( ~ ) pins, to get the motor to ramp up in speed. Another thing you will need to buy is a current sensor, like this one HERE. It should be fairly straight forward, Vcc, Gnd and Data. The Data line should be wired into one of the analog pins A0 - A5 (UNO) or A0 - A15 (MEGA), then with a simple analog*Read* of that pin, you should get values based on the current to the motor.

From there, its just IF / Else statements that check the limit switches and button.

Thank you for your suggestions and ideas. I truly have no idea of where to begin. I don't even know how to load the written program to the Arduino yet. I'm sorry if I made it sound as if I wanted someone to build the program for me. Some of you on here are very knowledgable and I wanted whomever that is offering ideas to know every aspect of what I have to work with or that what I'm seeking was even possible. I'm eager to learn but just don't know where to begin. However some of your attachments and ideas are giving more of a picture of what I need to do.

Should the arduino and mega moto have some basic instructions or program ideas with them? I haven't received them yet.

The megamoto [u]might[/u] come with an example or two, or might not. But that wont help you unless you learn how to upload sketches to the Arduino first.

I now have the arduino mega, megamoto (has load output pin), and the sensor shield (donn't think I need). I figured I'd at least get some type of directions on how to load or an example program or something. There's no paperwork at all so I still have no idea what to do. When I read directions or posts on here they don't speak in normal terms or explain what some terms and abbreviations mean. It's far more trickier finding info, ideas, and programing to helping me learn than I thought it would be.

Here is the manual HERE

Example sketch:

  MegaMoto Test Sketch
  Simply runs a motor back and forth
  ramping the speed from 0 to full (255)
  This example code is in the public domain.

int EnablePin = 8;
int duty;
int PWMPin = 11;  // Timer2
int PWMPin2 = 3;

const byte CPin = 0;  // analog input channel
int CRaw;      // raw A/D value
float CVal;    // adjusted Amps value

void setup() {                
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(EnablePin, OUTPUT);     
  pinMode(PWMPin, OUTPUT);
  pinMode(PWMPin2, OUTPUT);
  setPwmFrequency(PWMPin, 8);  // change Timer2 divisor to 8 gives 3.9kHz PWM freq

void loop() {
    // To drive the motor in H-bridge mode
    // the power chip inputs must be opposite polarity
    // and the Enable input must be HIGH
    digitalWrite(EnablePin, HIGH);
    analogWrite(PWMPin2, 0);
    for(duty = 0; duty <= 255; duty += 5){
      analogWrite(PWMPin, duty);
    analogWrite(PWMPin, 255);
    CRaw = analogRead(CPin);
    for(duty = 255; duty>=0; duty -= 5){
      analogWrite(PWMPin, duty);   
    analogWrite(PWMPin, 0);
    // Toggle enable to reset the power chips if we have had 
    // an overcurrent or overtemp fault
    digitalWrite(EnablePin, LOW);
    // Swap pins to make the motor reverse
    if(PWMPin == 11) {
      PWMPin = 3;
      PWMPin2 = 11;
    } else {
      PWMPin = 11;
      PWMPin2 = 3;
 * Divides a given PWM pin frequency by a divisor.
 * The resulting frequency is equal to the base frequency divided by
 * the given divisor:
 *   - Base frequencies:
 *      o The base frequency for pins 3, 9, 10, and 11 is 31250 Hz.
 *      o The base frequency for pins 5 and 6 is 62500 Hz.
 *   - Divisors:
 *      o The divisors available on pins 5, 6, 9 and 10 are: 1, 8, 64,
 *        256, and 1024.
 *      o The divisors available on pins 3 and 11 are: 1, 8, 32, 64,
 *        128, 256, and 1024.
 * PWM frequencies are tied together in pairs of pins. If one in a
 * pair is changed, the other is also changed to match:
 *   - Pins 5 and 6 are paired (Timer0)
 *   - Pins 9 and 10 are paired (Timer1)
 *   - Pins 3 and 11 are paired (Timer2)
 * Note that this function will have side effects on anything else
 * that uses timers:
 *   - Changes on pins 5, 6 may cause the delay() and
 *     millis() functions to stop working. Other timing-related
 *     functions may also be affected.
 *   - Changes on pins 9 or 10 will cause the Servo library to function
 *     incorrectly.
 * Thanks to macegr of the Arduino forums for his documentation of the
 * PWM frequency divisors. His post can be viewed at:
void setPwmFrequency(int pin, int divisor) {
  byte mode;
  if(pin == 5 || pin == 6 || pin == 9 || pin == 10) { // Timer0 or Timer1
    switch(divisor) {
      case 1: mode = 0x01; break;
      case 8: mode = 0x02; break;
      case 64: mode = 0x03; break;
      case 256: mode = 0x04; break;
      case 1024: mode = 0x05; break;
      default: return;
    if(pin == 5 || pin == 6) { 
      TCCR0B = TCCR0B & 0b11111000 | mode; // Timer0
    } else {
      TCCR1B = TCCR1B & 0b11111000 | mode; // Timer1
  } else if(pin == 3 || pin == 11) {
    switch(divisor) {
      case 1: mode = 0x01; break;
      case 8: mode = 0x02; break;
      case 32: mode = 0x03; break;
      case 64: mode = 0x04; break;
      case 128: mode = 0x05; break;
      case 256: mode = 0x06; break;
      case 1024: mode = 0x7; break;
      default: return;
    TCCR2B = TCCR2B & 0b11111000 | mode; // Timer2

Ok first let me say WOW, and Thank you "Hazardsmind" for your help! I've been reading and reading and reading and learning. I finally figured out how to attach the arduino and megamoto together (using the correct pins). At first I didn't see the pins would line up and go through to the next level/shield. Then found out how to load the sketch. Loaded the motor controller sketch you forwarded and watched a small motor ramp up, and run, then off, and do it again the other way. I got the arduino bug and had to load sketches that use switches to control led's and servos.

I haven't found any code sketch examples that use the load monitoring pin on the megamoto yet?

Your welcome. I took another look at the link to the manual I gave and if you look on pages 4 & 5, it tells you all about the current sensor and how to use it. Figures 3 and 4 show you the jumper configurations.

Now the only thing I don't know nor did I read anything in the manual, is if you need a certain formula to calculate the current. Being that no such formula was provided, leads me to think the output from the pin will be in milliamps already.

So the highest value the analog pin can read is 1023, so this would mean that the highest reading you can get is 1.023 Amps or 1023 milliamps. Give it a try, I'm sure you know how to read an analog pin, right?

I've read that manual so many times I think I could recite it back at a will! So because of a 634 ohm resister (on each load pin) of the H bridge it is half the reading. Which is ok because I'm just glad there's something to read. Although I don't know how to write in a monitored value into a sketch yet. answer your question, I do not know how to read an analog pin (yet). I could use a multimeter to read the load pin but I know that's not what you mean. Could you tell me how to write code to read the analog pin?

AnalogRead AnalogWrite