I am making an rotary platform for my product photoshoot. i want to take 360' picture of product which i would later use with 123D catch by autodesk to make an 3d model which i would later use for editing in Cinema4d and finally use it in game development in Unity3D.
So i have arduino mega 2560 and L298 motor driver along with Photo Intrrupter. The rotary disk have markings on its disk where the photo Intruppter sensor would be used to locate its location. check this picture here .
the code i wrote for to control the motor using the L298 Moto Driver is as below.
/*
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the serial monitor.
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
int D2 = 4;
int P2 = 5;
int x;
// the setup routine runs once when you press reset:
void setup() {
pinMode(D2, OUTPUT);
digitalWrite(D2,LOW);
Serial.begin(9600); // initialize serial communication at 9600 bits per second:
}
void loop()
{
int sensorValue = analogRead(A0); // my photosensor detects upon open 500-530 reading and upon close 2 - 25 reading in serial monitor
if (sensorValue < 30)
{
x = 0;
}
else
{
x = 1;
}
if (x == 1)
{
RunMotor();
}
else
{
StopMotor();
}
}
void RunMotor()
{
analogWrite(P2,255);
Serial.println("starting");
}
void StopMotor()
{
analogWrite(P2,0);
Serial.println("Stopping");
}
I would like to have your suggetion on following.
1) is the script efficient enough ?
2) does the motor would get clean command from the arduino to go ahead as per script and not like looping decision every microseconds cause i dont want the motor to behave on-Off situation every microseconds which i believe can take away the power from motor. i want one clean command for the motors to go ahead and stop not looping type, how can achieve that. can i avoid voidLoop alltogether and simply write if statement ?
You declare D2 and P2 as ints. You set D2 as an output, then you never use it in your program. You make use of P2 as an output, but, never set it as such.
Your program will move the motor until A0 is < 30. At that point, the program will loop, but, nothing will be specifically done other than telling the motor to stop. You will see a string of Stopping in the console window once A0 reads < 30.
You have to loop through the program so the motor will continue moving until it reaches your specified end point.
/ AnalogReadSerial Reads an analog input on pin 0, prints the result to the serial monitor. Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground. /
int D2 =5;
// the setup routine runs once when you press reset: voidsetup() {
pinMode(D2, OUTPUT);
digitalWrite(D2,LOW);
Serial.begin(9600); // initialize serial communication at 9600 bps
}
voidloop() { // my photosensor detects upon open 500-530 reading and // upon close 2 - 25 reading in serial monitor int sensorValue = analogRead(A0); if (sensorValue <30) {
moveMotor(0);
} else {
moveMotor(255);
}
}
Hi Birdmun i tried your code. the PWM light on the L298 moto driver is on but the motor is not running. when i change the D2 in your moveMotor Function the P2 then the motor is rotating fine.
i made little changes upon which when the photosensor comes on those 4gaps it stops for 10sec then there is little kick start for 1sec which make the photosensor away form those holes hence it moves forward untill another gap comes in.
but i want to find other solution for this kick start after 10sec delay as i need find way to tell motor to move ahead after waiting for 10sec upon it reaches the gap. as of now the code is working as expected but i think it is not efficient enough.
int Switch =2; int SwitchButtonState; int D1 =4; int P1 =5; int lightSensor =3; int LightSensorState; int LedPin =13;
voidsetup () {
Serial.begin(9600);
pinMode(P1, OUTPUT); // pwm output
pinMode(D1, OUTPUT); // direction output
digitalWrite(D1,LOW); // motor will always rotate in one direction
}
if (LightSensorState == LOW) {
MoveMotor(0);
delay (10000);
MoveMotor(255);
delay(1000); // I don’t think this delay is required.
} else {
MoveMotor(255);
}
}
voidMoveMotor(int spd) {
analogWrite(P1,spd);
}
The major mistake I made in my earlier code is not setting P1/P2 as OUTPUT in setup(). I made the same mistake when I started coding for my tadpole. OUTPUTs must be specifically specified. All pins start as INPUTs. That is why the code worked when you switched the connections from P2 to D2 on the first go 'round.
I realize my code is almost devoid of comments. Bad on me. I got rid of any code you left in that wasn’t doing anything. I removed your remarked code. As far as it goes, your lightSensorState decalaration could be moved in to the loop() function. It does not need to be a global variable. You only use it inside the loop() function.
this use to be automatic food feeder for dog which was borken long time back so i use it for making rotary platform. It has IR BeamSensor and switch to figure the position of the feeder. which i am using with mega2560 to control its precise movement for 360 photo cature purpose to put on my ebay shop.