0

I'm writing a project where an Arduino board that is connected to a servo engine receives commands from another Arduino. The possible commands are:

  1. Move 10 degrees
  2. Reset position
  3. Sweep until you're told to stop

My problem is with the sweep-mode, when I get the sweep command, I get into a while loop, that first checks for a sweep-terminator and then sweeps.

} else if (message == sweep) {
  Serial.println("sweep");
  while (msg1 != sweep_term) {
    // set the direction for the servo to move
    if (val == 750){
      val = 2250;
    } else {
      val = 750;
    }

    // move the servo
    myservo.write(val);
    // wait for the servo to move
    delay(15);

    // wait for a message
    if (vw_get_message(buf1, &buflen1)) {
      buf1[buflen1] = '\0';
      msg1 = (char*)buf1;
      memset(buf1,'0',VW_MAX_MESSAGE_LEN);
      Serial.print(buflen1);
      Serial.print(" [sweep] Got: ");
      Serial.println(msg1);
    }
  }
}

For some reason, when I'm in this loop I don't get the sweep terminator message, or I get a part of it. I suspect that the servo is responsible for this, since when I run the same program without myservo.write()s I do receive those terminators.

  • What do you imagine that memset(buf1,'0',VW_MAX_MESSAGE_LEN); does? – James Waldby - jwpat7 Aug 8 '15 at 17:07
  • @jwpat7, fills buf1 with zeros? It wasn't intended to be null-terminator or anything else. msg1 is a of type String, not char * – Quaker Aug 8 '15 at 20:45
  • @Quaker You're filling the string with VW_MAX_MESSAGE_LEN zero characters - it's ending up as "000000000....000". '0' is the character 0 as if you had typed it on your keyboard, not ASCII character code 0 or the NULL character. – Majenko Aug 8 '15 at 21:55
  • Please post the data types for msg1 and buf1. – Nick Gammon Aug 8 '15 at 22:09
  • I suspect that the servo is responsible for this - can you run the code, with the myservo.write() but without the servo physically connected. This will eliminate a possible electrical problem. – Nick Gammon Aug 9 '15 at 0:38
1

If msg1 is of type char* (as is implied by the cast in msg1 = (char*)buf1;), then it seems to me that that statement will copy the address of buf1 to msg1. Then, when you fill buf1 with ascii zeroes (ie, characters '0') msg1 ends up being a string of ascii zeroes.

On the other hand, if msg1 is of type String (to go along with your comment about buf1 being of type String) the cast should be unnecessary.

Note, if you don't give the source for the vw_get_message() routine and the declarations for all of the constants and variables used in the code extract, it's unlikely anyone can definitively answer your question.

Note, the while test, msg1 != sweep_term, may not be well-defined on entry to the while loop. Or, if msg1 is left over from a previous use, you won't enter the loop at all. Consider writing the loop in the form do { ... } while (msg1 != sweep_term); instead.

Note, if (as said in a comment) buf1 is of type String, then the statement memset(buf1,'0',VW_MAX_MESSAGE_LEN); is a line of code I'd recommend against, because it treats buf1 as a simple array rather than as an object. (In this case it should make no difference, but it requires some research to be sure of that. [1,2,3,4,5])

  • It seems likely buf1 is uint8_t judging by this prototype: uint8_t vw_get_message (uint8_t *buf, uint8_t *len) – Nick Gammon Aug 9 '15 at 1:05
  • buf1 is indeed a uint8_t and msg1 is a String. I thought that assigning a char * string to a String copies it (as in C#) and not just copying the pointer. – Quaker Aug 9 '15 at 13:43
  • Could it be that the movement of the engine is blocking the RF receiver from receiving the message that is being sent while it's working? – Quaker Aug 9 '15 at 13:43
  • 1
    For problem diagnosis, you could try powering different systems (such as Arduino, radio, and servos) with isolated and well-bypassed power supplies, and make all connections with short, soldered-in-place wires. (See eg seattlerobotics re bypassing.) Have a 10 or 20 uF cap connected where power goes onto each board, and 0.001 or 0.01 uF caps from each power line to ground, to prevent electrical noise from spreading via power supply connections. You may need to enclose the radio (except its antenna) in a metal case. – James Waldby - jwpat7 Aug 11 '15 at 2:15
  • 1
    If you can temporarily substitute a wired connection in place of the radio connection, that would allow you to check out your code and perhaps decide if problems are mostly hardware or mostly software. – James Waldby - jwpat7 Aug 11 '15 at 2:17
0

I simply connected the servo to an external power 5V power source, and everything works perfectly fine. It seems like the servo's resistance was too much for the RF receiver to be able to work as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.