I have a problem when I attach the servo motor in this code, it cracks the code, I tried to put other codes for the servo motor but same thing. Can you please help me to optimize the code for servomotor?
I have a smartphone app made in the myth app inventor, when I start the servo motor it works, but when I want to stop it I get a message (Broken Pipe)
#include <ServoTimer2.h> //libraria Servomotorului
#include <SoftwareSerial.h>
#include <DHT.h>
#include <String.h>
#define DHT_PIN 6 // Pinul la care este conectat senzorul DHT11
#define DHTTYPE DHT11 // Specifică tipul de senzor DHT
int RELEU_PIN = 3; // Pin citire date digitale
int SERVO_PIN = 9;
int LED_PIN = 2;
const byte rxPin = 10;
const byte txPin = 11;
int ANALOG_UMIDITATE = A4;
int ANALOG_PICATURI = A5;
int ProcentajPicaturi;
int ProcentajUmiditate;
String umiditate_picaturi;
int c ;
ServoTimer2 servomotor; // Declarați o instanță a obiectului Servo
SoftwareSerial bluetoothSerial(rxPin,txPin);
DHT dht(DHT_PIN, DHTTYPE); // Inițializează obiectul pentru senzorul DHT11
void setup() {
bluetoothSerial.begin(9600); //Initializare comunicare seriala cu modulul Bluetooth
Serial.begin(9600);
pinMode(RELEU_PIN, OUTPUT); // Atribuie pinului 3 valoarea output
digitalWrite(RELEU_PIN,LOW);
pinMode(LED_PIN,OUTPUT);
pinMode(SERVO_PIN,OUTPUT);
servomotor.attach(SERVO_PIN);
dht.begin(); // Inițializează senzorul DHT11
}
void loop() {
ProcentajUmiditate = analogRead (ANALOG_UMIDITATE);
int UmiditateSol = map(ProcentajUmiditate, 0, 1023, 100, 0);
ProcentajPicaturi = analogRead (ANALOG_PICATURI);
int PicaturiPloaie = map(ProcentajPicaturi, 0, 1023, 100, 0);
float umiditate_aer = dht.readHumidity(); // Citeste umiditatea
float temperatura_aer = dht.readTemperature(); // Citeste temperatura
delay(1000);
umiditate_picaturi = String(UmiditateSol) + ","+ String(PicaturiPloaie) + "," + String(umiditate_aer) + "," + String(temperatura_aer);
bluetoothSerial.println(umiditate_picaturi);
if (bluetoothSerial.available())
{
c = bluetoothSerial.read();
if (c == '1')
{
digitalWrite(RELEU_PIN, HIGH);
digitalWrite(LED_PIN,HIGH);
}
if (c == '0')
{
digitalWrite (RELEU_PIN, LOW);
digitalWrite(LED_PIN,LOW);
//servomotor.write(750);
}
}
if(c == '1'){
servomotor.write(750);
delay(1000);
servomotor.write(1500);
delay(1000);
}
delay(2000); //Pauza între citiri
}
I connected the arduino uno board to 9V, the whole system works fine, I disconnected the servo motor from the control pin.
With the servomotor connected to the command pin, after 1-2 minutes of opening the system, it does not respond to commands, but the rest of the system works fine, the servomotor after 1-2 minutes has a small error moves one degree to the left and one to the right without receiving the command.
This might be a memory-issue because each time you assign a new value to a variable of type String it eats another piece of free RAM until the RAM gets corrupted
At the moment I've tested it for 15 minutes and it works fine, I'll come back with an update if anything happens. Can you please remove the code from this platform. Because I am using it on a project that is going through the plagiarism program. You can leave only the part you added to the code. I'm afraid it will detect the whole thing as copied.
You asked on a public forum for help.
You should have modified your code that much before posting publicly that it will not be detected by a plagiarism test.
Well you have been too lazy for learning it all yourself. So you aked in the arduino-forum.
Everything is OK with asking.
If you want to make sure that your own code will pass the plagiarism-test you will have to modify all variable names and some of the structure after you have the solution that it will pass the test.
I recommend of you do modify your variable-names and function names that you take time to think about names that really describe spot-on what their purpose is and what your functions do.
This will make a positive impression to the people that judge about the project
is a raw anlog-to digital converted value ranging from 0 up tp 1023
and
map(ProcentajUmiditate, 0, 1023, 100, 0);
calculates a percentage-value ranging from 0 to 100
I might be wrong but
So far my impression is that you are trying to minimise your own effort
to get away "as cheap as possible"
I know what you're saying. I'll say I asked for help on some of the code, but in this case it's going to come out all plagiarized code. I sent it in this morning, I'll fill in the modified part, but they already have the first part of the code.
if (c == '1') {
servomotor.write(750);
delay(1000);
servomotor.write(1500);
delay(1000);
}
[/quote]
well using the support of a public forum would require to post your attempt how you tried it.
If your teacher / professor would find a thread where she/he can follow your learning process how to use functions I'm sure that this would make a good impression.
The way you are asking / posting has one possible plausible explanation:
You knowledge-level is rather low so that you do not yet understand what these lines of code do
you should post your own attempt how you tried to code it combined with a a specific question
so a detailed feedback can be given that you can learn from