the code and the topic are both down below
You may want to read this tutorial.
You also may want to read the forum guidelines. Especially the ones about code formatting.
thank you i did try to replace delay with millis but still not working
hi everyone
for my project I need to make a solar tracking system that shows the voltage and the power collected from that solar panel so I used an i2c LCD for display and a INA219 current sensor I have two codes one for solar tracking and one for power display , they work perfectly separated but when I combine them nothing works
if someone can help me i will be so thankful
those are the two codes that i need to combine
Solar tracking CODE :
#include <Servo.h>
//defining Servos
Servo servohori;
int servoh = 0;
int servohLimitHigh = 160;
int servohLimitLow = 20;
Servo servoverti;
int servov = 0;
int servovLimitHigh = 160;
int servovLimitLow = 20;
//Assigning LDRs
int ldrtopl = 2; //top left LDR green
int ldrtopr = 1; //top right LDR yellow
int ldrbotl = 3; // bottom left LDR blue
int ldrbotr = 0; // bottom right LDR orange
void setup ()
{
servohori.attach(10);
servohori.write(90);
servoverti.attach(9);
servoverti.write(90);
delay(500);
}
void loop()
{
servoh = servohori.read();
servov = servoverti.read();
//capturing analog values of each LDR
int topl = analogRead(ldrtopl);
int topr = analogRead(ldrtopr);
int botl = analogRead(ldrbotl);
int botr = analogRead(ldrbotr);
// calculating average
int avgtop = (topl + topr) / 2; //average of top LDRs
int avgbot = (botl + botr) / 2; //average of bottom LDRs
int avgleft = (topl + botl) / 2; //average of left LDRs
int avgright = (topr + botr) / 2; //average of right LDRs
if (avgtop < avgbot)
{
servoverti.write(servov + 1);
if (servov > servovLimitHigh)
{
servov = servovLimitHigh;
}
delay(10);
}
else if (avgbot < avgtop)
{
servoverti.write(servov - 1);
if (servov < servovLimitLow)
{
servov = servovLimitLow;
}
delay(10);
}
else
{
servoverti.write(servov);
}
if (avgleft > avgright)
{
servohori.write(servoh + 1);
if (servoh > servohLimitHigh)
{
servoh = servohLimitHigh;
}
delay(10);
}
else if (avgright > avgleft)
{
servohori.write(servoh - 1); if (servoh < servohLimitLow)
{
servoh = servohLimitLow;
}
delay(10);
}
else
{
servohori.write(servoh);
}
delay(50);
}
Display CODE :
// lcd
#include <Wire.h>
#include <INA219_WE.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDRESS 0x40
INA219_WE ina219(I2C_ADDRESS);
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup() {
lcd.init();
lcd.backlight();
lcd.clear();
Serial.begin(9600);
Wire.begin();
if (!ina219.init()) {
Serial.println("INA219 not connected!");
lcd.print("INA219");
lcd.setCursor(0, 1);
lcd.print(" not connected!");
}
Serial.println("INA219 Current Sensor with solar panel");
}
void loop() {
float shuntVoltage_mV = 0.0;
float loadVoltage_V = 0.0;
float busVoltage_V = 0.0;
float current_mA = 0.0;
float power_mW = 0.0;
bool ina219_overflow = false;
shuntVoltage_mV = ina219.getShuntVoltage_mV();
busVoltage_V = ina219.getBusVoltage_V();
current_mA = ina219.getCurrent_mA();
loadVoltage_V = busVoltage_V + (shuntVoltage_mV / 1000);
power_mW = current_mA * (loadVoltage_V);
ina219_overflow = ina219.getOverflow();
Serial.print("Shunt Voltage [mV]: "); Serial.println(shuntVoltage_mV);
Serial.print("Bus Voltage [V]: "); Serial.println(busVoltage_V);
Serial.print("Load Voltage [V]: "); Serial.println(loadVoltage_V);
Serial.print("Current[mA]: "); Serial.println(current_mA);
Serial.print("Power[mW]: "); Serial.println(current_mA * loadVoltage_V);
lcd.clear();
lcd.print("Voltage:");
lcd.setCursor(8, 0);
lcd.print(loadVoltage_V);
lcd.print("V ");
lcd.setCursor(0, 1);
lcd.print("Current:");
lcd.setCursor(8, 1);
lcd.print(current_mA);
lcd.print("mA ");
delay(3000);
lcd.clear();
lcd.print("Power:");
lcd.setCursor(6, 0);
lcd.print(power_mW);
lcd.print(current_mA * loadVoltage_V, 0);
lcd.print("mW ");
delay(3000);
if (!ina219_overflow) {
Serial.println("Values OK - no overflow");
}
else {
Serial.println("Overflow! Choose higher PGAIN");
}
Serial.println();
delay(1000);
}
This is the combination of the two codes :
#include <Servo.h>
#include <Wire.h>
#include <INA219_WE.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDRESS 0x40
INA219_WE ina219(I2C_ADDRESS);
LiquidCrystal_I2C lcd(0x27,16,2);
Servo servohori;
int servoh = 0;
int servohLimitHigh = 160;
int servohLimitLow = 20;
Servo servoverti;
int servov = 0;
int servovLimitHigh = 160;
int servovLimitLow = 20;
//Assigning LDRs
int ldrtopl = 2; //top left LDR green
int ldrtopr = 1; //top right LDR yellow
int ldrbotl = 3; // bottom left LDR blue
int ldrbotr = 0; // bottom right LDR orange
void setup ()
{
servohori.attach(10);
servohori.write(90);
servoverti.attach(9);
servoverti.write(90);
delay(500);
lcd.init();
lcd.backlight();
lcd.clear();
Serial.begin(9600);
Wire.begin();
if(!ina219.init()){
Serial.println("INA219 not connected!");
lcd.print("INA219");
lcd.setCursor(0,1);
lcd.print(" not connected!");
}
Serial.println("INA219 Current Sensor with solar panel");
}
void loop()
{
servoh = servohori.read();
servov = servoverti.read();
//capturing analog values of each LDR
int topl = analogRead(ldrtopl);
int topr = analogRead(ldrtopr);
int botl = analogRead(ldrbotl);
int botr = analogRead(ldrbotr);
// calculating average
int avgtop = (topl + topr) / 2; //average of top LDRs
int avgbot = (botl + botr) / 2; //average of bottom LDRs
int avgleft = (topl + botl) / 2; //average of left LDRs
int avgright = (topr + botr) / 2; //average of right LDRs
if (avgtop < avgbot)
{
servoverti.write(servov + 1);
if (servov > servovLimitHigh)
{
servov = servovLimitHigh;
}
delay(10);
}
else if (avgbot < avgtop)
{
servoverti.write(servov - 1);
if (servov < servovLimitLow)
{
servov = servovLimitLow;
}
delay(10);
}
else
{
servoverti.write(servov);
}
if (avgleft > avgright)
{
servohori.write(servoh + 1);
if (servoh > servohLimitHigh)
{
servoh = servohLimitHigh;
}
delay(10);
}
else if (avgright > avgleft)
{
servohori.write(servoh - 1); if (servoh < servohLimitLow)
{
servoh = servohLimitLow;
}
delay(10);
}
else
{
servohori.write(servoh);
}
delay(50);
float shuntVoltage_mV = 0.0;
float loadVoltage_V = 0.0;
float busVoltage_V = 0.0;
float current_mA = 0.0;
float power_mW = 0.0;
bool ina219_overflow = false;
shuntVoltage_mV = ina219.getShuntVoltage_mV();
busVoltage_V = ina219.getBusVoltage_V();
current_mA = ina219.getCurrent_mA();
loadVoltage_V = busVoltage_V + (shuntVoltage_mV / 1000);
power_mW = current_mA * (loadVoltage_V);
ina219_overflow = ina219.getOverflow();
Serial.print("Shunt Voltage [mV]: "); Serial.println(shuntVoltage_mV);
Serial.print("Bus Voltage [V]: "); Serial.println(busVoltage_V);
Serial.print("Load Voltage [V]: "); Serial.println(loadVoltage_V);
Serial.print("Current[mA]: "); Serial.println(current_mA);
Serial.print("Power[mW]: "); Serial.println(current_mA * loadVoltage_V);
lcd.clear();
lcd.print("Voltage:");
lcd.setCursor(8, 0);
lcd.print(loadVoltage_V);
lcd.print("V ");
lcd.setCursor(0, 1);
lcd.print("Current:");
lcd.setCursor(8, 1);
lcd.print(current_mA);
lcd.print("mA ");
delay(3000);
lcd.clear();
lcd.print("Power:");
lcd.setCursor(6, 0);
lcd.print(power_mW);
lcd.print(current_mA * loadVoltage_V, 0);
lcd.print("mW ");
delay(3000);
if (!ina219_overflow) {
Serial.println("Values OK - no overflow");
}
else {
Serial.println("Overflow! Choose higher PGAIN");
}
Serial.println();
delay(1000);
}
Please read How to get the best out of this forum and pay special attention to the use of code tags; next edit your post and apply it.
Please post your attempt to merge the two sketches and indicate what problems you have with that.
Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project.
That is a lot of improperly posted un-formatted code to dig through.
Read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.
Please go back and fix your original post
http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html
Please post tour attempt at the merged code. Tell us what the code actually does and how that differs from what you want. We can only help with code that we can see.
If there are compile errors, please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.
after merging both codes the system doesn't work the solar panel doesn't follow the light
but the LCD displays the power/ current / voltage
Thank you for fixing your post. Much easier to read.
I do not see anything in the combined code that would make the servos not move.
The Serial.print is your best debugging tool. Put some prints in your code to monitor the values of variables, function return values and watch program flow.
Your two topics on the same or similar subject have been merged.
Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.
Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.
Repeated duplicate posting could result in a temporary or permanent ban from the forum.
Could you take a few moments to Learn How To Use The Forum
It will help you get the best out of the forum in the future.
Thank you.
how can I print serial movement from the servos
I'm sorry I'm new to Arduino
i think i know what's the problem
the solar panel takes like 5 min to get to where the light is
i think it have to do with something with the delays
There is no way for the code to know where the servos actually are. The servo.read() function just returns the last position that was sent to the servo by the servo.write() function.
I am not sure that the delays are the whole problem. The servos need time to move.
If the panel has a long ways to move it will take a while at 1 degree at a time. Perhaps a more proportional approach to where the servos move in larger chunks when they have far to go. Say, take the horizontal LDR outputs. If they are only 10 adc counts apart move one at a time, but if the difference between left and right is 500 adc counts, move 50 degrees.
I didn't get it very well but I did delete all the delays from the code and it did work but the only problem is that the the LCD print everything at the same time is there anyway i can do an other temporization function
Your topic has been moved to a more suitable location on the forum.