Led and motor work strangely

Hi, I have a question about the rotary encoder. I have no idea why my rotary encoder is not changing the LED brightness. I would greatly appreciate any help as I am a noob.

Thanks in advance.

Here is part of my code:

//Include the encoder library
#include <Encoder.h>
Encoder myEnc(6,5);    //define encoder pin; (OutA, OutB)
 
long int oldPosition = -999;
long int newPosition; 
int positionDifference = 0;
int ledBrightness = 0; 

void setup(){
  Serial.begin(9600);
} //end of setup()

void loop(){
  newPosition = myEnc.read();
  if (newPosition != oldPosition){
    positionDifference = newPosition - oldPosition;
    oldPosition = newPosition;
  }
  ledBrightness = ledBrightness + positionDifference; 
  ledBrightness = constrain(ledBrightness, 0, 30);
  analogWrite(ledPin, ledBrightness);
} //end of loop()

Here is the whole code:

//Include the Arduino Stepper.h library
#include <Stepper.h>

//Include the encoder library
#include <Encoder.h>
Encoder myEnc(6,5);    //define encoder pin; (OutA, OutB)



//define the pin
const int buttonPin = 2;     //pushbutton1
const int ledPin = 3;        //12v led
const int touchPin = 4;      //capacitive touch sensor
const int wakePin = 12;      //pushbutton2
const int stepsPerRevolution = 2038;   //for stepper motor
Stepper myStepper = Stepper(stepsPerRevolution, 8, 10, 9, 11);  //create stepper object called'myStepper'



//variables will change
int buttonState = 0;
int touchState = 0;
int wakeState = 0;
long int oldPosition = -999;  //encoder old position
long int newPosition;         //encoder new position
int positionDifference = 0;
int ledBrightness = 0; 



