how to code for an accelerometer and a ESC

Would anyone be able to help with the coding if I were to use the IR remote to start the EDF rather than the accelerometer? I'd still want to have the EDF turn off automatically with a variable timer

If no one here knows how to code, then I must be in the wrong place.

This is certainly the wrong place for someone with your attitude.

If you don't want to learn the way most people do, by starting at the beginning, and don't want to follow the directions on how to use this forum, post on the Gigs and Collaborations section of the forum, and be prepared to pay for the help.

Good luck with your project!

jremington:
This is certainly the wrong place for someone with your attitude.

If you don't want to learn the way most people do, by starting at the beginning, and don't want to follow the directions on how to use this forum, post on the Gigs and Collaborations section of the forum, and be prepared to pay for the help.

Good luck with your project!

Well it sure doesn't look like anyone is trying to help me from the beginning. I stated earlier, imagine explaining this to a 12 year old. Other than the math explained by one or two, there has been no useful info gained from this site.

I'm really not trying to be an a**hole. I simply do not understand anything about coding and simply asking for someone to help walk me through the process step by step

Starting from zero coding experience, it’s simply unrealistic to expect to be “walked through” doing a project of even this modest complexity. I know you don’t mean it, but you’re essentially asking someone to do it for you.

If you want to do this yourself (with help from the forum), you need to start at the very beginning. The Arduino IDE has many built-in examples that help you learn coding from the ground up. That’s the only way to do it. Otherwise, you will have to get someone to do it for you, especially if you want it done by this weekend.

Problem is the derby is this weekend and I'm having trouble learning how to code that quickly.

At this point your options are 1) find someone who understands this stuff to do it for you or 2) forget it.

Next time, start new projects earlier.

It's important to know things about actuators and sensors because they often have special requirements and impact firmware. An ESC, for example, may require a calibration procedure on start-up. Some are uni-directional, others bi-directional and they have different control requirements. Accelerometers have different interface requirements, different start-up requirements etc.

Here's what I wrote last night. It only samples the ADXL345 accelerometer I had on hand and provides a basic but untested scaffold for "an" ESC that may or may not be bidir and which may or may not require calibration. I was playing the idea of the accelerometer sensing "free fall" and trigging a flag to fire the EDF but ran out of time to develop it. You need to experiment with the whole thing, determine what the sensor does the moment the car is released and act on that. But not before...

#include <SparkFun_ADXL345.h>
#include <Servo.h>
#include <Wire.h>

const byte pinLED = LED_BUILTIN;
const byte pinESC = 9;
const byte pinAccelIRQ = 2;

#define ESC_MAX             179
#define ESC_CENTER          90
#define ESC_MIN             0
#define ESC_ZERO            0
#define ESC_POST_CAL_DLY    2500
#define NUM_CAL_STEPS       2
typedef struct
{
    byte            CalValue;
    unsigned long   CalDuration;
    
}StructESCCalibration;

//this assumes ESC is fwd only
//may need to add a step if ESC is fwd/rev with MAX (179), MIN (0) and CENTER (90)
StructESCCalibration ESCCalibration[] =
{
    {
        //full
        .CalValue = ESC_MAX,
        .CalDuration = 3000
    },
    {
        //min
        .CalValue = ESC_MIN,
        .CalDuration = 3000
    }
        
};
    
Servo 
    edf_esc;

ADXL345 
    accelerometer = ADXL345();
bool
    bEDFEnable,
    bFreeFall;
    
void setup()
{
    pinMode( pinLED, OUTPUT );
    pinMode( pinESC, OUTPUT );
    pinMode( pinAccelIRQ, INPUT );
    Serial.begin(115200);
    bFreeFall = false;          //flag set in ISR when free-fall detected
    bEDFEnable = true;          //ensures impulse occurs once
    InitializeAccelerometer();  //set up the ADXL345
    
    edf_esc.attach( pinESC );   //set up the ESC

    //calibration required?
    //comment out if not needed
    //CalibrateESC();             //calibration may be required; eval required
    
}//setup

//accelerometer data is 13bit
//    0bfedc ba98 7654 3210
//    0bxxx1 1111 1111 1111

