top of page

Arduino Code (Final Code to be Posted Soon)

This is my Arduino Code for the Solar Tracker. What it does is compare the two photo-resistors, then has the motor move so that they are equal. This allows the Solar Panel to get the most sunlight.

​

Original Source Code belongs to:

http://robojax.com/learn/arduino/?vid=robojax-0293

​

int PinL = 5; //Left Sensor Pin
int PinR = 4; //Right Sensor Pin
int Pin8 = 8; //Digital Pin 8 for Motor
int Pin9 = 9; //Digital Pin 9 for Motor
int Pin10 = 10; //Digital Pin 10 for Motor
int Pin11 = 11; //Digital Pin 11 for Motor
int pResistorL = A4;  //Analog Pin 4 Photo-Resistor
int pResistorR = A5;  //Analog Pin 5 Photo-Resistor
int Step = 0;
int count = 0;
int pRValR = 0;
int pRValL = 0;
int CW = 2; //Clockwise Direction
int CCW = 1;  //Counter-Clockwise Direction
int Stop; //Variable to stop the motor
int dir = CW; 

void MotorControl(void);

void setup() {
  pinMode(Pin8, OUTPUT);
  pinMode(Pin9, OUTPUT);
  pinMode(Pin10, OUTPUT);
  pinMode(Pin11, OUTPUT);
  pinMode(pResistorR, INPUT);
  pinMode(pResistorL, INPUT);
  Step = 0;
  Serial.begin(9600);
}

void loop() {

  pRValR = analogRead(pResistorR);  //Sensor Value for Right Photo-Resistor
  pRValL = analogRead(pResistorL);  //Sensor Value for Left Photo-Resistor

  Serial.print('(');
  Serial.print(pRValR); //Print value for Right Photo-Resistor
  Serial.print(',');  
  Serial.print(pRValL);  //Print value for Left Photo-Resistor
  Serial.print(')');
  if(pRValR > pRValL + 5){
    dir = CCW;
  }
  else if(pRValR < pRValL - 5){
    dir = CW;
  }
  else{
    dir = Stop;
  }
  MotorControl();
} //End Main Loop

​

 void MotorControl(){
  switch(Step){     //Spin the motor
    case 0:
      digitalWrite(Pin8, LOW);
      digitalWrite(Pin9, LOW);
      digitalWrite(Pin10, LOW);
      digitalWrite(Pin11, HIGH);
      break;
    case 1:
      digitalWrite(Pin8, LOW);
      digitalWrite(Pin9, LOW);
      digitalWrite(Pin10, HIGH);
      digitalWrite(Pin11, LOW);
      break;
    case 2:
      digitalWrite(Pin8, LOW);
      digitalWrite(Pin9, HIGH);
      digitalWrite(Pin10, LOW);
      digitalWrite(Pin11, LOW);
      break;
    case 3:
      digitalWrite(Pin8, HIGH);
      digitalWrite(Pin9, LOW);
      digitalWrite(Pin10, LOW);
      digitalWrite(Pin11, LOW);
      break;
    default:
      digitalWrite(Pin8, LOW);
      digitalWrite(Pin9, LOW);
      digitalWrite(Pin10, LOW);
      digitalWrite(Pin11, LOW);
      break; 
    }
    if (dir == CW){
      Step++;
    }
    else if (dir == CCW){
      Step--;
    }
    else{
      void;
    }
    if(Step>3){
      Step = 0;
    }
    if(Step<0){
      Step = 3;
    }
}

© 2023 by Benjamin Fontaine. Proudly created with Wix.com

bottom of page