voidsetup(){ pinMode(enA, OUTPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(button, INPUT); // Set initial rotation direction digitalWrite(in1, LOW); digitalWrite(in2, HIGH); }
voidloop(){ // Read potentiometer value int potValue = analogRead(A0); // Map the potentiometer value from 0 to 255 int pwmOutput = map(potValue, 0, 1023, 0 , 255); // Send PWM signal to L298N Enable pin analogWrite(enA, pwmOutput);
// Read button - Debounce if (digitalRead(button) == true) { pressed = !pressed; } while (digitalRead(button) == true); delay(20);
// If button is pressed - change rotation direction if (pressed == true & rotDirection == 0) { digitalWrite(in1, HIGH); digitalWrite(in2, LOW); rotDirection = 1; delay(20); } // If button is pressed - change rotation direction if (pressed == false & rotDirection == 1) { digitalWrite(in1, LOW); digitalWrite(in2, HIGH); rotDirection = 0; delay(20); } }