Pages: [1]   Go Down
Author Topic: Solar Tracking Code  (Read 2597 times)
0 Members and 1 Guest are viewing this topic.
WV
Offline Offline
Full Member
***
Karma: 0
Posts: 160
Arduino, Helps With the ADD
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Since of course I'm New to the increasingly exciting World of Arduino, I have been messing with my uno for several days learning Coding.
I have done , Probably what others have already , but couldn't find a complete setup. So i have created a ( Currently easly changed to 2) single axis Stepper Driven VIA recycled Transistor and a Printer motor. Running 2 Photoresistors,for direction of course .With many trials an errors, I have ( I think) all of the coding set where it needs to be. I know there have be other possable uses of GPS /Time tracking systems,but this one was more fun.
Have even set low light threshhold and travel limit switch. (1 currently)
Lets see what others think smiley  ....
Anyone think it would be a goood idea to include a return to original rotation position from the limit switch , or do you think it would cause more headache than good smiley ?

Code:
/*
Thomas Wood
PhotoCell Controlled Stepper Motor Left and Right
Feb 15,2012
 */

#include <Stepper.h>

const int stepsPerRevolution = 48;  // Turns of the motor
   int inputPhotoLeft = 0; // photresistors pin
 int inputPhotoRight = 1; // photresistors pin

int Left = 0; // Store readings from the photoresistors.
 int Right = 0; // Store readings from the photoresistors.

int threshhold = 550; // this is here from reading the serial monitors valued to get a ruff Idea of value to set A
                      //And how dark you Want your motor to stop moving at

 int switchOff = 12; // adds a Rotation Limit Switch
 int switchState = 0 ; // Sets the limits Switches State
 


// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8,9,10,11);


void setup() {
  // Motor RPMs I Didn't want it to jerk on start
  myStepper.setSpeed(20);
  // initialize the serial port:
  Serial.begin(9600);
   pinMode(switchOff, INPUT);
}

void loop() {
  Left = analogRead(inputPhotoLeft); // sets the photcells position left - right
 Right = analogRead(inputPhotoRight);
 
 
switchState = digitalRead(switchOff); // gets the reading from the limit switch

if ((Left < threshhold) && (Right < threshhold))
{
  // this is to limit the after dark operation - no need in a plane,train,street light or automobil to make the panel move :)
  Serial.println("Lower threshhold Reached - hopefully we have stopped");
  Serial.print("Left Sensor \t");
  Serial.print(Left);
  Serial.print("   Right Sensor \t");
  Serial.println(Right);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
delay(500);

}



 // the +20 is to reduce possable jitter between photcell readings
  if ((Left > threshhold) > (Right +20 >threshhold))
{
  // runs the motor a 1/4 turn per instance
  
   Serial.print("clockwise \t");
   Serial.println(Left);
  
  myStepper.step(-stepsPerRevolution/4);
  delay(500);
  
}
  
   if  ((Left +20 > threshhold) <  (Right > threshhold))
{
   // runs the motor a 1/4 turn per instance
  Serial.print("counterclockwise \t");
  Serial.println(Right);
  
  myStepper.step(stepsPerRevolution/4);
  delay(500);

}
// checking the limit switch to see if its engaged then move the motor back for 1 second
if (switchState == HIGH)
{
  myStepper.step(stepsPerRevolution);
Serial.println("Limit Switch Engaged And Moving Motor In Reverse");
delay(1000);

}
// this line is only realy here to see the state of the switch
// - not realy currently to do anything
if (switchState == LOW)
{

 // Serial.println("Switch Is Off");
  delay(1000);
}

{
  // these are here to disengage the coils
//as not to hold and in turn over heat the motor
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
}
}
« Last Edit: February 16, 2012, 06:14:41 am by woody_unreal » Logged

Leighton Buzzard, UK
Offline Offline
Edison Member
*
Karma: 21
Posts: 1339
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

if you wrap code tags around it, makes it sooo much easier to read!
Code:

/*
Thomas Wood
PhotoCell Controlled Stepper Motor Left and Right
Feb 15,2012
 */

#include <Stepper.h>

const int stepsPerRevolution = 48;  // Turns of the motor
   int inputPhotoLeft = 0; // photresistors pin
 int inputPhotoRight = 1; // photresistors pin

int Left = 0; // Store readings from the photoresistors.
 int Right = 0; // Store readings from the photoresistors.

int threshhold = 550; // this is here from reading the serial monitors valued to get a ruff Idea of value to set A
                      //And how dark you Want your motor to stop moving at

 int switchOff = 12; // adds a Rotation Limit Switch
 int switchState = 0 ; // Sets the limits Switches State
 


// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8,9,10,11);


