Go Down

Topic: Why does he behaves like this? (Read 344 times) previous topic - next topic

Quietsch

Jan 17, 2018, 04:20 pm Last Edit: Jan 17, 2018, 04:37 pm by Quietsch
Hello, i am new with Arduino (and coding in general..) and try to understand the code. For this purpose i printed an roboticarm (MK1 by Carlo Franciscone - http://www.eezyrobots.it/index.html) and set up this with my Arduino. Take a look:
https://imgur.com/a/1FqH2 I couldn't bind the image into my post, sry.

Now, here is my Code:

Code: [Select]
#include <Servo.h> //Servo Biblio
#include <IRremote.h> //Setup IR + Biblio

Servo Servobase;
Servo Servoarml;
Servo Servoarmr;
Servo Servohand;

int angleBase = 86;
int angleArmr = 76;
int angleArml = 84;
int angleHand = 0;

#include <IRremote.h> //Setup IR + Biblio
int RECV_PIN = 7;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup() {
 Serial.begin(9600);

 Servobase.attach(9);
 Servoarml.attach(11);
 Servoarmr.attach(12);
 Servohand.attach(10);
 Servobase.write(angleBase);
 delay(500);
 Servoarml.write(angleArml);
 delay(500);
 Servoarmr.write(angleArmr);
 delay(500);
 Servohand.write(angleHand);
 delay(500);
 Serial.println("Servos active");

   // In case the interrupt driver crashes on setup, give a clue
  // to the user what's going on.
  Serial.println("Boot IRin");
  irrecv.enableIRIn(); // Start the receiver
  Serial.println("Enabled IRin");
}

void loop() {
   if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value

  if (results.value == 0)    {
      Servobase.write (angleBase+50);
      Serial.println("base cw");
      delay (20);
    }
  if (results.value == 800)    {
      Servobase.write (angleBase+50);
      Serial.println("base cw");
      delay (20);
    }
      if (results.value == 1)    {
      Servobase.write (angleBase-50);
      Serial.println("base ccw");
      delay (20);
    }
       if (results.value == 801)    {
      Servobase.write (angleBase-50);
      Serial.println("base ccw");
      delay (20);
    }
  }
 
}



Right now i just define 2 Buttons on my Remote App (Android RcCoid) and their repeat values (0 and 800; 1 and 801). But if i press the Buttons the Servo just does one turn for 50 degrees. A second press does not repeat the function... But a press of the other button lets him turn back and so on...

I can not film this right now because i use my phone as a remote, if i need to i will remap it to a tv remote to film it.

Is that understandable written...? Do you have the informations to tell me why he behaves like this?

Thank you

Quietsch

DrAzzy

#1
Jan 17, 2018, 04:45 pm Last Edit: Jan 17, 2018, 04:57 pm by DrAzzy
Connect it to a computer via USB while testing it and print some debugging information to the serial port so you can see what it's seeing - is it getting the second command? Is it just plain barfing at some point?

Does the *software* behave the same with the servos disconnected? If not, that implies that noise on the power rails from the motors is causing the microcontroller to hang or reset; use a separate power supply for the servos if this is the case (this is generally recommended)

Could it be that the interrupts required for servo and for IR are interfering with eachother?
ATtiny core for 841+1634+828 and x313/x4/x5/x61/x7/x8 series Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts (some assembled), mosfets and awesome prototyping board in my store http://tindie.com/stores/DrAzzy

Quietsch

#2
Jan 17, 2018, 05:08 pm Last Edit: Jan 17, 2018, 05:15 pm by Quietsch
Connected to the computer it shows me all the values i send...

The 4 Servos are connected to a DC Wall Adapter with 5V, while the Arduino runs from the USB port. The ground of both are connected...

The Servos got some noise if they are running they are a little jittering...

When i disconnect the servos and run it the Serial Monitor gives out this:

Quote from: Serial Monitor
Servos active
Boot IRin
Enabled IRin
0
base cw
1
base ccw
3
2
7
6
4
5
0
base cw
0
base cw
0
base cw
0
base cw
0
base cw
0
base cw
800
800
800
800
800
800
That means when i keep pressing the button he just print out the value, but not the control statement ("base cw" / "base ccw")... All the buttons i created he receives all the values correct...

DrAzzy

#3
Jan 17, 2018, 05:13 pm Last Edit: Jan 17, 2018, 05:16 pm by DrAzzy
Connected to the computer it shows me all the values i send...

The 4 Servos are connected to a DC Wall Adapter with 5V, while the Arduino runs from the USB port. The ground of both are connected...

The Servos got some noise if they are running they are a little jittering...
So have you checked via print statements that the point where the servo position is set is really getting run, and that your code is setting it to the position you think you are?

jitter and electrical noise are not the same. Jitter is a correct signal, but inconsistent timing, as opposed to spikes that shouldn't be there at all (for example, the starting and stopping of inductive loads can put spikes onto the power supply rail, sometimes large enough to reset or crash other things powered from it - though it sounds like this isn't happening here, since you report that it doesn't stop printing debug output).
ATtiny core for 841+1634+828 and x313/x4/x5/x61/x7/x8 series Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts (some assembled), mosfets and awesome prototyping board in my store http://tindie.com/stores/DrAzzy

Quietsch

#4
Jan 17, 2018, 05:35 pm Last Edit: Jan 17, 2018, 05:45 pm by Quietsch
 print statements
So have you checked via print statements that the point where the servo position is set is really getting run, and that your code is setting it to the position you think you are?

Could you give me an example how you mean that? I just got the "Serial.print" commands wich i checked...

jitter and electrical noise are not the same. Jitter is a correct signal, but inconsistent timing, as opposed to spikes that shouldn't be there at all (for example, the starting and stopping of inductive loads can put spikes onto the power supply rail, sometimes large enough to reset or crash other things powered from it - though it sounds like this isn't happening here, since you report that it doesn't stop printing debug output).
Ok, i think i make a video to this, i can't explain here what really happens..
Youtube Video Link
When i connect the Wall Adapter it doesn't start, only after booting the Arduino, so it has to come from the signal..?!

Quietsch

I tried to do it now with this switch case function, it behaves the same way...

Code: [Select]
void loop() {
   if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value
    switch (results.value) {
      case 0:
      Servobase.write (angleBase + 5);
      Serial.println("base cw");
      delay (20);
      break;
    }
    }
 }

DrAzzy

Yeah - I mean using serial.print() - but print more information. Like, when you pass a value to servo.write, also print that same value to the serial port - that way you can see if the value you're sending it is what you think you're sending it. That way, you can more closely follow what it's doing. I often draw out the control flow of a problematic piece of code, and write out the steps it should be doing by hand.

When a widely used hardware library isn't working for you, your default assumption should be that you're telling the library to do the wrong thing, so sanity check the values you send it.

(I haven't read all of your code - but this is the sort of thing I'd do to track down where things are going off the rails).
ATtiny core for 841+1634+828 and x313/x4/x5/x61/x7/x8 series Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts (some assembled), mosfets and awesome prototyping board in my store http://tindie.com/stores/DrAzzy

Quietsch

You're a boss!!!
While doing this i realized, that my code never changed the value of int angleBae...
I just wrote angleBase+5 to the servo....

I made this line: angleBase = angleBase+5;
and it is fixes :)

Thank you mate!!!

Go Up