Two void functions with same code, one works, the other one doesnt

Hello
I have a problem with my code. I defined different void functions (biturbo, vorgehen and kopf). I run them in my void loop().
Now the function biturbo() works, while vorgehen() doesnt. When I plug the arduino uno to my pc, biturbo() works only partially and vorgehen() not at all. When I plug it to the wall socket, biturbo() works like intended but vorgehen() not at all.

The function kopf() works perfectly fine (its a servo, biturbo() and vorgehen() are motor with a motorshield).

What did I miss? I cant find a solution.

#include <IRremote.hpp>

#include <Servo.h>




Servo myservo;
int pos = 0;

void kopf(){
  for (pos = 0; pos <= 70; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15 ms for the servo to reach the position
  }
  for (pos = 70; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15 ms for the servo to reach the position
  }
}
void vorgehen(){
  //Motor A vorwärts mit maximaler Geschwindigkeit
  digitalWrite(12, HIGH);  // Einstellen der Vorwärtsrichtung für Channel A
  digitalWrite(9, LOW);    // Deaktivieren der Bremse für Channel A
  analogWrite(3, 190);    // Drehen des Motors A mit maximaler Geschwindigkeit

  //Motor B vorwärts mit maximaler Geschwindigkeit
  digitalWrite(13, LOW);  // Einstellen der Vorwärtsrichtung für Channel B
  digitalWrite(8, LOW);    // Deaktivieren der Bremse für Channel B
  analogWrite(11, 190);    // Drehen des Motors B mit maximaler Geschwindigkeit
}
void rgehen(){
  //Motor A vorwärts mit normaler Geschwindigkeit
  digitalWrite(12, LOW);  // Einstellen der Rückwärtsrichtung für Channel A
  digitalWrite(9, LOW);    // Deaktivieren der Bremse für Channel A
  analogWrite(3, 150);    // Drehen des Motors A mit normaler Geschwindigkeit

  //Motor B vorwärts mit normaler Geschwindigkeit
  digitalWrite(13, HIGH);  // Einstellen der Rückwärtsrichtung für Channel B
  digitalWrite(8, LOW);    // Deaktivieren der Bremse für Channel B
  analogWrite(11, 150);    // Drehen des Motors B mit normaler Geschwindigkeit
}
void augen(){
  
}
void stoppen(){
  analogWrite(3, 0);
  analogWrite(11, 0);
  digitalWrite(9, HIGH);  // Aktivieren der Bremse für Channel A
  digitalWrite(8, HIGH);  // Aktivieren der Bremse für Channel B
}
void biturbo(){
  //Motor A vorwärts mit maximaler Geschwindigkeit
  digitalWrite(12, HIGH);  // Einstellen der Vorwärtsrichtung für Channel A
  digitalWrite(9, LOW);    // Deaktivieren der Bremse für Channel A
  analogWrite(3, 255);    // Drehen des Motors A mit maximaler Geschwindigkeit

  //Motor B vorwärts mit maximaler Geschwindigkeit
  digitalWrite(13, LOW);  // Einstellen der Vorwärtsrichtung für Channel B
  digitalWrite(8, LOW);    // Deaktivieren der Bremse für Channel B
  analogWrite(11, 255);    // Drehen des Motors B mit maximaler Geschwindigkeit
}
void setup() {
  Serial.begin(115200);
  IrReceiver.begin(7, ENABLE_LED_FEEDBACK); // Start the receiver
  myservo.attach(10); 
}

void loop() {
  
  if (IrReceiver.decode()){
    Serial.println(IrReceiver.decodedIRData.decodedRawData, HEX);

    IrReceiver.resume(); // Enable receiving of the next value
  } 
  
  if(IrReceiver.decodedIRData.decodedRawData  == 0xE718FF00){
    vorgehen();
    
    Serial.println("vgehen");
    
    delay(3000);    
  }
  if(IrReceiver.decodedIRData.decodedRawData == 0xAD52FF00){
    rgehen();
    Serial.println("rgehen");
    delay(3000);
  }
  if(IrReceiver.decodedIRData.decodedRawData == 0xBA45FF00){
    
    Serial.println("kopf");
    kopf();
    delay(3000);
  }
  if(IrReceiver.decodedIRData.decodedRawData == 0xB946FF00){
    augen();
    Serial.println("augen");
    delay(3000);
  }
  if(IrReceiver.decodedIRData.decodedRawData == 0xE916FF00){
    stoppen();
    Serial.println("stoppen");
    delay(1000);
  }
  if(IrReceiver.decodedIRData.decodedRawData == 0xF20DFF00){
    biturbo();
    Serial.println("biturbo");
    delay(3000);
    }
  
  //IrReceiver.resume(); // Enable receiving of the next value
  
}

