I have the LSM303 Accelerometer-Magnetometer http://www.adafruit.com/products/1247 connected to Arduino Mega via SCL/SDA lines.
Focusing on the magnetometer side, I want to use the angle between de sensor position and magnetic North (magnetic course), to control a 12 Volts DC motor CW or CCW using an "H-Bridge" circuit shown here:
As Arduino Mega has PWM pins 2-13 and 44-46 (all included), I only need 2 of them to control the "H-Bridge".
Now, at any time while LSM303 sensor is monitoring magnetic course at SDA/SCL, by pressing a single button to "hold" the value (I guess assigning a variable (let´s say "Sensitivity") for the stored value, any further change in course reading (after pressing the button) will be compared with the stored value and used to modulate the inputs at the BC182 transistors on the "H-Bridge"
Example: If stored value=120º and after that the sensor goes to +1º (121º reading) the BC182 transistor attached to a PWM-"A" Arduino output, will change from LOW to HIGH at 1/2" intervals . If difference goes to 2, the pulsed output goes to 1/4", an so on... until a difference reaches such value that the transistor senses an (almost) permant HIGH, thus applying permanent 12 volts to the Motor.
In other words, if I use the mechanical rotation to correct the the course error, the PWM signals will act slow for small error and fast if the error increases.
Anyway.. It would be a nice idea to establish different sets of PWM modulation.. Let´s say SLOW/MEDIUM/FAST to exànd posibilities, with a second button+led to set/modify the "sensitivity" for different hardware responses.
My guess, is that using PWM signals to control a DC motor by this method, the forces applied to mechanical robots will "smooth" their behaviour returnig to a defined position (stored course value) "quite, smooth, and easy"
And in the meantime... battery waste will be reduced (current peaks when motor receives full power witout any PWM control).
Well... I only have achieved (the LSM303 course values) at the Serial COM.
I don´t want to monitor (visually) any COM´s, displays, or other external Arduino gadgets. Just look to the motor shaft and when pressing the button, any angle change in the LSM303, will rotate the shaft CW or CCW until I position the sensor in the same exact position it was wen I pushed the button, without strong mechanical forces aplied to Motor as the LSM303 returns to the established (recorded angle) position.
As a non-too-self-sufficient-programmer, I´ve searched sketches and Forum´s ideas trying to find something usable, but I´m mostly "lost"....
¿Is it possible to put the above project in a single sketch?. Please... any guidance will be very much appreciated.
You will probably see that any other Magnetometer would do the same (instead of LSM303), but the idea is to use accelerometer values to indicate how mauch "tilt" should be applied if the components are NOT in the horizontal plane.
For this goal I think I will need some sort od LCD display..
But this last idea is reserved as a sencondary plan.
Thanks in advance.
That boars is for the FLORA, there are no level shifters on the board.
The Arduino Mega 2560 has a 5V I2C bus, and the LSM303 is a 3.3V chip.
You are feeding the LSM303 with 5V via the SDA and SCL, that's not good. Please use a I2C level shifter.
I really didn´t notice (my fault) the incompatibility of the voltage used on the SDA/SCL. I will research on that from now...
Up to this moment, the Flora circuit is working fine ... but no more risks.
Taking this oportunity
In your opinion:.... ¿Something wrong on the project? (without mention de SDA/SCL trouble)...
To be honest, I don't know what you want to make.
A boat that travels to a certain heading ? Like an autopilot ?
For an H-bridge, you better buy a module. That schematic freaks me out.
Look to your project from a distance. You have a sensor and buttons as input. The output is the motor.
Seperate the functionality into seperate blocks and write the code for each block and test it.
After that glue everything together.
Using the H-bridge will be a seperate block in your sketch. You can make a function that can drive the motors fast or slow. In your sketch, you just call that function.
Well.. That´s exactly what it is... I have a small sailboat 28 feet long and use to sail alone.
I have spent thousands euro (I live in Canary islands) in expensive (very expensive) tiller pilots with an unique result: 2/3 years in use or defective and when the warrany is about to expire.... repairs are almost as expesive as a new one. An the story keeps on and on...
The problem?: All autopilots are based en a H.Bridge calculated to control about 10 Amps, (even peaks of higher current..)
I´ve seen modules on the market with high current capabilities but very compact an some heating limitations. That´s why of my schematic which I´ve tested to be strong enough.
The other fact is the reason (in my opinion) the comercial autopilots always have trouble... The problem is that they use NO PWM control.
Normally is used a fluxgate sensor more or less sophisticated and circuits to compare manually inputs of desired course and "sesibility" (+/- X degrees) as limits for correction. But when the signal arrives to the motor and/or associated H-Bridge, they shake and vibrate as the current jumps to move rotors just in the first milliseconds.
These strong movements deform almost all mechanical components very easy. Old plastic or metallic housings are being replaced with other stuff that barely last more tah a couple of years.
I think that using PWM singals instead of the actual signal could reduce significantly the stress of components. That simple...
But I´m just a retired sailor who used to repair computers from the they were analog or even when they were ferrite/coil made memories... Something that hapened long... long... time ago.
You can make a smooth reaction in software, but there are other options.
For example using the multimap, that's an array of datapoints. You make those datapoints yourself and define yourself how the curve should be.
Or you can use a PID control. But I have my doubts about that. The PID control works with certain parameters, but I assume that wind and water current makes the boat behave different and the parameters are changing.