Go Down

Topic: dual axis solar tracker via 4 photoresistors and dc motors code cycling issue (Read 864 times) previous topic - next topic

Gompy

I really do not understand why you reject this circuit for the L298N ?
I have been using this schematic and the sketch for a while and I can not fault it.
I use normal engines with delay use but engines with a reduction ....... about 100 rpm is good enough.
This means that you do not have to use enA and enB for PWM and only in1 tm in4.
On the motors there is a threaded M8 with a bearing nut which controls the panel horizontally and vertically.
As a result, the displacement is only 1 mm per revolution or 100 mm per minute, if more than enough to adjust the panel quietly and without shocks.

The sketch must be adapted to the use without enA and enB.

Please turn off your capslock ...... it is now as if you are screaming.

BTW, why do you use count ????


Code: [Select]

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,20,4); // address 0x27 - 4 rows - 20 character

// motor azimuth L298N
  int in1 = 9;
  int in2 = 10;
// motor elevation L298N
  int in3 = 11;
  int in4 = 12;

void setup()
{
  Serial.begin(9600); // initialize the serial port
  lcd.init(); // initialize the lcd

  lcd.backlight(); //ON
  lcd.clear(); //lcd clear
}

void loop()
{

// LIGHT SENSORS
  int tr = analogRead(0); // LDR top right / brown
  int br = analogRead(1); // LDR bottom right / green
  int tl = analogRead(2); // LDR top left / yellow
  int bl = analogRead(3); // LDR bottom left / orange

// control delay time in milliseconds of LIGHT SENSOR readings
  int delaytime = analogRead(A7)*2;
// set range of tolerance between LIGHT SENSOR readings - Arduino LED indicate time
  int tolerance = analogRead(A6)/4;
 // set range for read 0 - 100%
  int sensetive = map(analogRead(A6), 1022, 0, 0, 100);

 //print LIGHT SENSOR values to serial monitor for debugging
  Serial.println("--------------------");
  Serial.print("Top Left\t");
  Serial.print(tl);
  Serial.println(); 
  Serial.print("Bottom Left\t");
  Serial.print(bl);
  Serial.println(); 
  Serial.print("Top Right\t");   
  Serial.print(tr);
  Serial.println(); 
  Serial.print("Bottom Left\t"); 
  Serial.print(bl);
  Serial.println();

  Serial.print("Delaytime\t");
  Serial.print(delaytime);
  Serial.println();
  Serial.print("Tolerance\t");
  Serial.print(tolerance);
  Serial.println();
  Serial.println("--------------------");

  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 dv = avt - avd; // average difference of top and bottom LIGHT SENSORS
  int dh = avl - avr;// average difference of left and right LIGHT SENSORS

  if (-1*tolerance > dv || dv > tolerance) // check if the difference in top/bottom LIGHT SENSORS is greater than tolerance
{
  if (avt > avd) // if average LIGHT SENSOR values on top side are greater than on bottom side then elevation motor rotates CLOCKWISE
  {

// set motor Elevation enable
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  Serial.println("ELEVATION MOTOR MOVES UPWARDS");
  lcd.setCursor(0,0);
  lcd.print("ELEVATION :    UP"); // print lcd row 1
  }
  else // if average LIGHT SENSOR values on bottom side are greater than on top side then elevation motor rotates COUNTERCLOCKWISE
  {
// set motor Elevation enable
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  Serial.println("ELEVATION MOTOR MOVES DOWN");
  lcd.setCursor(0,0);
  lcd.print("ELEVATION :  DOWN");// print lcd row 1
  }
}
  else if (-1*tolerance < dv || dv < tolerance) // if difference is smaller than tolerance, STOP elevation motor
  {

// stop Elevation motor
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  Serial.println("ELEVATION MOTOR STOP");
  lcd.setCursor(0,0);
  lcd.print("ELEVATION :  STOP"); // print lcd row 1
  }

  if (-1*tolerance > dh || dh > tolerance) // check if the difference in left and right LIGHT SENSORS is within tolerance range
{
  if (avl > avr) // if average LIGHT SENSOR values on left side are greater than right side, azimuth motor rotates CLOCKWISE
  {

// set motor Azimuth enable
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  Serial.println("AZIMUTH MOTOR MOVES EAST");
  lcd.setCursor(0,1);
  lcd.print("AZIMUTH   :  EAST"); // print lcd row 2
  }
  else // if average LIGHT SENSOR values on right side are greater than on left side, azimuth motor rotates COUNTERCLOCKWISE
  {
// set motor Azimuth enable
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  Serial.println("AZIMUTH MOTOR MOVES WEST");
  lcd.setCursor(0,1);
  lcd.print("AZIMUTH   :  WEST"); // print lcd row 2
  }
}
  else if (-1*tolerance < dh || dh < tolerance) //if difference is smaller than tolerance, STOP azimuth motor
  {
// stop motor Azimuth
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  Serial.println("AZIMUTH MOTOR STOP");
  lcd.setCursor(0,1);
  lcd.print("AZIMUTH   :  STOP"); // print lcd row 2
  }
// print lcd delaytime row 3
  lcd.setCursor(0,2);
  lcd.print("DELAY     :  ");
  char temp[5]; // enough room for 4 numbers
  snprintf(temp, 5, "%4d", delaytime); // %4d = 4 digits, right aligned
  lcd.print(temp);
  lcd.setCursor(18, 2);
  lcd.print("ms"); // time in milliseconds

// print lcd sensitive row 4
  lcd.setCursor(0,3);
  lcd.print("SENSITIVE :   ");
  char temp1[4]; // enough room for 3 numbers
  snprintf(temp1, 4, "%3d", sensetive); // %3d = 3 digits, right aligned
  lcd.print(temp1);
  lcd.setCursor(18, 3);
  lcd.print("%"); //
 
  delay(delaytime); // wait for next loop
}

