Created
January 25, 2018 11:11
-
-
Save azhaganandhan/ea494fabe496e2f29dd269d8c2dfb457 to your computer and use it in GitHub Desktop.
Working stepper motor with photodetector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int Horizontal = 0; // set to home position | |
#include <Stepper.h> | |
const int stepsPerRevolution = 512; | |
Stepper Vstepper(stepsPerRevolution, 8, 9, 10, 11); | |
Stepper Ystepper(stepsPerRevolution, 1, 2, 3, 4); | |
// LDR | |
// name = analogpin; | |
int topleft= A3; // (red) | |
int bottomleft = A1; // (Blue) | |
int topright = A0; // (Green) | |
int bottomright= A2; // (Yellow) | |
void setup() | |
{ | |
Serial.begin(9600); //initialize serial port | |
pinMode(8, OUTPUT); | |
pinMode(9, OUTPUT); | |
pinMode(10, OUTPUT); | |
pinMode(11, OUTPUT); | |
pinMode(1, OUTPUT); | |
pinMode(2, OUTPUT); | |
pinMode(3, OUTPUT); | |
pinMode(4, OUTPUT); | |
Vstepper.setSpeed(1200); // setting the speed of stepper | |
Ystepper.setSpeed(1200); // setting the speed of stepper | |
} | |
void loop() | |
{ | |
int tl = analogRead(topleft); | |
int tr = analogRead(topright); | |
int bl = analogRead(bottomleft); | |
int br = analogRead(bottomright); | |
int delayt = 25; | |
int sensitivity = 100; | |
int avgl = (tl + bl) / 2; | |
int avgr = (tr + br) / 2; | |
int avgt = (tl + tr) / 2; | |
int avgb = (bl + br) / 2; | |
int diffv = avgl - avgr; //diff between left n right | |
int diffh = avgt - avgb; //diff between top n bottom | |
if (-1* sensitivity > diffv || diffv > sensitivity) // check for difference in sensitivity | |
{ | |
if (avgl > avgr) // if average left is greater than average right | |
{ | |
Vstepper.step(stepsPerRevolution); | |
Ystepper.step(-stepsPerRevolution); | |
} | |
else // if the average left is smaller than average right | |
{ | |
Vstepper.step(-stepsPerRevolution); | |
Ystepper.step(stepsPerRevolution); | |
} | |
} | |
else if (-1* sensitivity < diffv || diffv < sensitivity) | |
{ | |
Vstepper.step(0); | |
Ystepper.step(0); | |
} | |
if (-1*sensitivity > diffh || diffh > sensitivity) | |
{ | |
if (avgt > avgb) | |
{ | |
Horizontal = -- Horizontal; | |
if (Horizontal < 0) | |
{ | |
Horizontal = 0; | |
} | |
} | |
else if (avgt < avgb) | |
{ | |
Horizontal = ++ Horizontal; | |
if (Horizontal > 180) | |
{ | |
Horizontal = 180; | |
} | |
} | |
else if (avgt == avgb) | |
{ | |
//nothing | |
} | |
} | |
delay(delayt); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment