Running scanning leds and a pot controlled servo programming issue.

Hi
I am all new to this programming language and unfortunately the only work i have done in the past (a loooooooooong time ago) was with Amiga basic.
I have got my Arduino uno setup with a 5 LED scanner (left to right and return) and a servo connected controlled by a potentiometer. The program is set so the delay is not in the void setup but the void loop as to i assumed avoid a delay at the servo when i moved the potentiometer but that has not worked.
I tried moving the code to the top of the loop section so the servo was first but that just changes where in the sequence it moves. Not sure how to sort it so i thought i would put it up on here and see who could teach me the error of my ways so i can continue to make these sort of experiments.
here is a copy of the code i currently am running…

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
 
int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 
int pin2 = 2;
int pin3 = 3;
int pin4 = 4;
int pin5 = 5;
int pin6 = 6;


void setup(){
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
  pinMode(pin4, OUTPUT);
  pinMode(pin5, OUTPUT);
  pinMode(pin6, OUTPUT);
  myservo.attach(9);
}

void loop() {
   val = analogRead(potpin);
   val = map(val, 0, 1023, 0, 180);
   myservo.write(val); 

  digitalWrite(pin2, HIGH);
   delay(100);
   digitalWrite(pin2, LOW);
   delay(100);

   digitalWrite(pin3, HIGH);
   delay(100);
   digitalWrite(pin3, LOW);
   delay(100);

   digitalWrite(pin4, HIGH);
   delay(100);
   digitalWrite(pin4, LOW);
   delay(100);
   
   digitalWrite(pin5, HIGH);
   delay(100);
   digitalWrite(pin5, LOW);
   delay(100);

   digitalWrite(pin6, HIGH);
   delay(100);
   digitalWrite(pin6, LOW);
   delay(100);  
   
   digitalWrite(pin5, HIGH);
   delay(100);
   digitalWrite(pin5, LOW);
   delay(100); 
  
  digitalWrite(pin4, HIGH);
   delay(100);
   digitalWrite(pin4, LOW);
   delay(100); 
 
 digitalWrite(pin3, HIGH);
   delay(100);
   digitalWrite(pin3, LOW);
   delay(100); 

 
   

}

knightrider_scan_plus_servo_play.ino (1.25 KB)

You need to get rid of all those delays in the program because whilst each of them is happening no other part of the program will run. You have 16 delays of 100 milliseconds each between each read of the pot so the reaction to moving the pot will only happen at an interval of 1.6 seconds in the worst case. This will happen no matter where in the program the pot value is read.

So, what to do ? Look at the BlinkWithoutDelay example in the IDE. It will show you how to use millis() to decide whether it is time to do something yet, like turn an LED on or off, and if not go and do something else like reading a pot an moving a servo.

Basically, save the start time of an action (say LED on) then every time through loop() check whether the appropriate time period has elapsed. If so then start the next action (say LED off), save the start time and off you go again. If the period has not elapsed then read the pot and move the servo if necessary.

More advice.
Put the code in [code] tags when posting here to make it easier to read.

If you are going to the trouble of giving pins names then make the names meaningful rather than just naming pin 2 pin2 etc

Note that there is a pattern to the pin numbers in your sequences. Consider using a loop to iterate through them. Even if the pin numbers were not in a sequence you could put them in an array and iterate through that. Either way will make your code much more compact.

DIESELMIKE:
I look forward to your reply’s

so here is an approach you can try to learn. I set your led pins into an array to make it easier to give them all instructions (e.g. in a for() loop).

Then, I created a BYTE array called mask that contains the ‘pattern’ of lit LEDs that I want to follow, arranged in order. you can see that here:

byte mask[5] = {0b00001, 0b00010, 0b00100, 0b01000, 0b10000};

Then created a loop that will set the LED state for each led every time the millis() timer expires:

if (millis() - lastBlinkTime >= 100UL)

I used the bitRead() function to look at each position and return the one (HIGH) or 0 (LOW) for each led based on what bit returns from the bytes we created in mask():.

bitRead(mask[index], i)

Changing the index, advances you to the next mask in the array, so you are looking at the next mask the next time you execute the timer.

compiles but not tested…

#include <Servo.h> 

Servo myservo;  // create servo object to control a servo 

int potpin = 0;  // analog pin used to connect the potentiometer
int lastVal;    // variable to read the value from the analog pin 
int ledPin[5] = {2,3,4,5,6};
byte mask[5] = {0b00001, 0b00010, 0b00100, 0b01000, 0b10000};

//
void setup()
{
  for (int i = 0; i < 5; i++)
  {
    pinMode(ledPin[i], OUTPUT);
  }
  myservo.attach(9);
}

void loop() 
{
  int val = analogRead(potpin);
  if (val != lastVal)// <<<<<<<<<<<< I also set this to move the servo only on a change in val
  {
    myservo.write(map(val, 0, 1023, 0, 180)); 
  }
  lastVal = val;
  knightRider();
}

void knightRider()
{
  static unsigned long lastBlinkTime;
  static byte index = 0;
  static  byte increment = 1;
  if (millis() - lastBlinkTime >= 100UL)//<<<<<< your flash interval
  {
    for (byte i = 0; i < 5; i++)
    {
      digitalWrite(ledPin[i], bitRead(mask[index], i));
    }
    index += increment;
    if (index == 0 || index == 4) increment = -increment;
    lastBlinkTime += 100UL;
  }
}

if you learn this (bitRead and BlinkWithoutDelay) you can create some very nice/fun LED patterns. Hook up 8 LEDs to get the max out of bitRead() and set up your mask array like this:

byte mask[8] = {0b00000001, 0b00000010, 0b00000100, 0b00001000, 0b00010000, 0b00100000, 0b01000000, 0b10000000};

or any pattern you which to cycle through.

Thanks I have it sorted now and have some better code to play with :D