Problem with my code

Hi everyone,
I have a problem occurring with my code, can someone please help me to fix this.
I am using a push button to turn on the motor, and turn off it, but I also want the motor to run at a low speed such as 200 rpm, as well as the problem in my code, I am very new to this stuff, so your help will be appreciated. @jim-p

// declare variables
const int LEDpin = 9;
const int potpin = A0;
int pot;
int speed;

void setup(){
  // don't need to use pinMode
  // with analogRead and analogWrite
}

void loop(){
  //read button state
  state = digitalRead(butpin);
  // check if button is pressed
  if(state ==HIGH){ // button not pressed
    digitalWrite(LEDpin,LOW); //turn LED off
  }
  else{  // button pressed
    digitalWrite(LEDpin,HIGH); // turn LED on
  }
}
    

well seems you use digitalRead...

and digitalWrite

what about INPUT_PULLUP and OUTPUT?

what do you mean?

Sorry, I am very new to this stuff, so I have no clue what I am doing.

Where do I add this in the code? @J-M-L

in the setup() function

spend a bit of time reading and playing with basic tutorials about handling a button and a LED to understand the various modes for the pins.

What happened to this schematic and the code?

didn't need potentiometer, to complicated.

What happend to the code that worked but the motor ran to fast?
You seem to have thrown away everything you did in your other topic.

Are you searching for something like this?

ye
Now I just need a on and off push button to turn on or off the dc motor, and set the motor speed to 200 rpm in the code. However, I need assistance in adding the set speed in my code.

// declare variables
const int LEDpin = 9;
const int potpin = A0;
int pot;
int speed;

void setup(){
  // don't need to use pinMode
  // with analogRead and analogWrite
}

void loop(){
  //read button state
  state = digitalRead(butpin);
  // check if button is pressed
  if(state ==HIGH){ // button not pressed
    digitalWrite(LEDpin,LOW); //turn LED off
  }
  else{  // button pressed
    digitalWrite(LEDpin,HIGH); // turn LED on
  }
}


@jim-p

no, but i am searching for one push button to turn on or off the dc motor, and set the motor speed to 200 rpm in the code, with diode, and NPN transistor @docdoc

If you post the code that worked but the motor ran to fast, I can help.

const int BUTTON_PIN = 2;        // Arduino pin connected to button's pin
const int POTENTIOMETER_PIN = A0; // Arduino pin connected to potentiometer pin
const int MOTOR_PIN = 9;          // Arduino PWM pin connected to motor driver

byte ledState = LOW;             // The current state of the built-in LED
byte lastButtonState;            // The previous state of the button
byte currentButtonState;         // The current state of the button

void setup()
{
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT_PULLUP);     // Set button pin to input pull-up mode
  pinMode(LED_BUILTIN, OUTPUT);          // Set LED pin to output mode
  pinMode(MOTOR_PIN, OUTPUT);            // Set motor pin to output mode

  lastButtonState = digitalRead(BUTTON_PIN); // Read the button state
}

void loop()
{
  currentButtonState = digitalRead(BUTTON_PIN); // Read the new button state

  if (lastButtonState == HIGH && currentButtonState == LOW)
  {
    Serial.println("The button is pressed");
    ledState = !ledState; // Toggle state of LED
  }
  lastButtonState = currentButtonState; // Save the last button state

  // control LED according to the toggled state
  digitalWrite(LED_BUILTIN, ledState);

  if (ledState == HIGH)
  {
    // Read the potentiometer value
    int potValue = analogRead(POTENTIOMETER_PIN);

    // Map the potentiometer value to motor speed range (0-255)
    int motorSpeed = map(potValue, 0, 1023, 0, 255);

    // Set the motor speed
    analogWrite(MOTOR_PIN, motorSpeed);
  }
  else
  {
    // Turn off the motor
    analogWrite(MOTOR_PIN, 0);
  }

  delay(50);
}

here you go @jim-p

I hope it isn't a problem for you to turn it on or off with a button...

That project sets the motor speed using a potentiometer, so you can use it to see how you can control the speed and how change it programatically.
Is it a problem for you testing that code and see how it works? Or you want us doing the things for you?

yes please, i am very confused, and stuck on what do. @jim-p @docdoc

Try this

////////////**** MOTOR SPEED*****////////////////////
// Adjust to whatever you like: 0=stop, 255=full speed
int motorSpeed = 100;           
////////////////////////////////////////////////////

const int BUTTON_PIN = 2;   // Arduino pin connected to button's pin
const int MOTOR_PIN = 9;    // Arduino PWM pin connected to motor driver

byte ledState = LOW;        // The current state of the built-in LED
byte lastButtonState;       // The previous state of the button
byte currentButtonState;    // The current state of the button


void setup()
{
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT_PULLUP);     // Set button pin to input pull-up mode
  pinMode(LED_BUILTIN, OUTPUT);          // Set LED pin to output mode
  pinMode(MOTOR_PIN, OUTPUT);            // Set motor pin to output mode

  analogWrite(MOTOR_PIN, 0);             // Stop the motor

  lastButtonState = digitalRead(BUTTON_PIN); // Read the button state
}

void loop()
{
  currentButtonState = digitalRead(BUTTON_PIN); // Read the new button state

  if (lastButtonState == HIGH && currentButtonState == LOW)
  {
    Serial.println("The button is pressed");
    ledState = !ledState; // Toggle state of LED
  }
  lastButtonState = currentButtonState; // Save the last button state

  // Control LED according to the toggled state
  digitalWrite(LED_BUILTIN, ledState);

  if (ledState == HIGH)
  {
    // Turn on motor with speed = motorSpeed
    analogWrite(MOTOR_PIN, motorSpeed);
  }
  else
  {
    // Turn off the motor
    analogWrite(MOTOR_PIN, 0);
  }

  delay(50);
}

Ok, but what about the code you have shown us on post #14, different from the first one? What you think it does? Have you tried running that code with a potentiometer attached?
If so, you just need to set the potentiometer to get the motor speed you require, and se what is the (fixed) value you need for the analogWrite() statement. to get that value, just print it on the Serial and see the value suitable for you the remove the potentiometer readings and set that constant value to motorSpeed variable.

PS: to make things easier for you and us, especially when dealing with some code and "standard" devices, I always recommend using a simulator like "Wokwi". You can draw your project (good to copy/paset it here), test it, and even share the project link so as we can copy it on our Wokwi, make more tests, and even share you back to see the changes!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.