Hello everybody,
I'm quite new in the programming but I'm trying to improve my skills.
I'm making a project for my greengarden. I'm making automatic irrigation boom contdolled by my Mega 2560 and Nextion display. The problem that I have is that the "DO ....WHILE" loop in the "void b1PushCallback(void *ptr) " exits for some reason even if the while statement is still true. The idea is to repeat the code inside the function until the boom is detected on the other end of the greenhouse. I'm doing something wrong but have no idea what. Inside of the loop the RPM of my motor are calculated and the PWM to the VFD is regulated to achive a constant speed of movement. I'm posing my code and I'm here for any additional information.
#include <Nextion.h>
int ambtemp; // ambient air temp
int shum; // soil humiduty
volatile byte tick; // counter for the motor wheel
unsigned int rpm;
unsigned long timeold;
int actualspeed;
int pwm;
int adjust = 5;
#define ssensor A0 // soil sensor pin
#define rpmsensor 21 // motor rpm sensor
#define startpos 22 // sensor for start
#define endpos 23 // sensor for end
#define forward 24 // forward direction of movement
#define reverse 25 // reverse direction of movement
#define motor 2 // pwm for motor speed
#define pump 28 // pump relay
NexButton b1 = NexButton(3, 3, "b1"); // Button 5 l/dka
NexButton b2 = NexButton(3, 4, "b2"); // Button 10 l/dka
NexButton b3 = NexButton(3, 5, "b3"); // Button 15 l/dka
NexButton b0 = NexButton(7, 4, "b0"); // Stop Button
// Declare touch event objects to the touch event list:
// You just need to add the names of the objects that send a touch event.
// Format: &<object name>,
NexTouch *nex_listen_list[] =
{
&b1, // Button added
&b2, // Button added
&b3, // Button added
&b0,
NULL // String terminated
}; // End of touch event list
void tick_count() {
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
// If interrupts come faster than 200ms, assume it's a bounce and ignore
if (interrupt_time - last_interrupt_time > 200)
{
tick++;
}
last_interrupt_time = interrupt_time;
}
void RPM_calculation() {
if (tick >= 3) //Points to detect
{
rpm = 15 * 1000 / (millis() - timeold) * tick;
timeold = millis();
tick = 0;
}
}
void pwmvalue1 () {
if (rpm < 5) {
if (pwm != 255) {
pwm += adjust;
}
}
if (rpm > 5) {
if (pwm != 0) {
pwm -= adjust;
}
}
}
void b1PushCallback(void *ptr) // Press event for button 5 l/dka
{
digitalRead(startpos);
digitalRead(endpos);
if (startpos == 0 && endpos == 1) {
digitalWrite(reverse , HIGH);
} else digitalWrite(forward , HIGH);
do {
RPM_calculation();
Serial.print("rpm.val=");
Serial.print(rpm);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
pwmvalue1 ();
digitalWrite(pump , HIGH);
analogWrite(motor , pwm);
} while (startpos == 0);
}
//void b0PushCallback(void *ptr) // Press event for back button
//{
//digitalWrite(pump , LOW);
//analogWrite(motor , 0);
//}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(ssensor , INPUT);
pinMode(rpmsensor, INPUT_PULLUP);
pinMode(startpos , INPUT);
pinMode(endpos, INPUT);
pinMode(forward , OUTPUT);
digitalWrite(forward , LOW);
pinMode(reverse, OUTPUT);
digitalWrite(reverse , LOW);
pinMode(motor , OUTPUT);
attachInterrupt(digitalPinToInterrupt(rpmsensor), tick_count, FALLING);
// Register the event callback functions of each touch event:
// You need to register press events and release events seperatly.
// Format for press events: <object name>.attachPush(<object name>PushCallback);
// Format for release events: <object name>.attachPop(<object name>PopCallback);
// b0.attachPush(b0PushCallback); // Button press
b1.attachPush(b1PushCallback); // Button press
// b2.attachPush(b2PushCallback); // Button press
// b3.attachPush(b3PushCallback); // Button press
}
void loop() {
// put your main code here, to run repeatedly:
nexLoop(nex_listen_list); // Check for any touch event
digitalWrite(pump , LOW);
analogWrite(motor , 0);
shum = analogRead(ssensor);
shum = map(shum, 550, 0, 0, 100);
Serial.print("n7.val=");
Serial.print(shum);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
delay(1000);
}