Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 4

`//Includes the Arduino Stepper Library

#include <Stepper.h>

// Defines the number of steps per rotation


const int stepsPerRevolution = 2038;

// Creates an instance of stepper class


// Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence
Stepper myStepper = Stepper(stepsPerRevolution, 8, 10, 9, 11);

void setup() {
// Nothing to do (Stepper Library sets pins as outputs)
}

void loop() {
// Rotate CW slowly at 5 RPM
myStepper.setSpeed(5);
myStepper.step(stepsPerRevolution);
delay(1000);

// Rotate CCW quickly at 10 RPM


myStepper.setSpeed(10);
myStepper.step(-stepsPerRevolution);
delay(1000);
}

// using unipolar stepper motors 28BYJ-48 with ULN2003 driver breakout board
#include <Stepper.h> // include stepper library

const int stepsPerRevolution = 4076; // the number of steps per revolution for your motor

//initialize the stepper library on pins 4 through 1


Stepper myStepperH(stepsPerRevolution, 4, 5, 6, 7); //azimuth adjustment
Stepper myStepperV(stepsPerRevolution, 8, 9, 10, 11); //elevation adjustment
// LDR (Light Dependent Resistor) pin connections
// name = analog pin on respective arduino board;
int ldrtopright = 1; //LDR top right
int ldrtopleft = 0; //LDR top left
int ldrbottomleft = 2; //LDR bottom left
int ldrbottomright = 3; //LDR bottom right

int dtime = 25; //delay time in milliseconds


int tolerance = 100; //range of tolerance between LDR readings
int count = 0; //start millisecond count

void setup()
{
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
Serial.begin(9600); // initialize the serial port:
myStepperH.setSpeed(15);
myStepperV.setSpeed(15);
}
void loop()
{
//int count = millis(); //start millisecond count
int tr = analogRead(ldrtopright);
int tl = analogRead(ldrtopleft);
int bl = analogRead(ldrbottomleft);
int br = analogRead(ldrbottomright);

//print resistance values to serial monitor for debugging


Serial.println(tr);
Serial.println(tl);
Serial.println(bl);
Serial.println(br);
Serial.println();

count++;

int avt= (tr + tl) / 2; // average value top


int avd = (bl + br) / 2; // average value down
int avl = (tl + bl) / 2; // average value left
int avr = (tr + br) / 2; // average value right
int avgTot = (tr+tl+bl+br)/4;

int dvert = avt - avd; // check the difference of top and bottom LDRs
int dhoriz = avl - avr;// check the difference of left and right LDRs
if (-1*tolerance > dvert || dvert > tolerance) // check if the difference is in the tolerance else change
vertical angle
{
if (avt > avd) //if average resistance on top is greater than on bottom
{
myStepperV.step(stepsPerRevolution);
}
else //avg resistance on bottom is greater than on top
{
myStepperV.step(-stepsPerRevolution);
}
}
else if (-1*tolerance < dvert || dvert < tolerance) //if difference is smaller than tolerance, stop
vertical stepper
{
myStepperV.setSpeed(0);
}

if (-1*tolerance > dhoriz || dhoriz > tolerance) // check if the difference is in the tolerance else
change horizontal angle
{
if (avl > avr)
{
myStepperH.step(-stepsPerRevolution);
}
else
{
myStepperH.step(stepsPerRevolution);
}
}
else if (-1*tolerance < dhoriz || dhoriz < tolerance) //if difference in horizontal
{
myStepperH.setSpeed(0);
}
delay(dtime);
}
First, reformat your code using Ctrl-T in the IDE...it will be easier to read. Second,
replace these statements:

int avt= (tr + tl) / 2; // average value top


int avd = (bl + br) / 2; // average value down
int avl = (tl + bl) / 2; // average value left
int avr = (tr + br) / 2; // average value right
int avgTot = (tr+tl+bl+br)/4;

int dvert = avt - avd; // check the difference of top and bottom LDRs
int dhoriz = avl - avr;// check the difference of left and right LDRs

suspect a simpler approach would be something like this pseudo code

loop() {
// read LDRs
if (leftLDR > rightLDR) {
// move one step left // move towards brighter LDR
}
else if (rightLDR > leftLDR) {
// move one step right
}
}

...R

if a0 and a3 is 1
motor 2 is move left until a2 a3 is 1
then
if a2 a3 is 1
move motor 1 down until a0 a1 a2 a3 is 1

like this but this flow chart is very simple.

Hey econjack i tried ctrl-t but says no changes necessary for auto format. And i dont
know what am I expected for these values My friends send to these codes

You might also like