void loop()
{
    double
        fAccels[3];
    double
        resultant;
    unsigned int 
        x,y,z;   
#if 0
    accelerometer.readAccel(&x, &y, &z);
    x>>=3;
    y>>=3;
    z>>=3;
    Serial.print("X "); Serial.println(x,HEX); 
    Serial.print("Y "); Serial.println(y,HEX); 
    Serial.print("Z "); Serial.println(z,HEX); 
    Serial.println();

    accelerometer.get_Gxyz( fAccels );
    Serial.print("X "); Serial.println(fAccels[0],2); 
    Serial.print("Y "); Serial.println(fAccels[1],2); 
    Serial.print("Z "); Serial.println(fAccels[2],2); 
    Serial.println();
    resultant = sqrt( pow(fAccels[0],2) + pow(fAccels[1],2) + pow(fAccels[2],2) );
    Serial.print("Resultant "); Serial.println(resultant,3); 
    Serial.println();
    
#endif 

    accelerometer.get_Gxyz( fAccels );
    resultant = sqrt( pow(fAccels[0],2) + pow(fAccels[1],2) + pow(fAccels[2],2) );
    Serial.print(resultant);Serial.print(",");
    Serial.print(fAccels[0]);Serial.print(",");
    Serial.print(fAccels[1]);Serial.print(",");
    Serial.println(fAccels[2]);
     
    //handlerAccelIRQ();    
    //if EDF is enabled and free-fall has been detected
    if( bEDFEnable && bFreeFall )
    {
        //hit the motor for a second
        EDFImpulse();
        //then clear the enable and ISR flags
        bEDFEnable = false;
        bFreeFall = false;
         
    }//if
    
    delay(10);
    
}//loop

void EDFImpulse( void )
{
    digitalWrite( pinLED, HIGH );
    edf_esc.write( ESC_MAX );
    delay(1000);
    edf_esc.write( ESC_MIN );
    digitalWrite( pinLED, LOW );
    
}//EDFImpulse

void CalibrateESC( void )
{
    //if ESC calibration is required, this might work
    //may not be necessary if ESC can be programmed with calibration prior to use here
    for( int i=0; i<NUM_CAL_STEPS; i++ )
    {
        edf_esc.write( ESCCalibration[i].CalValue );
        delay( ESCCalibration[i].CalDuration );
        
    }//for

    edf_esc.write( ESC_ZERO );
    delay( ESC_POST_CAL_DLY ); 
        
}//CalibrateESC

void InitializeAccelerometer( void )
{
    accelerometer.powerOn();
    accelerometer.setRangeSetting(2);
    //
    accelerometer.setActivityXYZ(1, 1, 1);       // Set to activate movement detection in the axes "adxl.setActivityXYZ(X, Y, Z);" (1 == ON, 0 == OFF)
    accelerometer.setActivityThreshold(75);      // 62.5mg per increment   // Set activity   // Inactivity thresholds (0-255)
 
    accelerometer.setInactivityXYZ(0, 0, 0);
    accelerometer.setTapDetectionOnXYZ(0, 0, 0);
 
    // Set values for what is considered FREE FALL (0-255)
    accelerometer.setFreeFallThreshold(5);       // (5 - 9) recommended - 62.5mg per increment
    accelerometer.setFreeFallDuration(30);       // (20 - 70) recommended - 5ms per increment
   
    // Turn on Interrupts for each mode (1 == ON, 0 == OFF)
    accelerometer.InactivityINT(0);
    accelerometer.ActivityINT(0);       //may need to try this one too.
    accelerometer.FreeFallINT(1);
    accelerometer.doubleTapINT(0);
    accelerometer.singleTapINT(0);
  
    attachInterrupt( digitalPinToInterrupt(pinAccelIRQ), handlerAccelIRQ, RISING );   // Attach Interrupt    
    
}//InitializeAccelerometer

//ISR for ADXL345. All we look for is the free-fall detect
void handlerAccelIRQ( void )
{
    byte 
        intSrc;
    
    //get the source
    intSrc = accelerometer.getInterruptSource();
      
    //if free-fall, set the flag for loop
    if( accelerometer.triggered( intSrc, ADXL345_FREE_FALL ) )
    {
        Serial.println("Got here" );
        bFreeFall = true;
    }
        
    
}//handlerAccelIRQ

gfvalvo:
Starting from zero coding experience, it’s simply unrealistic to expect to be “walked through” doing a project of even this modest complexity. I know you don’t mean it, but you’re essentially asking someone to do it for you.

If you want to do this yourself (with help from the forum), you need to start at the very beginning. The Arduino IDE has many built-in examples that help you learn coding from the ground up. That’s the only way to do it. Otherwise, you will have to get someone to do it for you, especially if you want it done by this weekend.

I have gone through a lot of the examples and youtube videos. Things have just not clicked for me. I don't know why it's not clicking for me.

yes I know I'm asking for someone to basically do it for me, I am asking for step by step instructions. I just thought someone on here would help me.

Step 1 - load up the Blink tutorial. Push it to your arduino. See the LED blink.

This confirms that you are able to push up a sketch and see the results.

Step 2 - change the blink tutorial code so that the LED blinks at a different rate. Push it to your arduino and see the different blink rate.

This confirms that you can change code successfully.

Steps 3 through 37 - try out other tutorials

Step 38 - change the sample code that came with your accelerometer, so that it records the various readings on to a memory card. Then put the project on to a derby car and make several runs, letting the arduino record the vibrations/g-force/freefall. Later, download that data on to a PC and see if you can identify which readings indicate the start of the race.

This will let you use those conditions as the trigger for your ESC run.

Edit: I suspect that a switch at the nose that detects when it is against the starting pin will be a much more repeatable trigger condition.