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

**URL:** <https://forum.arduino.cc/t/two-void-functions-with-same-code-one-works-the-other-one-doesnt/987508>\
**Category:** Programming\
**Created:** [May 2, 2022, 12:00pm UTC](https://forum.arduino.cc/t/two-void-functions-with-same-code-one-works-the-other-one-doesnt/987508 "2022-05-02T12:00:22Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![aniturtle](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/aniturtle/32/760805_2.png) [@aniturtle](https://forum.arduino.cc/u/aniturtle)\
**Post date:** [May 2, 2022, 12:00pm UTC](https://forum.arduino.cc/t/two-void-functions-with-same-code-one-works-the-other-one-doesnt/987508/1 "2022-05-02T12:00:22Z")

</div>

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.

```cpp
#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.

---

<div class="post-metadata">

**Author:** ![Idahowalker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/idahowalker/32/238858_2.png) [@Idahowalker](https://forum.arduino.cc/u/Idahowalker)\
**Post date:** [May 2, 2022, 12:36pm UTC](https://forum.arduino.cc/t/two-void-functions-with-same-code-one-works-the-other-one-doesnt/987508/2 "2022-05-02T12:36:13Z")

</div>

> [@aniturtle](#):
>
> ```arduino
> if(IrReceiver.decodedIRData.decodedRawData == 0xE718FF00){
> //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
> Serial.println("vgehen");
>     
> delay(3000);    
> }
> 
> ```

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

> [@aniturtle](#):
>
> When I plug it to the wall socket, biturbo() works like intended but vorgehen() not at all.

May be power issues.

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [May 2, 2022, 12:47pm UTC](https://forum.arduino.cc/t/two-void-functions-with-same-code-one-works-the-other-one-doesnt/987508/3 "2022-05-02T12:47:33Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![aniturtle](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/aniturtle/32/760805_2.png) [@aniturtle](https://forum.arduino.cc/u/aniturtle)\
**Post date:** [May 2, 2022, 12:47pm UTC](https://forum.arduino.cc/t/two-void-functions-with-same-code-one-works-the-other-one-doesnt/987508/4 "2022-05-02T12:47:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![aniturtle](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/aniturtle/32/760805_2.png) [@aniturtle](https://forum.arduino.cc/u/aniturtle)\
**Post date:** [May 2, 2022, 12:48pm UTC](https://forum.arduino.cc/t/two-void-functions-with-same-code-one-works-the-other-one-doesnt/987508/5 "2022-05-02T12:48:40Z")

</div>

thank you

---

<div class="post-metadata">

**Author:** ![Idahowalker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/idahowalker/32/238858_2.png) [@Idahowalker](https://forum.arduino.cc/u/Idahowalker)\
**Post date:** [May 2, 2022, 12:51pm UTC](https://forum.arduino.cc/t/two-void-functions-with-same-code-one-works-the-other-one-doesnt/987508/6 "2022-05-02T12:51:15Z")

</div>

```cpp
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?

---

<div class="post-metadata">

**Author:** ![aniturtle](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/aniturtle/32/760805_2.png) [@aniturtle](https://forum.arduino.cc/u/aniturtle)\
**Post date:** [May 2, 2022, 12:55pm UTC](https://forum.arduino.cc/t/two-void-functions-with-same-code-one-works-the-other-one-doesnt/987508/7 "2022-05-02T12:55:47Z")

</div>

Yes it prints everything

---

<div class="post-metadata">

**Author:** ![Idahowalker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/idahowalker/32/238858_2.png) [@Idahowalker](https://forum.arduino.cc/u/Idahowalker)\
**Post date:** [May 2, 2022, 1:03pm UTC](https://forum.arduino.cc/t/two-void-functions-with-same-code-one-works-the-other-one-doesnt/987508/8 "2022-05-02T13:03:01Z")

</div>

So vorgehen() runs.

---

<div class="post-metadata">

**Author:** ![aniturtle](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/aniturtle/32/760805_2.png) [@aniturtle](https://forum.arduino.cc/u/aniturtle)\
**Post date:** [May 2, 2022, 1:06pm UTC](https://forum.arduino.cc/t/two-void-functions-with-same-code-one-works-the-other-one-doesnt/987508/9 "2022-05-02T13:06:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aniturtle](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/aniturtle/32/760805_2.png) [@aniturtle](https://forum.arduino.cc/u/aniturtle)\
**Post date:** [May 2, 2022, 1:11pm UTC](https://forum.arduino.cc/t/two-void-functions-with-same-code-one-works-the-other-one-doesnt/987508/10 "2022-05-02T13:11:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Idahowalker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/idahowalker/32/238858_2.png) [@Idahowalker](https://forum.arduino.cc/u/Idahowalker)\
**Post date:** [May 2, 2022, 1:19pm UTC](https://forum.arduino.cc/t/two-void-functions-with-same-code-one-works-the-other-one-doesnt/987508/11 "2022-05-02T13:19:17Z")

</div>

> [@aniturtle](#):
>
> Yes, it prints everything, but digitalWrite() und analogWrite seem not to work.

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.

---

<div class="post-metadata">

**Author:** ![DaveX](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/davex/32/751443_2.png) [@DaveX](https://forum.arduino.cc/u/DaveX)\
**Post date:** [May 2, 2022, 1:21pm UTC](https://forum.arduino.cc/t/two-void-functions-with-same-code-one-works-the-other-one-doesnt/987508/12 "2022-05-02T13:21:04Z")

</div>

> [@aniturtle](#):
>
> What did I miss? I cant find a solution.
> 
> ```arduino
> 
> ```

> **[pinMode() - Arduino Reference](https://www.arduino.cc/reference/en/language/functions/digital-io/pinmode/)**
>
> The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.

> **[sketch.ino - Wokwi ESP32, STM32, Arduino Simulator](https://wokwi.com/projects/330551838935876179)**
>
> Run IoT and embedded projects in your browser: ESP32, STM32, Arduino, Pi Pico, and more. No installation required!

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/b/7/3/b737011505e60b3c3b2a23fdf5402456bd5e2118.png)

---

<div class="post-metadata">

**Author:** ![Idahowalker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/idahowalker/32/238858_2.png) [@Idahowalker](https://forum.arduino.cc/u/Idahowalker)\
**Post date:** [May 2, 2022, 1:26pm UTC](https://forum.arduino.cc/t/two-void-functions-with-same-code-one-works-the-other-one-doesnt/987508/13 "2022-05-02T13:26:02Z")

</div>

> [@aniturtle](#):
>
> ```arduino
> void setup() {
> Serial.begin(115200);
> IrReceiver.begin(7, ENABLE_LED_FEEDBACK); // Start the receiver
> myservo.attach(10); 
> }
> 
> ```

> [@aniturtle](#):
>
> `digitalWrite(12, HIGH); `

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.

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

and so on and so froth.

```

> **[pinMode() - Arduino Reference](https://www.arduino.cc/reference/en/language/functions/digital-io/pinmode/)**
>
> The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.

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."

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [May 2, 2022, 1:27pm UTC](https://forum.arduino.cc/t/two-void-functions-with-same-code-one-works-the-other-one-doesnt/987508/14 "2022-05-02T13:27:46Z")

</div>

> [@aniturtle](#):
>
> I tested different sources and still the same.

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

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [May 2, 2022, 4:36pm UTC](https://forum.arduino.cc/t/two-void-functions-with-same-code-one-works-the-other-one-doesnt/987508/15 "2022-05-02T16:36:21Z")

</div>

> [@aniturtle](#):
>
> but digitalWrite() und analogWrite seem not to work

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.

---

<div class="post-metadata">

**Author:** ![aniturtle](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/aniturtle/32/760805_2.png) [@aniturtle](https://forum.arduino.cc/u/aniturtle)\
**Post date:** [May 2, 2022, 5:15pm UTC](https://forum.arduino.cc/t/two-void-functions-with-same-code-one-works-the-other-one-doesnt/987508/16 "2022-05-02T17:15:35Z")

</div>

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

> [@aniturtle](#):
>
> `Serial.println("biturbo");`

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

> [@aniturtle](#):
>
> `Serial.println("vgehen");`

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?

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [May 2, 2022, 5:16pm UTC](https://forum.arduino.cc/t/two-void-functions-with-same-code-one-works-the-other-one-doesnt/987508/17 "2022-05-02T17:16:49Z")

</div>

We can't see your code.

---

<div class="post-metadata">

**Author:** ![aniturtle](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/aniturtle/32/760805_2.png) [@aniturtle](https://forum.arduino.cc/u/aniturtle)\
**Post date:** [May 2, 2022, 5:18pm UTC](https://forum.arduino.cc/t/two-void-functions-with-same-code-one-works-the-other-one-doesnt/987508/18 "2022-05-02T17:18:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [May 2, 2022, 5:19pm UTC](https://forum.arduino.cc/t/two-void-functions-with-same-code-one-works-the-other-one-doesnt/987508/19 "2022-05-02T17:19:55Z")

</div>

Where?  
Why edit a previous post?

---

<div class="post-metadata">

**Author:** ![aniturtle](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/aniturtle/32/760805_2.png) [@aniturtle](https://forum.arduino.cc/u/aniturtle)\
**Post date:** [May 2, 2022, 5:26pm UTC](https://forum.arduino.cc/t/two-void-functions-with-same-code-one-works-the-other-one-doesnt/987508/20 "2022-05-02T17:26:03Z")

</div>

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.

[Next page](https://forum.arduino.cc/t/two-void-functions-with-same-code-one-works-the-other-one-doesnt/987508.md?page=2)
