Rotary Platform

Hi All,

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 ?

thanks for suggestion.

Your code is a bit broken.

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.

My shot at it looks like:

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/
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:
void setup() {
pinMode(D2, OUTPUT);
digitalWrite(D2,LOW);
Serial.begin(9600); // initialize serial communication at 9600 bps
}

void loop() {
// 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);
}
}

void moveMotor(int spd) {
String messageString;
analogWrite(D2,spd);
if (spd > 0) {
messageString = “Starting”;
} else {
messageString = “Stopping”;
}
Serial.println(messageString);
}

how can i achieve the below function in comment "// "

 

int Switch = 2;

int SwitchButtonState;

int D2 = 4;

int P2 = 5;

int lightSensor = 3;

int LightButtonState;

int LedPin = 13;


void setup ()

{

  Serial.begin(9600);

  pinMode(D2, OUTPUT);

  digitalWrite(D2,LOW);

  pinMode(LedPin,OUTPUT);

  pinMode (Switch,INPUT);

  pinMode (lightSensor,INPUT);

}


void loop ()

{

  SwitchButtonState = digitalRead(Switch);

  LightButtonState = digitalRead(lightSensor);


  Serial.print(SwitchButtonState);

  Serial.println(LightButtonState);


  if (SwitchButtonState == HIGH && LightButtonState == HIGH)

  {

// need runing motor pause for 3 seconds and then rotate 

  }

  else if (SwitchButtonState == LOW && LightButtonState == HIGH)

{

// keep the motor running 

}

  else if (SwitchButtonState == LOW && LightButtonState == LOW)

{

//need runing motor pause for 3 seconds and then rotate 

}

}


void MoveMotor(int spd)

{

  analogWrite(P2,spd);

}


 

The motor is not rotating.

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.

Next run at it.

I cleaned your code up again.

 

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#define Switch 2
#define lightSensor 3
#define D2 4
#define P2 5
int SwitchButtonState;
int LightSensorState;

void setup () {
Serial.begin(9600);
pinMode(D2, OUTPUT);
digitalWrite(D2,LOW);
}

void loop () {
SwitchButtonState = digitalRead(Switch);
LightSensorState = digitalRead(lightSensor);

Serial.print(SwitchButtonState);
Serial.println(LightSensorState);

if (SwitchButtonState == LOW && LightSensorState == HIGH) {
MoveMotor(255);
} else {
delay(3000);
MoveMotor(255);
}
}

void MoveMotor(int spd) {
analogWrite(P2,spd);
}

**little change **

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;

void setup ()
{
Serial.begin(9600);
pinMode(D1, OUTPUT);
digitalWrite(D1,LOW);
pinMode(LedPin,OUTPUT);
pinMode (Switch,INPUT);
pinMode (lightSensor,INPUT);
}

void loop ()
{
SwitchButtonState = digitalRead(Switch);
LightSensorState = digitalRead(lightSensor);

Serial.print(SwitchButtonState);
Serial.println(LightSensorState);

// if (SwitchButtonState == HIGH && LightSensorState == HIGH)
// {
// MoveMotor(255);
//
// }
// else if(SwitchButtonState == HIGH && LightSensorState == LOW)
// {
//
//
// }
// else

if (LightSensorState == LOW)
{
MoveMotor(0);

  delay (<span style="color: #0000dd; font-weight: bold;">10000</span>);
  MoveMotor(<span style="color: #0000dd; font-weight: bold;">255</span>);
  delay(<span style="color: #0000dd; font-weight: bold;">1000</span>);
  

}

else
{
MoveMotor(255);
//delay(10000);

   <span style="color: #888888;">//LightSensorState = HIGH;</span>

}

}

void MoveMotor(int spd)
{
analogWrite(P1,spd);
}

I made some dumb mistakes in my most recent posted code.

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#define lightSensor 3
#define D1 4
#define P1 5
int LightSensorState;

void setup () {
Serial.begin(9600);
pinMode(P1, OUTPUT); // pwm output
pinMode(D1, OUTPUT); // direction output
digitalWrite(D1,LOW); // motor will always rotate in one direction
}

void loop () {
LightSensorState = digitalRead(lightSensor);
Serial.println(LightSensorState);

if (LightSensorState == LOW) {
MoveMotor(0);
delay (10000);
MoveMotor(255);
delay(1000); // I don’t think this delay is required.
} else {
MoveMotor(255);
}
}

void MoveMotor(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.

Works great

Well the code u mentioned above work just fine. Thanks for it. 

I’m curious where you

I’m curious where you salvaged the black base in your picture from (with the gears)?  I’d like to make something like this myself :slight_smile:

it was from:

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.