Bitshift and write to port D

hi guys,

I would like some help to create a repeating pattern with 6 leds.

I have Six LEDs connected in a ring to pins 4,5,6,7,8,9 of a nano.

I would like to set up a pattern in binary and write it to the pins and cycle it through endlessly.

I need the function to work without using delay and I would like to do something smart with bit shift and operations but I am struggling to get started and understand how I mask the upper 2 bits as I am not using them as output pins.

This is the first pattern I want to achieve

000001
000010
000100
001000
010000
100000

and here is the code I have so far:




#define REDLED_1 8
#define REDLED_2 7
#define REDLED_3 6
#define REDLED_4 5
#define REDLED_5 4
#define REDLED_6 9

#define ON_time 30
#define OFF_time 5




unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 1000;           // interval at which to blink (milliseconds)

int Pattern1 = B000000001;

byte    pins[2, 3, 4, 5, 6, 7, 8, 9];
byte*   p = pins;


void setup() {

  pinMode(REDLED_1, OUTPUT);
  pinMode(REDLED_2, OUTPUT);
  pinMode(REDLED_3, OUTPUT);
  pinMode(REDLED_4, OUTPUT);
  pinMode(REDLED_5, OUTPUT);
  pinMode(REDLED_6, OUTPUT);
}


void loop() {
unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    ENGINE_SPIN();
  }
}

void ENGINE_SPIN(){
  for (pins[i]=0;i<=6;i++){
    digitalWrite(pins[i], Pattern1 && B00111111); // not sure how to get the correct bit from Pattern1
    //bitshift >> // not sure how to shift the bits on pattern1 and make it roll over from MSB to LSB again?
    //
    }  
  }





Any help would be greatly appreciated.

You can add a "=" if you wish

I'm not clear why you don't use the pins array here

REDLED_X is just remnants of test code to make sure the leds are wired correctly.




//#define REDLED_1 8
//#define REDLED_2 7
//#define REDLED_3 6
//#define REDLED_4 5
//#define REDLED_5 4
//#define REDLED_6 9

#define ON_time 30
#define OFF_time 5


unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 1000;           // interval at which to blink (milliseconds)

int Pattern1 = B000000001;

byte    pins[6] = {2, 3, 4, 5, 6, 7, 8, 9};
byte*   p = pins;


void setup() {

  pinMode(pins[1], OUTPUT);
  pinMode(pins[2], OUTPUT);
  pinMode(pins[3], OUTPUT);
  pinMode(pins[4], OUTPUT);
  pinMode(pins[5], OUTPUT);
  pinMode(pins[6], OUTPUT);
}


void loop() {
unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    ENGINE_SPIN();
  }
}

void ENGINE_SPIN(){
  for (pins[i]=0;i<=6;i++){
    digitalWrite(pins[i], Pattern1 && B00111111); // not sure how to get the correct bit from Pattern1
    //bitshift >> // not sure how to shift the bits on pattern1 and make it roll over from MSB to LSB again?
    //
    }  
  }





Double oops

thanks for the help...

