Hello,
Find below my code :
#include <AccelStepper.h>
// Define some steppers and the pins the will use
//AccelStepper stepper1(8, 8, 9, 10, 11);
//AccelStepper stepper1(8, 7, 8, 9, 10);
AccelStepper stepper1(8, 4, 5, 6, 7);
int value = 0;
int Signal = A0;
long int pos = 0;
int previous = 0;
//int angle_deviation = 3072; // Angle de déviation = angle*4096/360
int homeButton_A1 = A1;
//byte hBval;
int hBval;
float x = 0;
int i = 0;
void setup()
{
pinMode (Signal,INPUT); // Analog Input signal (0 to 5V)
pinMode (homeButton_A1,INPUT); // Analog Input for home position
pinMode(13, OUTPUT);
stepper1.setMaxSpeed(2000.0); // Max speed of the first motor - modify if you want to
stepper1.setAcceleration(2000.0); // Rate at which the first motor accelerate -
motorHome(); // Do only one time to find the 0 position (switch or photoresistor mode)
stepper1.setCurrentPosition(0); // Initialize the step counter to 0
}
void loop()
{
value = analogRead(Signal);
// pos = (value*2390)/1024L;
// x = 2.5;
x = 2795.0/1024.0; // 2795 pour 240° de déviation
pos = (value*x);
if (stepper1.distanceToGo() == 0)
{
stepper1.moveTo(float(pos));
delayMicroseconds(0.5); // 500 µs
}
stepper1.run();
}
void motorHome()
{
hBval = analogRead(homeButton_A1);
while (hBval <= 950)
{
if (stepper1.distanceToGo() == 0)
{
stepper1.move(-2795);
hBval = analogRead(homeButton_A1);
}
stepper1.run();
}
stepper1.setCurrentPosition(0); // Initialize the step counter to 0
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
}
For the ATMEGA328P.
Summary of my connection :
Pin 7 and 20 : 5V
Pin 8 and 22 : GND
Pin 1 : To 5V through 10K resistor
I connected my motor driver to :
pin 6, pin 11, pin 12 and pin 13
And I connected my two analog inputs to pin 23 and 24.
When I Give 5V to the µC with the USB2SERIAL board it works well, but nothing happen with an external power supply.
My power supply is an Agilent E3631A and I can put 5V/5A current limit.
The consumption is arround 300mA maximum for my motor
I have no idea wh it doesn't work with external supply.
Maybe the problem comes from the bootloader.
For information to loard my code I used the USB2SERIAL board. Parameters on arduino IDE :
Board : Arduino UNO
Programmer : AVR ISP