Loop not looping

I hit a wall that I just cannot get through. This is a project that's reading the user input through a load cell that's connected through an RF transmitter to a nano and it's receiving in an RF receiver to a Mega. The communication between these two works but once I set the motors to react according to certain limits set, the loop doesn't loop. The reading works but once the reading hits the threshold, the motors turn on and it stays like that, dead.

I need help! Please!!!!!!!!

#include <RH_ASK.h>
#include <SPI.h> 

#define enA 9
#define in1 4
#define in2 5
#define enB 10
#define in3 6
#define in4 7

int motorSpeedA = 0;
int motorSpeedB = 0;

//int period = 5000;
//unsigned long time_now = 0;

String str_weight;
String str_counter;
String str_out;

RH_ASK rf_driver;

void setup() {
  Serial.begin(9600);
  
  rf_driver.init();
  
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
 
}

void loop() {
  
  uint8_t buf[11];
  uint8_t buflen=sizeof(buf);
  if (rf_driver.recv(buf, &buflen))
  {
    str_out=String((char*)buf);

    for (int i=0; i< str_out.length(); i++) 
    {
      if (str_out.substring(i, i+1) == ",")
      {
        str_weight = str_out.substring(0, i);
        str_counter = str_out.substring(i+1);
        break;
      }
    }

int weight = str_weight.toInt();

Serial.print("Reading: ");
Serial.println(weight);  
    

//    Serial.print("Reading: ");
//    Serial.print(str_weight);
//    Serial.print("Counter: ");
//    Serial.println(str_counter);

 if ((weight >= 5) && (weight <= 11)) {
  // Set Motor A
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  // Set Motor B 
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  
  motorSpeedA = map(weight, 11, 6, 170, 255);
  motorSpeedB = map(weight, 11, 6, 170, 255); 

  Serial.print ("motorSpeedA: ");
  Serial.println (motorSpeedA);
  Serial.print ("motorSpeedB: ");
  Serial.println (motorSpeedB);

  analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
   }
   else{
   }
  }
}

I don't think loop() doesn't loop. There's a fair chance though your code gets stuck on a line like this:

if (rf_driver.recv(buf, &buflen))

Is that by chance a function that only returns when the complete message of buflen characters has been received? I would also expect to see the & in front of buf rather than buflen.

Try autoformat. To me it looks like a strange order among the right curly bracketts {} belove the line mentioned by wvmarle.

How do you power the motors? If the trouble starts with motor start, then most probably your power supply is too weak for this project.

wvmarle:
I don't think loop() doesn't loop. There's a fair chance though your code gets stuck on a line like this:

if (rf_driver.recv(buf, &buflen))

Is that by chance a function that only returns when the complete message of buflen characters has been received? I would also expect to see the & in front of buf rather than buflen.

Yes it is, it is receiving the info from the nano through the RF communication. I tried to add & in front of buf and it gave me an error.

The & in front of buflen is definitely wrong.
Use:

if (rf_driver.recv(buf, buflen))

Pete

el_supremo:
The & in front of buflen is definitely wrong.

It's not (at least not for version 1.89 of the RH library). From the documentation

recv()
bool RH_ASK::recv ( uint8_t * buf,
uint8_t * len
)
virtual

Turns the receiver on if it not already on. If there is a valid message available, copy it to buf and return true else return false. If a message is copied, *len is set to the length (Caution, 0 length messages are permitted). You should be sure to call this function frequently enough to not miss any messages It is recommended that you call it in your main loop.

Parameters
[in] buf Location to copy the received message
[in,out] len Pointer to available space in buf. Set to the actual number of octets copied.

Returns
true if a valid message was copied to buf

From the library source files

bool RH_ASK::recv(uint8_t* buf, uint8_t* len)
{
    if (!available())
 return false;

    if (buf && len)
    {
 // Skip the length and 4 headers that are at the beginning of the rxBuf
 // and drop the trailing 2 bytes of FCS
 uint8_t message_len = _rxBufLen-RH_ASK_HEADER_LEN - 3;
 if (*len > message_len)
    *len = message_len;
 memcpy(buf, _rxBuf+RH_ASK_HEADER_LEN+1, *len);
    }
    _rxBufValid = false; // Got the most recent message, delete it
//    printBuffer("recv:", buf, *len);
    return true;
}

rjaguilera1993:
Yes it is, it is receiving the info from the nano through the RF communication.

That means if you're sending less data than it expects, it will block. That can very well be your problem. & or no & will not cause it to hang normally, just not behave as you expect to.

@sterretje. Thanks. My bad.

Pete

Thank you all for the response, I added a delay after the analogwrite and somehow it magically worked.

I hope you realise that this is not a good fix.

It does not address the original problem - you don't even seem to know what the original problem really is. It may work now, but when the stars align differently it may stop working again and you're back at square 0.