knightridar

hi, i didn't know you could use the l298 without pwm enA and enB options.
thanks for the code that could be useful for everyone else and I may try it out too.

I read on other sites the L298 is inefficient.
It may have caused a voltage drop when I was powering my motors with a 6V solar panel.
The cause for them not working could have been something else.
Lots of people on the forum recommended using a battery and it did help and work.

I wanted to try to move the motors without the use of batteries.
I ended up getting a larger 6V solar panel that was harder to find but I eventually found it:

https://grabcad.com/library/solar-module-6v-10w-1-6a-pn-cnc340x220-6-1

https://www.aliexpress.com/item/Solar-Module-6V-10W-1-6A-Portable-Module-DIY-Small-Solar-Panel-for-Cellular-Phone-Charger/32831959124.html?spm=a2g0s.9042311.0.0.37854c4dvY10AI

My motors move fine with a good amount of sun.

I would recommend using the L298 for cost effectiveness since it's cheaper than the BTS7960:
The bts 7960 is better for larger motors that require more voltage and current.

https://grabcad.com/library/bts7960-43a-high-power-h-bridge-module-1

https://www.aliexpress.com/store/product/Double-BTS7960-43A-H-bridge-High-power-Motor-Driver-module-diy-smart-car-Current-diagnostic/221555_32849254807.html?spm=2114.12010615.0.0.44882c80voOAwR

Here is the original code for reference:
http://www.instructables.com/id/Arduino-Solar-Tracker/


I will try out the L298 again myself
Another cheap alternative is L9110S
I don't know if it's more efficient than the L298 but it's voltage range is not as high.

https://grabcad.com/library?page=1&time=all_time&sort=recent&query=l9110s

I was/am a fan of writing in CAPs but since it's considered screaming I'm not using it.
I am noticing though it's easier to code using a both upper case and lower case letters.

I can't recall what the count++ function was for.




Go Up