"Pattern1" is non-zero, therefore true.
B00111111` is clearly non-zero and so also true.

I think you meant & not &&.

But they're both constants, so I'm not sure why you'd want that.

I figure I need to mask out the upper 2 bits so I am only affecting the 6 outputs I want to control. so yes I have made a mistake and need to AND them

and Pattern 1 is what I want to bit shift left or right to cycle the pattern.

Have you fixed the array?

yes I have fixed the array thanks.




//#define REDLED_1 8
//#define REDLED_2 7
//#define REDLED_3 6
//#define REDLED_4 5
//#define REDLED_5 4
//#define REDLED_6 9

#define ON_time 30
#define OFF_time 5


unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 1000;           // interval at which to blink (milliseconds)

int Pattern1 = B000000001;

byte    pins[6] = {4, 5, 6, 7, 8, 9};
byte*   p = pins;


void setup() {

  pinMode(pins[1], OUTPUT);
  pinMode(pins[2], OUTPUT);
  pinMode(pins[3], OUTPUT);
  pinMode(pins[4], OUTPUT);
  pinMode(pins[5], OUTPUT);
  pinMode(pins[6], OUTPUT);
}


void loop() {
unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    ENGINE_SPIN();
  }
}

void ENGINE_SPIN(){
  for (pins[i]=0;i<=6;i++){
    digitalWrite(pins[i], Pattern1 & B00111111); // not sure how to get the correct bit from Pattern1
    //bitshift >> // not sure how to shift the bits on pattern1 and make it roll over from MSB to LSB again?
    //
    }  
  }





So, that's a "no" then.

Can you talk us through what you think ENGINE_SPIN should do, please?
There seem to be a ton of misunderstanding in there.

(All caps are usually reserved for macros)

What have I done wrong with the pins array?

I have renamed ENGINE_SPIN to Cycle_6_Leds but i want it to start with the contents of Pattern1 and cycle outputs every interval.

000001
000010
000100
001000
010000
100000




//#define REDLED_1 8
//#define REDLED_2 7
//#define REDLED_3 6
//#define REDLED_4 5
//#define REDLED_5 4
//#define REDLED_6 9

#define ON_time 30
#define OFF_time 5


unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 1000;           // interval at which to blink (milliseconds)

int Pattern1 = B000000001;

byte    pins[6] = {4, 5, 6, 7, 8, 9};
byte*   p = pins;


void setup() {

  pinMode(pins[0], OUTPUT);
  pinMode(pins[1], OUTPUT);
  pinMode(pins[2], OUTPUT);
  pinMode(pins[3], OUTPUT);
  pinMode(pins[4], OUTPUT);
  pinMode(pins[5], OUTPUT);
}


void loop() {
unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    Cycle_6_Leds();
  }
}

void Cycle_6_Leds(){
  for (pins[i]=0;i<=6;i++){
    digitalWrite(pins[i], Pattern1 & B00111111); // not sure how to get the correct bit from Pattern1
    //bitshift >> // not sure how to shift the bits on pattern1 and make it roll over from MSB to LSB again?
    //
    }  
  }





Changing the name doesn't decrease the amount of misunderstanding.
Please talk us through it

  pinMode(pins[0], OUTPUT);

A for loop here would reduce the amount of duplicated code

Telling people they have made mistakes and not explaining doesn't help either.

It gives you a chance to review your code and ask questions about what you think the problem might be, or fix the problem yourself without feeling patronised or dumb.

No, the way you have written everything is patronising.

Patronising examples are "clearly" and "that's a no then" and "have you fixed"

Examples of helpful non patronising sentences are "there is a problem with your array.. you should look at... to fix it"

OK, I can see you'd rather have a meta-argument than fix your code (I still can't see where PORTD comes into the problem), so I'll leave you to get on with it.

Good luck.

Dude you are the one being derogatory and unhelpful.

meanwhile I have been trying to fix my code and write psudocode to better explain what i am trying to do.




//#define REDLED_1 8
//#define REDLED_2 7
//#define REDLED_3 6
//#define REDLED_4 5
//#define REDLED_5 4
//#define REDLED_6 9

#define ON_time 30
#define OFF_time 5


unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 1000;           // interval at which to blink (milliseconds)

int Pattern1 = B000000001;

byte    pins[6] = {4, 5, 6, 7, 8, 9};
byte*   p = pins;


void setup() {

  pinMode(pins[0], OUTPUT);
  pinMode(pins[1], OUTPUT);
  pinMode(pins[2], OUTPUT);
  pinMode(pins[3], OUTPUT);
  pinMode(pins[4], OUTPUT);
  pinMode(pins[5], OUTPUT);
}


void loop() {
unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    Cycle_6_Leds();
  }
}

void Cycle_6_Leds(){
 //turn on LED at pins[0] //000001   - pattern1 000001 - pattern2 0000101
 // bit shift left 
 //turn on LED at pins[1] //000010
 // bit shift left 
 //turn on LED at pins[2] //000100
 // bit shift left 
 //turn on LED at pins[3] //001000
 // bit shift left 
 //turn on LED at pins[4] //010000
 // bit shift left 
 //turn on LED at pins[5] //100000
 // overflow bit and start at pins[0] again 
    }  
  }





void Cycle_6_Leds(){
  static byte led = 0;
  digitalWrite(pins[led], LOW);
  if (++led >= sizeof(pins)/sizeof(pins[0])) led = 0;
  digitalWrite(pins[led], HIGH);
}