void setup() {
  // Motor RPMs I Didn't want it to jerk on start
  myStepper.setSpeed(20);
  // initialize the serial port:
  Serial.begin(9600);
   pinMode(switchOff, INPUT);
}

void loop() {
  Left = analogRead(inputPhotoLeft); // sets the photcells position left - right
 Right = analogRead(inputPhotoRight);
 
 
switchState = digitalRead(switchOff); // gets the reading from the limit switch

if ((Left < threshhold) && (Right < threshhold))
{
  // this is to limit the after dark operation - no need in a plane,train,street light or automobil to make the panel move smiley
  Serial.println("Lower threshhold Reached - hopefully we have stopped");
  Serial.print("Left Sensor \t");
  Serial.print(Left);
  Serial.print("   Right Sensor \t");
  Serial.println(Right);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
delay(500);

}



 // the +20 is to reduce possable jitter between photcell readings
  if ((Left > threshhold) > (Right +20 >threshhold))
{
  // runs the motor a 1/4 turn per instance
  
   Serial.print("clockwise \t");
   Serial.println(Left);
  
  myStepper.step(-stepsPerRevolution/4);
  delay(500);
  
}
  
   if  ((Left +20 > threshhold) <  (Right > threshhold))
{
   // runs the motor a 1/4 turn per instance
  Serial.print("counterclockwise \t");
  Serial.println(Right);
  
  myStepper.step(stepsPerRevolution/4);
  delay(500);

}
// checking the limit switch to see if its engaged then move the motor back for 1 second
if (switchState == HIGH)
{
  myStepper.step(stepsPerRevolution);
Serial.println("Limit Switch Engaged And Moving Motor In Reverse");
delay(1000);

}
// this line is only realy here to see the state of the switch
// - not realy currently to do anything
if (switchState == LOW)
{

 // Serial.println("Switch Is Off");
  delay(1000);
}

{
  // these are here to disengage the coils
//as not to hold and in turn over heat the motor
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
}
}

looks ok
does it work how you wanted it to?
« Last Edit: February 16, 2012, 06:07:18 am by mmcp42 » Logged

there are only 10 types of people
them that understands binary
and them that doesn't

WV
Offline Offline
Full Member
***
Karma: 0
Posts: 160
Arduino, Helps With the ADD
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes all i have to do is run it on a Large Turn table, to call it complete smiley
Logged

WV
Offline Offline
Full Member
***
Karma: 0
Posts: 160
Arduino, Helps With the ADD
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok Have redone the coding  the othere has some real world bugs, To much light wouldnt let it work, So here it is for everyone to use. Hope it fits your bill smiley
Code:

/*
Thomas Wood
PhotoCell Controlled Stepper Motor Left and Right
Feb 15,2012
 */

#include <Stepper.h>

const int stepsPerRevolution = 200;  // Turns of the motor
int inputPhotoLeft = 0; // photresistors pin
int inputPhotoRight = 1; // photresistors pin
int Left = 0; // Store readings from the photoresistors.
int Right = 0; // Store readings from the photoresistors.
const int threshhold = 300; // this is here from reading the serial monitors valued to get a ruff Idea of value to set A
                      //And how dark you Want your motor to stop moving at
int difSence = 15; // differenace tolarance between sensors
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8,9,10,11);
void setup() {
   myStepper.setSpeed(150);
   //Serial.begin(9600);
    pinMode(13, OUTPUT);
}
void loop() {
 Left = analogRead(inputPhotoLeft); // sets the photcells position left - right
 Right = analogRead(inputPhotoRight);
digitalWrite(13,LOW); //led doesnt need to be on to start with
if  (((Left > threshhold) || (Right > threshhold)) && (Left > Right + difSence))
{
  // Serial.print("clockwise \t");
 // Serial.println(Left);
    myStepper.step(-stepsPerRevolution);
    digitalWrite(13,HIGH);
   }
if  (((Right > threshhold) || (Left > threshhold)) && (Right > Left + difSence))
{
 // Serial.print("counterclockwise \t");
 // Serial.println(Right);
   myStepper.step(stepsPerRevolution);
   digitalWrite(13,HIGH);
}
if ((Left < threshhold) && (Right < threshhold))
{
  /*
  // this is to limit the after dark operation -
  //no need in a plane,train,street light or automobil to make the panel move :)
  Serial.println("Lower threshhold Reached - hopefully we have stopped");
  Serial.print("Left Sensor \t");
  Serial.print(Left);
  Serial.print("   Right Sensor \t");
  Serial.println(Right);
  */
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
delay(5000);
}
 
{
 // these are here to disengage the coils as not to hold ,and in turn over heat the motor
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);

}
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What stepper motor did you use? Or what do you mean by a printer motor?
Logged

Pages: [1]   Go Up
Jump to: