Hello, I am trying to make a weather station powered by two solar cells that follow the sun with two grove servomotors, but it does not work. I determined that the problem is that the servomotors do not work at all when the gsm module is connected, but separately they work just fine. What is the problem?
#include <NeoSWSerial.h>
#include "LowPower.h"
#include "DHT20.h"
#include "U8g2lib.h"
#include "Seeed_BMP280.h"
#include "Servo.h"
Servo servoA;
Servo servoB;
int leftL;
int leftR;
int rightL;
int rightR;
int waittime = 50;
int resetThreshold =100;
int lightlevel = 0;
const int analogInPin = A4;
uint8_t count = 0;
int BatteryValue = 0;
float outputValue = 0;
int button_state = 0;
int value = 0;
bool sendTemperature = false;
bool sendHumidity = false;
bool sendPressure = false;
bool sendAltitude = false;
bool sendBatV = false;
bool sendLight = false;
bool sleepon = false; // Flag for sleep mode
bool previousSleepState = false;
// Flag to turn off sleep mode
NeoSWSerial sim800(8, 7); // RX, TX
void setup() {
sim800.begin(9600); // SIM800C baud rate
delay(1000);
sim800.println("AT+CMGF=1");
delay(500);
sim800.println("AT+CNMI=1,2,0,0,0");
servoA.attach(3);
servoB.attach(2);
servoA.write(60);
servoB.write(60);
}
void loop() {
if (sim800.available()) {
String receivedMessage = sim800.readStringUntil('\n');
receivedMessage.trim();
processReceivedSMS(receivedMessage);
}
leftL = analogRead(A0);
leftR = analogRead(A1) ;
rightL =analogRead(A3);
rightR = analogRead(A2);
lightlevel = (leftR + leftL + rightL + rightR) / 4;
if (lightlevel < resetThreshold) {
while (servoA.read() != 60 || servoB.read() != 60) {
if (servoA.read() >60) {
servoA.write(servoA.read() - 1);
}
else {
servoA.write(servoA.read() + 1);
}
if ( servoB.read() > 60) {
servoB.write(servoB.read() - 1);
}
else {
servoB.write(servoB.read() + 1);
}
delay(waittime);
}
// while (lightlevel < resetThreshold) {
// delay(waittime);
// leftL = analogRead(A1);
// leftR = analogRead(A2) - 80;
// value = map(analogRead(A4),800, 1023, 0, 1023);
// rightL = value;
// rightR = analogRead(A3);
// lightlevel = (leftR + leftL + rightL + rightR) / 4;
// monitorButtonAndDisplay();
}
else
{
while (abs(leftL - leftR) > 2000|| abs(rightL - rightR) > 2000)
{
if (leftL > leftR) {
servoA.write(servoA.read() + 1);
if(servoA.read() > 100 ) {servoA.write(servoA.read() - 1);}
} else {
servoA.write(servoA.read() - 1);
if(servoA.read() < 20 ) {servoA.write(servoA.read() +1); }
}
if (rightL > rightR) {
servoB.write(servoB.read() + 1);
if(servoB.read() > 100 ) {servoB.write(servoB.read() - 1);}
} else {
servoB.write(servoB.read() - 1);
if(servoB.read() < 20 ) {servoB.write(servoB.read() +1); }
}
delay(waittime);
leftL = analogRead(A0);
leftR = analogRead(A1) ;
rightL = analogRead(A3);
rightR = analogRead(A2);
}
}
}
void processReceivedSMS(String smsContent) {
sendTemperature = false;
sendHumidity = false;
sendPressure = false;
sendAltitude = false;
sendLight = false;
sendBatV = false;
if (smsContent.indexOf("SendTemperature") != -1)
{
sendTemperature = true;
}
if (smsContent.indexOf("SendHumidity") != -1)
{
sendHumidity = true;
}
if (smsContent.indexOf("SendPressure") != -1) {
sendPressure = true;
}
if (smsContent.indexOf("SleepOn") != -1) {
sleepon = true; // Set sleep mode flag
}
if (smsContent.indexOf("SleepOff") != -1) {
// Set flag to turn off sleep mode
sleepon = false;
}
if (smsContent.indexOf("SendLight") != -1)
{
sendLight = true;
}
if (smsContent.indexOf("SendAltitude") != -1)
{
sendAltitude = true;
}
if (smsContent.indexOf("sendBat") != -1)
{
sendBatV = true;
}
if (sendTemperature || sendHumidity || sendPressure || sendLight || sendAltitude || sendBatV) {
String response = F("Sensor Readings:\n");
if (sendTemperature) {
response += F("Temperature:C\n");
}
if (sendHumidity) {
response += F("Humidity:%\n");
}
if (sendPressure) {
response += F("Pressure: atm\n");
}
if(sendLight)
{
response += F("Lightlevel: \n");
}
if(sendAltitude)
{
response += F("Altitude: m") ;
}
if(sendBatV)
{
response += F("Bat Voltage: V\n");
}
sim800.println("AT+CMGF=1");
delay(500);
sim800.print("AT+CMGS=\"+40752189030\"\r");
sim800.println();
delay(500);
if (previousSleepState != sleepon) {
previousSleepState = sleepon;
String statusMessage = " ";
if (sleepon) {
statusMessage = F("STATUS:OFFLINE\n");
} else {
statusMessage = F("STATUS:ONLINE\n");
}
delay(1000);
sim800.println(statusMessage);
sim800.write(char(26));
}
sim800.println(response);
sim800.write(char(26));
if(sleepon == true)
{
delay(500);
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
}
}
}