//Servo motor library
#include <Servo.h>
//Initialize variables
int mode = 0;
int axe = 0;            
int buttonState1 = 0;    
int buttonState2 = 0;   
int prevButtonState1 = 0;
int prevButtonState2 = 0; 
 
int ldrtopr= 0;                // top-right LDR                          
int ldrtopl = 1;               // top-left LDR                          
int ldrbotr = 2;               // bottom-right LDR                     
int ldrbotl = 3;               // bottom-left LDR                   
int topl = 0;
int topr = 0; 
int botl = 0;
int botr = 0;

//Declare two servos
Servo servo_updown;
Servo servo_rightleft;

int threshold_value=10;           //measurement sensitivity

void setup()
{
  Serial.begin(9600);                                //serial connection setup  //opens serial port, sets data rate to 9600 bps
  Serial.println("CLEARDATA");                       //clear all data that’s been place in already
  Serial.println("LABEL,t,voltage,current,power,Mode");   //define the column headings (PLX-DAQ command)

  pinMode(12, INPUT);              //Mode switch Button
  pinMode(11, INPUT);              //Axis switch
  pinMode(A4, INPUT);              //Potentiometer for right-left movement and for up-down movement
  
  servo_updown.attach(5);             //Servo motor up-down movement
  servo_rightleft.attach(6);          //Servo motor right-left movement
}

void loop()
{
//  pv_power();
char Mode;
    float volt = analogRead(A5)*5.0/1023;
    float voltage = 2*volt;                //  Volt=(R1/R1+R2)*Voltage / R1=R2=10Ohms  => voltage=2*volt)
    float current = voltage/20;            //  I=voltage/(R1+R2) 
    float power = voltage*current;
    Serial.print("DATA,TIME,"); // PLX-DAQ command
    Serial.print(voltage);    //send the voltage to serial port
    Serial.print(",");
    Serial.print(current);    //send the current to serial port
    Serial.print(",");
    Serial.print(power);  //send the power to serial port
    Serial.print(",");
    
//    Serial.println(Mode);      
  buttonState1 = digitalRead(12);
  if (buttonState1 != prevButtonState1) {
    if (buttonState1 == HIGH) {
      //Change mode and ligh up the correct indicator  
      if (mode == 1) {
        mode = 0;
      } else {
        mode = 1;
      }
    }
  }
  prevButtonState1 = buttonState1;
  delay(50); // Wait for 50 millisecond(s)
  
  if (mode == 0) {
    Mode='M';
    Serial.println(Mode);   //send Mode "Manual" to serial port    
    manualsolartracker();
  } else { // mode automatic
    Mode = 'A';
    Serial.println(Mode);      
    automaticsolartracker(); //send Mode "Automatic" to serial port
    } 
}

void automaticsolartracker(){
  
     //capturing analog values of each LDR
     topr= analogRead(ldrtopr);         //capturing analog value of top right LDR
     topl= analogRead(ldrtopl);         //capturing analog value of top left LDR
     botr= analogRead(ldrbotr);         //capturing analog value of bot right LDR
     botl= analogRead(ldrbotl);         //capturing analog value of bot left LDR

    // calculating average
     int avgtop = (topr + topl) / 2;     //average of top LDRs
     int avgbot = (botr + botl) / 2;     //average of bottom LDRs
     int avgleft = (topl + botl) / 2;    //average of left LDRs
     int avgright = (topr + botr) / 2;   //average of right LDRs
   
    //Get the different 
     int diffelev = avgtop - avgbot;      //Get the different average betwen LDRs top and LDRs bot
     int diffazi = avgright - avgleft;    //Get the different average betwen LDRs right and LDRs left
    
    //left-right movement of solar tracker
     
      if (abs(diffazi) >= threshold_value){        //Change position only if light difference is bigger then the threshold_value
       if (diffazi > 0) {
        if (servo_rightleft.read() < 180) {
          servo_rightleft.write((servo_updown.read() + 2));
        }
      }
      if (diffazi <  0) {
        if (servo_rightleft.read() > 0) {
          servo_rightleft.write((servo_updown.read() - 2));
        }
      }
    }
             
      //up-down movement of solar tracker

      if (abs(diffelev) >= threshold_value){    //Change position only if light difference is bigger then thethreshold_value
       if (diffelev > 0) {
        if (servo_updown.read() < 180) {
          servo_updown.write((servo_rightleft.read() - 2));
        }
      }
      if (diffelev <  0) {
        if (servo_updown.read() > 0) {
          servo_updown.write((servo_rightleft.read() + 2));
        }
      }
    }       
 }  

void manualsolartracker(){
  buttonState2 = digitalRead(13);
  if (buttonState2 != prevButtonState2) {
    if (buttonState2 == HIGH) {
      //Change mode and ligh up the correct indicator  
      if (axe == 1) {
        axe = 0;
      } else {
        axe = 1;
      }
    }
  }
  prevButtonState2 = buttonState2;
  delay(50); // Wait for 50 millisecond(s)
  if (axe == 0) {     //control right-left movement
    servo_rightleft.write(map(analogRead(A4), 0, 1023, 0, 180));
  } else { // //control up-down movement
    servo_updown.write(map(analogRead(A4), 0, 1023, 0, 180));
  } 
} 

Assembly Online Compiler

Write, Run & Share Assembly code online using OneCompiler's Assembly online compiler for free. It's one of the robust, feature-rich online compilers for Assembly language. Getting started with the OneCompiler's Assembly compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Assembly and start coding.

About Assembly

Assembly language(asm) is a low-level programming language, where the language instructions will be more similar to machine code instructions.

Every assembler may have it's own assembly language designed for a specific computers or an operating system.

Assembly language requires less execution time and memory. It is more helful for direct hardware manipulation, real-time critical applications. It is used in device drivers, low-level embedded systems etc.

Syntax help

Assembly language usually consists of three sections,

  1. Data section

    To initialize variables and constants, buffer size these values doesn't change at runtime.

  2. bss section

    To declare variables

  3. text section

    _start specifies the starting of this section where the actually code is written.

Variables

There are various define directives to allocate space for variables for both initialized and uninitialized data.

1. To allocate storage space to Initialized data

Syntax

variable-name    define-directive    initial-value 
Define DirectiveDescriptionAllocated Space
DBDefine Byte1 byte
DWDefine Word2 bytes
DDDefine Doubleword4 bytes
DQDefine Quadword8 bytes
DTDefine Ten Bytes10 bytes

2. To allocate storage space to un-initialized data

Define DirectiveDescription
RESBReserve a Byte
RESWReserve a Word
RESDReserve a Doubleword
RESQReserve a Quadword
RESTReserve a Ten Bytes

Constants

Constants can be defined using

1. equ

  • To define numeric constants
CONSTANT_NAME EQU regular-exp or value

2. %assign

  • To define numeric constants.
%assign constant_name value

3. %define

  • To define numeric or string constants.
%define constant_name value

Loops

Loops are used to iterate a set of statements for a specific number of times.

mov ECX,n
L1:
;<loop body>
loop L1

where n specifies the no of times loops should iterate.

Procedures

Procedure is a sub-routine which contains set of statements. Usually procedures are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

procedure_name:
   ;procedure body
   ret