# Newbie Problem mit einem Sketch

**URL:** <https://forum.arduino.cc/t/newbie-problem-mit-einem-sketch/1339031>\
**Category:** Deutsch\
**Created:** [January 4, 2025, 6:25pm UTC](https://forum.arduino.cc/t/newbie-problem-mit-einem-sketch/1339031 "2025-01-04T18:25:08Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![uwefed](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/uwefed/32/213736_2.png) [@uwefed](https://forum.arduino.cc/u/uwefed)\
**Post date:** [January 5, 2025, 11:48pm UTC](https://forum.arduino.cc/t/newbie-problem-mit-einem-sketch/1339031/21 "2025-01-05T23:48:53Z")

</div>

> [@martin-lo](#):
>
> pinMode(POT\_PIN, INPUT);

Analoge Eingänge müssen und sollen nicht mit pinMode() deklariert werden.

---

<div class="post-metadata">

**Author:** ![martin-lo](https://avatars.discourse-cdn.com/v4/letter/m/82dd89/32.png) [@martin-lo](https://forum.arduino.cc/u/martin-lo)\
**Post date:** [January 5, 2025, 11:52pm UTC](https://forum.arduino.cc/t/newbie-problem-mit-einem-sketch/1339031/22 "2025-01-05T23:52:56Z")

</div>

Solang ich kein PullUp gebe sollte es nicht schaden 😉  
Ich habe diesbezüglich noch keine Probleme bekommen.

---

<div class="post-metadata">

**Author:** ![uwefed](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/uwefed/32/213736_2.png) [@uwefed](https://forum.arduino.cc/u/uwefed)\
**Post date:** [January 5, 2025, 11:56pm UTC](https://forum.arduino.cc/t/newbie-problem-mit-einem-sketch/1339031/23 "2025-01-05T23:56:28Z")

</div>

Aber daß es pinMode() für einen analogen Eingang braucht ist falsch.

---

<div class="post-metadata">

**Author:** ![martin-lo](https://avatars.discourse-cdn.com/v4/letter/m/82dd89/32.png) [@martin-lo](https://forum.arduino.cc/u/martin-lo)\
**Post date:** [January 6, 2025, 12:01am UTC](https://forum.arduino.cc/t/newbie-problem-mit-einem-sketch/1339031/24 "2025-01-06T00:01:55Z")

</div>

Kann schon sein, das Du recht hast. Ich habe das Datenblatt des ATMega noch nicht verinnerlicht. Ich weiß nur aus dem was ich so mache was geht und was nicht.

---

<div class="post-metadata">

**Author:** ![martin-lo](https://avatars.discourse-cdn.com/v4/letter/m/82dd89/32.png) [@martin-lo](https://forum.arduino.cc/u/martin-lo)\
**Post date:** [January 6, 2025, 1:12am UTC](https://forum.arduino.cc/t/newbie-problem-mit-einem-sketch/1339031/25 "2025-01-06T01:12:58Z")

</div>

Habe dein Prog mal etwas angepasst. So benutze ich diese H-Brücke.

> **Sketch abgewandelt**
>
> ```cpp
> /*
> * Written by Tiny Boat Productions, 2022
> * DIY Pick up winder version 2
> * 
> * Referance Documents
> * Potentiometer: https://docs.arduino.cc/learn/electronics/potentiometer-basics
> * DC Motor: https://www.tutorialspoint.com/arduino/arduino_dc_motor.htm
> * Reed Switch: https://create.arduino.cc/projecthub/muchika/reed-switch-with-arduino-81f6d2
> * I2C LCD: https://create.arduino.cc/projecthub/Arnov_Sharma_makes/lcd-i2c-tutorial-664e5a
> * Debounce: https://www.arduino.cc/en/Tutorial/BuiltInExamples/Debounce
> * H-Bridge: https://hackerstore.nl/PDFs/Tutorial298.pdf
> * 
> */
> 
> #include "Wire.h"
> #include "LiquidCrystal_I2C.h" // v1.1.2
> 
> const int DEBUG_PORT = 9600;
> const unsigned long DEBOUNCE_DELAY = 20;
> const int LCD_COLUMNS = 16;
> 
> //Pin declarations
> //const int Enable = 9; //motor Enable pin
> const int POT_PIN = A1; //pot switch pin
> const int REED_PIN = 3; //reed switch pin
> const int CW_PIN = 6; //clockwise pin vorher 5
> const int CCW_PIN = 5; //vorher 4
> const int LPWM = 10; // IN1 L298
> const int RPWM = 11; // IN2 L298
> 
> LiquidCrystal_I2C lcd(0x27,20,4); //LCD setup
> 
> //Inital Values
> int potVal; //reading from the potentiometer
> int lastPotVal = 0;
> int motorSpeed;
> int turnCount = 0; //revoultion count
> bool runState = false; //run state
> bool lastRunState = false;
> unsigned long lastDebounceTime = 0;
> int turnsSinceUpdate = 0;
> int lastUpdateTime = 0;
> int currentRPM = 0;
> int lastPercent = 0;
> int motorPercent = 0;
> byte Richtung;
> enum Richtung {Stop, Rechts, Links};
> 
> void handleReedUpdate() {
> int currentTime = millis();
> 
> if (currentTime - lastDebounceTime > DEBOUNCE_DELAY) {
> turnsSinceUpdate++;
> currentRPM = 60 / (currentTime - lastUpdateTime);
> lastUpdateTime = currentTime;
> }
> lastDebounceTime = currentTime;
> }
> 
> void setup() {
> //set up motor and reed switch pins
> // pinMode(Enable, OUTPUT);
> pinMode(REED_PIN, INPUT_PULLUP);
> attachInterrupt(digitalPinToInterrupt(REED_PIN), handleReedUpdate, FALLING);
> pinMode(CW_PIN, INPUT_PULLUP);
> pinMode(CCW_PIN, INPUT_PULLUP);
> pinMode(RPWM, OUTPUT);
> pinMode(LPWM, OUTPUT);
> // pinMode(POT_PIN, INPUT);
> 
> Serial.begin(DEBUG_PORT);
> Serial.println("Start");
> //set up the lcd
> lcd.init();
> lcd.backlight(); //turn on the backlight
> lcd.setCursor(0,0); //set the cursor in the uper left corner
> lcd.print("Pickup winder");
> lcd.setCursor(0,1); //set the cursor at the start of the second line
> lcd.print("By: Tiny Boat");
> delay(1500);
>   
> while (analogRead(POT_PIN) > 5){ //Make sure the motor is at low speed before starting it
> lcd.clear();
> lcd.setCursor(0,0);
> lcd.print("Turn pot CCW");
> delay(500);
> }
>   
> while(digitalRead(REED_PIN) == 0){ //Ensure you dont start on the magnet so the count is accurate
> lcd.clear();
> lcd.setCursor(0,0);
> lcd.print("Turn winding wheel 1/4 turn");
> delay(500);
> turnCount = 0;
> }
> 
> while(digitalRead(CW_PIN) == 0 || digitalRead(CCW_PIN) == 0){ //Ensure the switch is in the off position
> lcd.clear();
> lcd.setCursor(0,0);
> lcd.print("Flip switch to");
> lcd.setCursor(0,1);
> lcd.print("off position");
> delay(500);
> }
> 
> lcd.clear();
> lcd.print("Speed: Count:");
> lcd.setCursor(0, 1);
> lcd.print("0");
> }
> 
> void loop() {
> // put your main code here, to run repeatedly:
> potVal = analogRead(POT_PIN);
> if(digitalRead(CW_PIN) == 0){
> lastRunState = runState;
> runState = true;
> Richtung= Rechts;
> } else if (digitalRead(CCW_PIN) == 0){
> lastRunState = runState;
> runState = true;
> Richtung= Links;
> } else {
> lastRunState = runState;
> runState = false;MOTOR(0, 0); Richtung=Stop;
> }
> 
> //set the motor speed var
> if (!runState){
> motorSpeed = 0;
> } else if ((potVal != lastPotVal || runState != lastRunState) && runState) { //if the motor speed or the run state has ch/anged, and the motor is not off
> motorSpeed = map(potVal,4,1020,0,255);
> motorSpeed = constrain(motorSpeed, 0, 255);
> lastPotVal = potVal;    
> }
> if(Richtung){MOTOR(Richtung, motorSpeed);}
> //set the motor speed pwm
>   
> 
> //update the screen
> motorPercent = (motorSpeed*100)/255;
> if (motorPercent != lastPercent){
> //if( motorSpeed >= lastSpeed*0.01)||(motorSpeed <= lastSpeed*0.01){
> lcd.setCursor(0, 1);
> lcd.print(" ");
> lcd.setCursor(0, 1);
> lcd.print( motorPercent);
> lastPercent = motorPercent;
> //}
> }
>   
> if (turnsSinceUpdate > 0) {
> if(digitalRead(CCW_PIN) == 0){
> turnCount += turnsSinceUpdate;
> } else {
> turnCount -= turnsSinceUpdate;
> }
>     
> turnsSinceUpdate = 0;
> 
> lcd.setCursor(LCD_COLUMNS / 2, 1);
> lcd.print(turnCount);
> }
> 
> Serial.print("Motor speed: ");
> Serial.print(motorSpeed);
> Serial.print(",Count: ");
> Serial.print(turnCount);
> Serial.print(",run state: ");
> Serial.println(runState);
> }
> void MOTOR(uint8_t uds,uint8_t pw){
> switch (uds) {
> case 0: // STOP
> analogWrite(LPWM, 0);
> analogWrite(RPWM, 0);
> break;
> case 1: // UP
> analogWrite(LPWM, 0);
> delayMicroseconds(100);
> analogWrite(RPWM, pw);
> break;
> case 2: // DOWN
> analogWrite(RPWM, 0);
> delayMicroseconds(100);
> analogWrite(LPWM, pw);
> break;
> }
> }
> 
> ```

Achtung ! Enabel Jumper auf L298 wieder setzen.  
IO 10 (NANO) auf IN1 L298  
IO 11 (NANO) auf IN2 L298  
Denke bitte beim Testen daran den Poti wieder auf 0 zu stellen bevor Du die Richtung umschaltest. Das ist ja noch nicht abgefangen im Prog aber zum Testen reicht es ersteinmal.  
Später würde ich noch den Schleifer vom Poti mit einem Elko 10 uF versehen.

 ![grafik](https://europe1.discourse-cdn.com/arduino/original/4X/b/c/7/bc73f1684a672430dcd1aab5dbbec6a02466ed7f.jpeg)

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [January 6, 2025, 6:27am UTC](https://forum.arduino.cc/t/newbie-problem-mit-einem-sketch/1339031/26 "2025-01-06T06:27:49Z")

</div>

> [@martin-lo](#):
>
> Kann schon sein, das Du recht hast. Ich habe das Datenblatt des ATMega noch nicht verinnerlicht. Ich weiß nur aus dem was ich so mache was geht und was nicht.

Das bedeutet du hast Programme die einwandfrei **analoge** Spannungen einlesen obwohl du auf den **analogen** Eingang

```arduino
pinMode(myPin.,INPUT);

```

angewendet hast?

Edit:  
Ich habe inzwischen dazu recherchiert. Wenn man zum Beispiel

```arduino
pinMode(A0, INPUT) ; 

```

Dann wird in der function pinMode() einfach sofort zurückgesprungen weil als erstes einmal geprüft wird ob es sich überhaupt um einen digitalen Pin handelt.

```arduino
void pinMode(uint8_t pin, uint8_t mode)
{
	uint8_t bit = digitalPinToBitMask(pin);
	uint8_t port = digitalPinToPort(pin);
	volatile uint8_t *reg, *out;

	if (port == NOT_A_PIN) return;

```

---

<div class="post-metadata">

**Author:** ![combie](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/combie/32/217369_2.png) [@combie](https://forum.arduino.cc/u/combie)\
**Post date:** [January 6, 2025, 7:25am UTC](https://forum.arduino.cc/t/newbie-problem-mit-einem-sketch/1339031/27 "2025-01-06T07:25:27Z")

</div>

> [@StefanL38](#):
>
> Ich kenne mich nicht mit den internen Details der function PinMode() nicht aus,

Schade eigentlich!  
Würde uns einige Nebelkerzen ersparen,

> [@StefanL38](#):
>
> aber ich könnte mir vorstellen, das das Aufrufen der function PinMode() diesen Pin als **digitalen** Pin konfiguriert und dann ist nix mehr mit **analoger** Spannung einlesen.

Eine rege Fantasie, du hast.

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [January 6, 2025, 7:31am UTC](https://forum.arduino.cc/t/newbie-problem-mit-einem-sketch/1339031/28 "2025-01-06T07:31:33Z")

</div>

wieder was dazugelernt. Leider nicht durch Cpombie sondern andere user.

---

<div class="post-metadata">

**Author:** ![combie](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/combie/32/217369_2.png) [@combie](https://forum.arduino.cc/u/combie)\
**Post date:** [January 6, 2025, 7:43am UTC](https://forum.arduino.cc/t/newbie-problem-mit-einem-sketch/1339031/29 "2025-01-06T07:43:33Z")

</div>

> [@StefanL38](#):
>
> Ich habe mich sogar auf die Suche gemacht.

Is klar...

---

<div class="post-metadata">

**Author:** ![combie](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/combie/32/217369_2.png) [@combie](https://forum.arduino.cc/u/combie)\
**Post date:** [January 6, 2025, 8:24am UTC](https://forum.arduino.cc/t/newbie-problem-mit-einem-sketch/1339031/30 "2025-01-06T08:24:39Z")

</div>

> [@StefanL38](#):
>
> wieder was dazugelernt.

Das falsche!

> [@StefanL38](#):
>
> ch habe inzwischen dazu recherchiert. Wenn man zum Beispiel
> 
> ```arduino
> pinMode(A0, INPUT) ; 
> 
> ```
> 
> Dann wird in der function pinMode() einfach sofort zurückgesprungen

Falsch!  
Natürlich ist A0 ein Digital Pin.  
Und natürlich kann man ihn mit pinMode konfigurieren.

---

<div class="post-metadata">

**Author:** ![cpt\_v](https://avatars.discourse-cdn.com/v4/letter/c/5fc32e/32.png) [@cpt\_v](https://forum.arduino.cc/u/cpt_v)\
**Post date:** [January 6, 2025, 8:31am UTC](https://forum.arduino.cc/t/newbie-problem-mit-einem-sketch/1339031/31 "2025-01-06T08:31:43Z")

</div>

A0 ist ein Digital PIN? Beim Arduino? Also ich behapute jetzt mal, das ist ein analog-PIN den man als digtal konfigurieren kann und nicht anders herum... Aber, kann mich auch irren, beim Teensy ist das fast schon egal welchen IO du nimmst. 😄

---

<div class="post-metadata">

**Author:** ![combie](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/combie/32/217369_2.png) [@combie](https://forum.arduino.cc/u/combie)\
**Post date:** [January 6, 2025, 8:40am UTC](https://forum.arduino.cc/t/newbie-problem-mit-einem-sketch/1339031/32 "2025-01-06T08:40:44Z")

</div>

> [@cpt\_v](#):
>
> Also ich behapute jetzt mal, das ist ein analog-PIN den man als digtal konfigurieren kann und nicht anders herum

Ein ganz normaler Digital Pin, den man auch analog nutzen kann.  
Auch gerne gleichzeitig (nur nicht mit analogRead())

Nachtrag;  
A0 ist beim Nano Digital Pin 14  
Und damit ist

> [@StefanL38](#):
>
> `if (port == NOT_A_PIN) return;`

false, also kein unmittelbares return.

---

<div class="post-metadata">

**Author:** ![bertl79](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bertl79/32/1126363_2.png) [@bertl79](https://forum.arduino.cc/u/bertl79)\
**Post date:** [January 6, 2025, 9:34am UTC](https://forum.arduino.cc/t/newbie-problem-mit-einem-sketch/1339031/33 "2025-01-06T09:34:17Z")

</div>

Jetzt bin ich maximal verwirrt 😅

---

<div class="post-metadata">

**Author:** ![combie](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/combie/32/217369_2.png) [@combie](https://forum.arduino.cc/u/combie)\
**Post date:** [January 6, 2025, 9:36am UTC](https://forum.arduino.cc/t/newbie-problem-mit-einem-sketch/1339031/34 "2025-01-06T09:36:48Z")

</div>

Wo ist deine Verwirrung?

---

<div class="post-metadata">

**Author:** ![bertl79](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bertl79/32/1126363_2.png) [@bertl79](https://forum.arduino.cc/u/bertl79)\
**Post date:** [January 6, 2025, 10:04am UTC](https://forum.arduino.cc/t/newbie-problem-mit-einem-sketch/1339031/35 "2025-01-06T10:04:22Z")

</div>

Wegen der Beschaltung der H-Brücke hab ich mich natürlich auch schlau gemacht, weil ich dachte dass da evtl. was falsch ist.. aber alle schreiben das gleiche, wie z.B. hier:

[https://dj.liveblog365.com/controlling-dc-motor-speed-and-direction-with-arduino-nano-and-l298n-h-bridge-motor-driver/?i=1](https://dj.liveblog365.com/controlling-dc-motor-speed-and-direction-with-arduino-nano-and-l298n-h-bridge-motor-driver/?i=1)

Und da wird auch der ENA über einen PWM Pin angesteuert.

Also wie ich das sehe ist alles korrekt angeschlossen. Und der Motor dreht ja auch und wechselt je nach Schalterstellung die Richtung.

Wie man oben in einem meiner Beiträge auch sehen kann, wertet die Software das Poti Signal ja sogar korrekt aus, nur kommt es eben am Motor nicht so an und auch das Display zeigt ab 100 nur Murks 🤔

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [January 6, 2025, 10:11am UTC](https://forum.arduino.cc/t/newbie-problem-mit-einem-sketch/1339031/36 "2025-01-06T10:11:12Z")

</div>

> [@cpt\_v](#):
>
> A0 ist ein Digital PIN? Beim Arduino? Also ich behapute jetzt mal, das ist ein analog-PIN den man als digtal konfigurieren kann und nicht anders herum...

Also im englischen Forum gibt es user die bedeutend detaillierter erklären (als manch deutscher User je Lust drauf hätte)

> **ist englisch deshalb nur ausklappen wenn man von englischen Wörtern keine Augenschmerzen bekommt**
>
> [In which file can I find the declaration of function pinMode() ? - #7 by GolamMostafa](https://forum.arduino.cc/t/in-which-file-can-i-find-the-declaration-of-function-pinmode/1339512/7)

---

<div class="post-metadata">

**Author:** ![cpt\_v](https://avatars.discourse-cdn.com/v4/letter/c/5fc32e/32.png) [@cpt\_v](https://forum.arduino.cc/u/cpt_v)\
**Post date:** [January 6, 2025, 10:14am UTC](https://forum.arduino.cc/t/newbie-problem-mit-einem-sketch/1339031/37 "2025-01-06T10:14:02Z")

</div>

> Also im englischen Forum gibt es user die bedeutend detaillierter erklären (als manch deutscher User je Lust drauf hätte)

Na ja, nobody's perfect, hm? 😉

---

<div class="post-metadata">

**Author:** ![combie](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/combie/32/217369_2.png) [@combie](https://forum.arduino.cc/u/combie)\
**Post date:** [January 6, 2025, 10:41am UTC](https://forum.arduino.cc/t/newbie-problem-mit-einem-sketch/1339031/38 "2025-01-06T10:41:02Z")

</div>

> [@StefanL38](#):
>
> Also im englischen Forum gibt es user die bedeutend detaillierter erklären (als manch deutscher User je Lust drauf hätte)

Das ist völlig ok, wenn man dir da die Windeln wechselt, habe ich nichts gegen, wenn du selber die Doku, Datenblätter und die Quelltexte nicht lesen willst.  
Ich lese sie dir nicht vor.  
Nenne mich ja nicht ohne Grund: DokuVorLeseVerweigerer

Wenn du allerdings hier Nebelkerzen verbreitest, die Unwahrheit schreibst, werde ich dir das nicht unkommentiert durchgehen lassen.  
Und genau das, hast du in diesem Thread min. 2 mal geschafft.  
Von mir aus darfst du dich so oft blamieren wie du möchtest, aber meine MitForenUser verarschen, das ist nicht drin.

---

<div class="post-metadata">

**Author:** ![bertl79](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bertl79/32/1126363_2.png) [@bertl79](https://forum.arduino.cc/u/bertl79)\
**Post date:** [January 6, 2025, 11:17am UTC](https://forum.arduino.cc/t/newbie-problem-mit-einem-sketch/1339031/39 "2025-01-06T11:17:58Z")

</div>

Ich habe jetzt mal das Wickelrad mit dem Magneten an den Motor montiert, dass er auch mit Zählt.. also: er Zählt bis zu einer Geschwindigkeit von (angezeigt) 40 zuverlässig mit, danach nurnoch sporadisch bis garnicht mehr (wäre mir egal). Der Motor erreicht seine Maximalgeschwindigkeit bei einer Anzeige von ca. 100, was wohl auch den 1000 u/min enspricht, das ist ca. Eine viertel umdrehung am Poti.  
Dann geht das spiel für die nächste viertel Umdrehung von vorne los.. usw., nur dass dann eben die Geschwindigkeitsanzeige spinnt.  
Kann ich das evtl. mit einem Anderen Poti lösen?

Oder kann es sein dass der Motor kacke ist?

---

<div class="post-metadata">

**Author:** ![my\_xy\_projekt](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/my_xy_projekt/32/810062_2.png) [@my\_xy\_projekt](https://forum.arduino.cc/u/my_xy_projekt)\
**Post date:** [January 6, 2025, 11:28am UTC](https://forum.arduino.cc/t/newbie-problem-mit-einem-sketch/1339031/40 "2025-01-06T11:28:52Z")

</div>

Ins Blaue hinein:  
PWM geht bis 255.  
AnalogRead gibt Dir am oberen Anschlag 1024 zurück.  
Kommt es zum Überlauf, fängt alles von vorn an.

Das heisst, Du müsstest 4 unterschiedliche Stellungen haben, bei denen sich der Motor gleich (mit maximaler Drehzahl) bewegt.  
Richtig?

[Previous page](https://forum.arduino.cc/t/newbie-problem-mit-einem-sketch/1339031.md?page=1)

[Next page](https://forum.arduino.cc/t/newbie-problem-mit-einem-sketch/1339031.md?page=3)
