Potentiometer Troubleshooting

Hello, I apologize if this is breaking any of the forum's rules, please let me know if I am and I will change things. I am working on a simple project. The goal is to get 6 LEDs to light up in a bilateral way, so every .5 seconds the LEDs will switch (for example the first 3 will turn on then after .5 seconds the other 3 will turn on, and this loop will run forever). The other thing I want to do is while this loop is running I want to be able to change the brightness of the LEDs using a potentiometer. Here is the code so far:

#define LED_1_PIN 5
#define LED_2_PIN 6
#define LED_3_PIN 3
#define LED_4_PIN 10
#define LED_5_PIN 8
#define LED_6_PIN 11
const int potPin = A0;
int readValue = 0;
int writeValue;
int brightness = 0;
int motorState = 0;

int inputVal = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(potPin, INPUT);
  pinMode(LED_1_PIN, OUTPUT);
  pinMode(LED_2_PIN, OUTPUT);
  pinMode(LED_3_PIN, OUTPUT);
  pinMode(LED_4_PIN, OUTPUT);
  pinMode(LED_5_PIN, OUTPUT);
  pinMode(LED_6_PIN, OUTPUT);
}

void Potentiometer()
{
  readValue = analogRead(potPin);
  brightness = (255./1023.) * readValue;
  if (readValue > 0) 
  {
    vibrate();
  }
}

void vibrate()
{
  if(motorState == 0) {
    analogWrite(LED_1_PIN, brightness);
    analogWrite(LED_2_PIN, brightness);
    analogWrite(LED_3_PIN, brightness);
    analogWrite(LED_4_PIN, 0);
    analogWrite(LED_5_PIN, 0);
    analogWrite(LED_6_PIN, 0);

    motorState = motorState + 1;
  }
  else if(motorState == 1)
  {
    analogWrite(LED_1_PIN, 0);
    analogWrite(LED_2_PIN, 0);
    analogWrite(LED_3_PIN, 0);
    analogWrite(LED_4_PIN, brightness);
    analogWrite(LED_5_PIN, brightness);
    analogWrite(LED_6_PIN, brightness);

    motorState = 0;
  }
  //analogWrite(LED_1_PIN, brightness);
  //analogWrite(LED_2_PIN, brightness);
  //analogWrite(LED_3_PIN, brightness);
  //analogWrite(LED_4_PIN, brightness);
  //analogWrite(LED_5_PIN, brightness);
  //analogWrite(LED_6_PIN, brightness);
}

void loop()
{
  Potentiometer();
  /*
  digitalWrite(LED_1_PIN, 1023);
  digitalWrite(LED_2_PIN, LOW);
  digitalWrite(LED_3_PIN, 1023);
  digitalWrite(LED_4_PIN, LOW);
  digitalWrite(LED_5_PIN, 500);
  delay(400);
  digitalWrite(LED_1_PIN, LOW);
  digitalWrite(LED_2_PIN, HIGH);
  digitalWrite(LED_3_PIN, LOW);
  digitalWrite(LED_4_PIN, 500);
  digitalWrite(LED_5_PIN, 200);
  delay(400);
  digitalWrite(LED_1_PIN, 500);
  digitalWrite(LED_2_PIN, 200);
  digitalWrite(LED_3_PIN, 0);
  digitalWrite(LED_4_PIN, 500);
  digitalWrite(LED_5_PIN, 0);
  delay(400);
  */
}

I am very new at programming so any and all help would be great. Thank you!

The what?

Maybe @gracep means the speed of the LEDs

Sorry that was a typo. I meant the LEDs.

Your code has the delay()s commented out which is just as well because each delay() causes the program to stop and do nothing else until the delay() ends

You need to implement non blocking timing

See Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

You didn't implement this part. Perhaps what you want is:

void loop()
{
  static unsigned long previousMillis = 0;
  if (millis() - previousMillis >= 500)
  {
    previousMillis += 500;
   Potentiometer();
  }
}

Code with an easy to use non-blocking timer
This demo-code shows the non-blocking character through counting up a variable as fast as the loop runs down

unsigned long DemoTimer      = 0; // variables that are used to store values of function millis()
unsigned long DemoTimerTwo   = 0; // the must be of type unsigned long to work properly all the time
unsigned long DemoTimerThree = 0;
unsigned long DoDelayTimer   = 0;

unsigned long myCounter = 0;

// helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &expireTime, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - expireTime >= TimePeriod )
  {
    expireTime = currentMillis; // set new expireTime
    return true;                // more time than TimePeriod) has elapsed since last time if-condition was true
  }
  else return false;            // not expired
}


void setup() {
  Serial.begin(115200);
  Serial.println("Program started activate Show timestamp in serial monitor");
  Serial.println("maximise window of serial monitor");
  Serial.println("to see the the messages in full.......................................................length");
}


void myDemofunction_1() {
  Serial.println("once per second Huhu ! time for Action A ");
  Serial.print("myCounter=");
  Serial.println(myCounter);
}

void myDemoFuncB() {
  Serial.println("once every 3 seconds Hi there                      time for Action B once every 3 seconds");
}

void my_Demo_function_3() {
  Serial.print("once every 5 seconds ready now");
  for ( int i = 0; i< 40; i++) {
    Serial.print(".");
  }  
  Serial.println("time for Action C once every 5 seconds");
}


void loop() {
  myCounter++; // count up very fast to demonstrate the non-blocking character
  if (  TimePeriodIsOver(DemoTimer, 1000)  ) {
    myDemofunction_1();
  }


  if (  TimePeriodIsOver(DemoTimerTwo, 3000)  ) {
    myDemoFuncB();
  }

  if (  TimePeriodIsOver(DemoTimerThree, 5000)  ) {
    my_Demo_function_3();
  }

  // show the effect of BLOCKING timing caused by function delay()
  if (  TimePeriodIsOver(DoDelayTimer, 20000)  ) {
    Serial.println("every 20 seconds execute delay(5500)... to make all other timers overdue");
    
    Serial.print("value of myCounter right before delay =");
    Serial.println(myCounter);
    
    delay(5500);
    
    Serial.print("value of myCounter right AFTER delay =");
    Serial.println(myCounter);

    Serial.println("as delay(5500 has BLOCKED code-execution all three timers are overdue");
    Serial.println("which means all three timers fire in the SAME microsecond one after the other ");
  }
}

/*
  the basic principle of non-blocking timing is to check if a defined timeinterval
  has passed by.

  This can be done by using the function millis()
  The function millis()gives back the amount of milliseconds (hence the name millis)
  that have passed by since power-up of the microcontroller.
  It counts up to 2^32 which means reaching the max-value is reached after 49 days.
  There is a calculation-technique that even "rollover" from max to zero is handled
  automatically the right way

  This non-blocking timing needs a timer-variable which is used for taking
  snapshots of time as a comparison-point

  The variable-type for this variable MUST be of type unsigend long
  to make it work reliably all the time

  unsigned long myLcdUpdateTimer;

  now the following construction executes the code inside the if-condition
  only once every two seconds

  if ( TimePeriodIsOver(myLcdUpdateTimer,2000) ) {
     // time for timed action
  }

additionally the code demonstrates how you can code your own functions
and how to call/execute them.
The names of the functions are a bit lengthy to demonstrate which names 
inside the code you can choose freely and to demonstrate where really a relation
between names is and where NO relation between names is
*/

best regards Stefan

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