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?
//
}
}
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?
//
}
}
//#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?
//
}
}
//#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?
//
}
}
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.
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.
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
}
}