I edited it to my current code.

If this line of code //vorgehen(); was changed to vorgehen(); vorgehen run?

May be power issues.

@aniturtle you posted about your project in a section that said it was not for posting about your project. Please take more chair in future.

I have moved your post to a better place.

sorry, its just vorgehen(). the // where from another testrun. Still, vorgehen(); doesnt work.

Regarding the power issues, I tested different sources and still the same.

Thank you for your input.

thank you

void vorgehen(){
Serial.println( "A" );
  //Motor A vorwärts mit maximaler Geschwindigkeit
  digitalWrite(12, HIGH);  // Einstellen der Vorwärtsrichtung für Channel A
Serial.println( "b" );
  digitalWrite(9, LOW);    // Deaktivieren der Bremse für Channel A
Serial.println( "C" );
  analogWrite(3, 190);    // Drehen des Motors A mit maximaler Geschwindigkeit
Serial.println( "D" );
  //Motor B vorwärts mit maximaler Geschwindigkeit
  digitalWrite(13, LOW);  // Einstellen der Vorwärtsrichtung für Channel B
Serial.println( "E" );
  digitalWrite(8, LOW);    // Deaktivieren der Bremse für Channel B
Serial.println( "F" );
  analogWrite(11, 190);    // Drehen des Motors B mit maximaler Geschwindigkeit
Serial.println( "G" );
}

If the call to the function is uncommented and the thing is changed to print out stuff, does it print anything?

1 Like

Yes it prints everything

So vorgehen() runs.

1 Like

Yes, it prints everything, but digitalWrite() und analogWrite seem not to work.

I also treid to copy the code of biturbo() into vorgehen() and biturbo() worked but vorgehen() not at all.

This is a different problem then the problem described in post#1.

So please, describe the issue.

Post images of the project.

Post a schematic.

2 Likes

Was pin 12 setup in setup() programed to be an output, an input, with a pull up, or a pulldown or no pull?

Such as.

pinMode( 12, OUTPUT );
pinMode( 9,OUTPUT );
pinMode( 3, OUPUT);

and so on and so froth.

Essentially making the problem, "I did not properly setup the pin modes in setup() and now I am complaining the GPIO pins do not properly work."

but but but you already said

When I plug the arduino uno to my pc...
When I plug it to the wall socket…

which leaves me thinking it is a power issue.

Try your code with LEDs with series resistors take the place of anything that draws significant power.

And yeah, time for a schematic of everything.

a7

Well for analogWrite that only works on the pins that have a ~ against there name, so on an Arduino Uno that is pins 11, 10, 9, 6, 5 & 3.

No, I didnt set up the pins in void setup(). I was provided an Arduino uno to work with in a school project and I was given a test programm where the pins werent set up in setup(), therefore I didnt see the need for that. In the end, this is a programm to control a little and simple robot. Besides the Arduino I got a motorshield and the motors (Motor A and B). vorgehen() is the code/function to get the motors moving. As you can see, the code of vorgehen() is the same as biturbo() but one number different. These numbers (255&190) represent the speed.
Im sorry if I phrased my question badly, please let me rephrase: Im using a IR remote to "trigger" the different functions (which you can find infront of setup() ). When I plug the whole setup (consisting of arduino, motorshield and motors) everything works fine if I press the button for biturbo() and it even prints

in the loop(). But when I press the button for vorgehen(), it prints

but nothing happens. Like the function itself works (seen by "vgehen"), but the digitalWrite() and analogWrite() dont. So why does biturbo() execute its function and vorgehen() not?

We can't see your code.

edited it, now its cleaner and hopefully better for you to understand.

Where?
Why edit a previous post?

I had a code that didnt work, tried a lot of different things, got frustrated and in my frustration uploaded a mess. The code currently upoaded is my original (not working) code.