//setup
void setup(){
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(touchPin, INPUT);
  pinMode(wakePin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
} //end of setup()



//loop
void loop(){
  //read the state of the parts
  buttonState = digitalRead(buttonPin);   
  touchState = digitalRead(touchPin);
  wakeState = digitalRead(wakePin);



  //use encoder to change the brightness
  newPosition = myEnc.read();
  if (newPosition != oldPosition){
    positionDifference = newPosition - oldPosition;
    oldPosition = newPosition;
    } //end of if newposition
  
  ledBrightness = ledBrightness + positionDifference; 
  ledBrightness = constrain(ledBrightness, 0, 30);
  analogWrite(ledPin, ledBrightness);



  //use two buttons and sensor to control the motor and led
  if (buttonState == LOW){
    
    if (touchState == HIGH){                                            
      myStepper.setSpeed(15);  
      myStepper.step(stepsPerRevolution/8);  
    } else{
        if (wakeState == LOW){                                          
          myStepper.setSpeed(5);  
          myStepper.step(-stepsPerRevolution/8);
        } else{                                                         
          analogWrite(ledPin, LOW);
          }//end of wakeState LOW
      } //end of touchState HIGH
      
  } else{
    
    if (touchState == HIGH){                                            
      myStepper.setSpeed(10);  
      myStepper.step(stepsPerRevolution/8); 
    } else{                                                             
          myStepper.setSpeed(2);
          myStepper.step(-stepsPerRevolution/8); 
      } //end of touchState HIGH
      
  } //end of buttonState LOW

  
} //end of loop()

why do you constraint the PWM value to max 30?

1 Like

Could this line be the reason?

1 Like

Becuase I am doing the sleep lamp. I hope the maximum brightness is limited so that it won't be way too bright.

I was planning to use two pushbutton&touch sensor to control the motor&led in the if loop. I want the LED is LOW when buttonState = LOW & touchSensor = LOW & WakeState = HIGH. That's why I had analogWrite(ledPin, LOW);

Do you suggest changing this? Or should I add analogWrite(ledPin, HIGH)` in the other places of the if loop?

Thanks everyone! Luckily it does change the brightness now!
In fact, it was working and I couldn't tell. Because it takes too long for led to react (about 8 seconds?). Now I reckon this may be because the heavy if section slows it down?

Yes

No

analogWrite() needs a number from 0 to 255. Not HIGH or LOW.

digitalWrite() needs HIGH or LOW.

if is not a loop.

1 Like

Thank you so much! This is really helpful

Now I am using if() statement: use two pushbutton & touch sensor to control the motor & LED.
The problem is, the reaction time of the arduino is extremely slow, and a bit randomly as well.

This was actually not that slow when testing with only serial.prinln. However, as I connect components altogether, they work strangely.

For example:

  1. LED takes about 8 seconds to change the brightness,
  2. motor reacts slowly, and also works a bit randomly (sometimes the speed&direction changed without touching anything
  3. the LED can only be turned off for less than 1 second and then turned on again (But I want it to remain off as long as the button is pressed)

Here is my code:


//Include the Arduino Stepper.h library
#include <Stepper.h>

//Include the encoder library
#include <Encoder.h>
Encoder myEnc(6,5);


//define the pin
const int buttonPin = 2;
const int ledPin = 3;
const int touchPin = 4;
const int wakePin = 12;
const int stepsPerRevolution = 2038;   
Stepper myStepper = Stepper(stepsPerRevolution, 8, 10, 9, 11);



//variables will change
int buttonState = 0;
int touchState = 0;
int wakeState = 0;
long int oldPosition = -999;  //encoder old position
long int newPosition;   //encoder new position
int positionDifference = 0;
int ledBrightness = 0; 



//setup
void setup(){
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(touchPin, INPUT);
  pinMode(wakePin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  Serial.begin(12500);
} //end of setup()



//loop
void loop(){
  //read the state of the parts
  buttonState = digitalRead(buttonPin);   
  touchState = digitalRead(touchPin);
  wakeState = digitalRead(wakePin);



  //use encoder to change the brightness
  newPosition = myEnc.read();
  if (newPosition != oldPosition){
    positionDifference = newPosition - oldPosition;
    oldPosition = newPosition;
    } //end of if newposition
  
  ledBrightness = ledBrightness + positionDifference; 
  ledBrightness = constrain(ledBrightness, 0, 30);
  analogWrite(ledPin, ledBrightness);



  //use two buttons and sensor to control the motor and led
  if (buttonState == LOW){
    
    if (touchState == HIGH){                                            
      myStepper.setSpeed(15);  
      myStepper.step(stepsPerRevolution/8);  
    } else{
        if (wakeState == LOW){                                       
          myStepper.setSpeed(5);  
          myStepper.step(-stepsPerRevolution/8);  
        } else{                                                         
          digitalWrite(ledPin, LOW);
          }//end of wakeState LOW
      } //end of touchState HIGH
      
  } else{
    
    if (touchState == HIGH){                
      myStepper.setSpeed(10);  
      myStepper.step(stepsPerRevolution/8); 
    } else{                                                       
          myStepper.setSpeed(2);
          myStepper.step(-stepsPerRevolution/8); 
      } //end of touchState HIGH
      
  } //end of buttonState LOW                                  
  
} //end of loop()

The Stepper library step() function blocks. That makes the program unresponsive. I would suggest that you use a library that has non-blocking functions like the AccelStepper library or the MobaTools stepper library ( I am finding MobaTools stepper easier to use). Both libraries are available via the IDE library manager.

1 Like

Thanks for reaching out.

I tried MotaTool Library. The motor is kinda working. I assume there're some issues with my code when using the MotaTool Library. I may need to fix up the speed.
Besides, the LED is still changing brightness slowly.

Here is my code after changing to use MotaTool Library:

//Include the Arduino Stepper.h library
#include <MobaTools.h>
const int stepsPerRev = 2048;    // Steps per revolution - may need to be adjusted
MoToStepper myStepper( stepsPerRev, STEPDIR );  // create a stepper instance

//Include the encoder library
#include <Encoder.h>
Encoder myEnc(6,5);


//define the pin
const int buttonPin = 2;
const int ledPin = 3;
const int touchPin = 4;
const int wakePin = 12;
const byte stPn[] = { 8,9,10,11 }; 



//variables will change
int buttonState = 0;
int touchState = 0;
int wakeState = 0;
long int oldPosition = -999;  //encoder old position
long int newPosition;   //encoder new position
int positionDifference = 0;
int ledBrightness = 0; 



//setup
void setup(){
  myStepper.attach( stPn[0],stPn[1],stPn[2],stPn[3] );
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(touchPin, INPUT);
  pinMode(wakePin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  Serial.begin(12500);
} //end of setup()



//loop
void loop(){
  //read the state of the parts
  buttonState = digitalRead(buttonPin);   
  touchState = digitalRead(touchPin);
  wakeState = digitalRead(wakePin);



  //use encoder to change the brightness
  newPosition = myEnc.read();
  if (newPosition != oldPosition){
    positionDifference = newPosition - oldPosition;
    oldPosition = newPosition;
    } //end of if newposition
  
  ledBrightness = ledBrightness + positionDifference; 
  ledBrightness = constrain(ledBrightness, 0, 30);
  analogWrite(ledPin, ledBrightness);



  //use two buttons and sensor to control the motor and led
  if (buttonState == LOW){
    
    if (touchState == HIGH){                                            
      myStepper.setSpeed(15);  
      myStepper.setRampLen(stepsPerRev/8);
      myStepper.rotate(1);   
    } else{
        if (wakeState == LOW){                                       
          myStepper.setSpeed(5);  
          myStepper.setRampLen(stepsPerRev/8);
          myStepper.rotate(-1);     
        } else{                                                         
          digitalWrite(ledPin, LOW);
          }//end of wakeState LOW
      } //end of touchState HIGH
      
  } else{
    
    if (touchState == HIGH){                
      myStepper.setSpeed(10);  
      myStepper.setRampLen(stepsPerRev/8);
      myStepper.rotate(1);    
    } else{                                                       
          myStepper.setSpeed(2);
          myStepper.setRampLen(stepsPerRev/8); 
          myStepper.rotate(-1);   
      } //end of touchState HIGH
      
  } //end of buttonState LOW                                  
  
} //end of loop()

@lilyyyw
You have posted 3 separate topics asking closely related if not the same question. Posting the same thing multiple times is against the forum rules as it wastes the time of the volunteers trying to help you. Repeated posting of the same thing will result in a temporary or permanent ban on posting.

I have merged 2 of your topics into this one and deleted the third as it had no replies.

Please do not ask this question again in a separate topic, if you want to ask additional questions please post them in this topic. If you have a new project with different questions feel free to start a new topic.

Please read the forum instructions How to get the best out of this forum

Thank you.

1 Like

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