Hi All
My first post here. I'm completely new to programming and need a little help please.
A brief synopsis of what I'm trying to do:
I would like to flash a number of patterns on a range of LED's currently six.
I would like the patterns to change on the push of a single button.
Once selected I would like the pattern to loop until the next button push.
I would like a potentiometer to vary the speed that the pattern runs at as it is running.
basically a simple set of fairy lights!
I've got a few different codes from the web and that I have written.
They either work separately but I cant combine the features I want into one code or they partialy work with some of the functionality I want.
This was my first attempt on my own its very simple. the patterns change on the push of the button but only flash once!
int buttonPin = 13; // set up pinn 13 as button pin
int timer = 75; // The higher the number, the slower the timing.
int state = 0;
void setup()
{
// use a for loop to initialize each pin as an output:
for (int thisPin = 5; thisPin < 13; thisPin++)
{
pinMode(thisPin, OUTPUT);
pinMode (buttonPin, INPUT);
}
}
void loop()
{
if (digitalRead(buttonPin))
{
if (state == 0)
{
patOne();
state = 1;
}
else if (state == 1)
{
patTwo();
state = 0;
}
}
}
void patOne()
{
for (int thisPin = 5; thisPin < 13; thisPin++) // loop from the lowest pin to the highest:
{
digitalWrite(thisPin, HIGH); // turn the pin on:
delay(timer);
digitalWrite(thisPin, LOW); // turn the pin off:
}
}
void patTwo()
{
for (int thisPin = 12; thisPin >= 5; thisPin--) // loop from the highest pin to the lowest:
{
digitalWrite(thisPin, HIGH); // turn the pin on:
delay(timer);
digitalWrite(thisPin, LOW); // turn the pin off:
}
}
This one uses a counter but again the led sequences only flash once.
int buttonPin = 13; // set up pinn 13 as button pin
int timer = 75; // The higher the number, the slower the timing.
int counter = 0;
int switchVal = digitalRead(buttonPin);
void setup()
{
// use a for loop to initialize each pin as an output:
for (int thisPin = 5; thisPin < 13; thisPin++)
{
pinMode(thisPin, OUTPUT);
pinMode (buttonPin, INPUT);
}
}
void loop()
{
int switchVal = digitalRead(buttonPin);
if(switchVal == HIGH)
{
counter ++;
}
if (digitalRead(buttonPin))
{
if (counter == 1)
{
patOne();
}
else if (counter == 2)
{
patTwo();
}
else if (counter ==3)
{
counter = 0;
}
}
}
void patOne()
{
for (int thisPin = 5; thisPin < 13; thisPin++) // loop from the lowest pin to the highest:
{
digitalWrite(thisPin, HIGH); // turn the pin on:
delay(timer);
digitalWrite(thisPin, LOW); // turn the pin off:
}
}
void patTwo()
{
for (int thisPin = 12; thisPin >= 5; thisPin--) // loop from the highest pin to the lowest:
{
digitalWrite(thisPin, HIGH); // turn the pin on:
delay(timer);
digitalWrite(thisPin, LOW); // turn the pin off:
}
}
I found the two following codes on the net and have them both running well
This code has the flash sequences I like. It controls the sequences through the use of a six way dip switch not the single button I want I would also like to change the use of delay to the use of millis but being the noob coder that I am I cant get my head round how to implement it !
// www.TheElectronicsHobbyist.com/blog
// Natalia Fargasch Norman
// LED control via DIP switches
// Arduino pins used for the LEDs
// LED1 13
// LED2 12
// LED3 11
// LED4 10
// LED5 9
// LED6 8
// Arduino pins used for the switches
// S1 7
// S2 6
// S3 5
// S4 4
// S5 3
// S6 2
// State of each switch (0 or 1)
int state[6];
// Random values for LED state and delay
long onoroff;
long millisecs;
// loop counters
int i, j;
// delay
int d = 250;
void setup() {
// pins for LEDs are outputs
// LEDs 1-6 on pins 13-8
for (i = 13; i >= 8; i--) {
pinMode(i, OUTPUT);
}
// pins for switches are inputs
// switches 1-6 on pins 7-2
for (i = 7; i >= 2; i--) {
pinMode(i, INPUT);
}
}
void loop() {
for (i = 0, j = 7; i < 6, j >= 2; i++, j--) {
state[i] = digitalRead(j);
}
if (state[0]) {
// scroll right
for (i = 13; i >= 8; i--) {
digitalWrite(i, HIGH);
delay(d);
digitalWrite(i, LOW);
}
} else if (state[1]) {
// scroll left
for (i = 8; i <= 13; i++) {
digitalWrite(i, HIGH);
delay(d);
digitalWrite(i, LOW);
}
} else if (state[2]) {
// scroll in
// light up LEDs on pins i and 8+(13-i)
for (i = 13; i >= 11; i--) {
digitalWrite(i, HIGH);
digitalWrite(21 - i, HIGH);
delay(d);
digitalWrite(i, LOW);
digitalWrite(21 - i, LOW);
}
} else if (state[3]) {
// scroll out
// light up LEDs on pins i and 8+(13-i)
for (i = 11; i <= 13; i++) {
digitalWrite(i, HIGH);
digitalWrite(21 - i, HIGH);
delay(d);
digitalWrite(i, LOW);
digitalWrite(21 - i, LOW);
}
} else if (state[4]) {
// scroll back and forth
for (i = 13; i >= 8; i--) {
digitalWrite(i, HIGH);
delay(d);
digitalWrite(i, LOW);
}
for (i = 9; i <= 12; i++) {
digitalWrite(i, HIGH);
delay(d);
digitalWrite(i, LOW);
}
} else if (state[5]) {
// random
for (i = 13; i >= 8; i--) {
randomSeed(analogRead(i - 8));
onoroff = random(0, 2);
millisecs = random(0, 301);
digitalWrite(i, onoroff);
delay(millisecs);
}
} else {
// default: off
for (i = 13; i >= 8; i--) {
digitalWrite(i, LOW);
}
}
}
This code I have also runs and it works well. I found it in a thread on here. I would like to use the sections of code that use the potetiometer to regulate the speed of the led pattern. Trouble is I dont understand how they work and how they change the speed as the pattern is flashing.
const int buttonPin = 2;
const int ledPin1 = 13;
int buttonState = 0;
int leds[] = {3, 4, 5, 6, 7, 8, 9, 10};
#define NUMBER_OF_LEDS (sizeof(leds)/sizeof(int))
boolean flickbook[][NUMBER_OF_LEDS] = {
{ HIGH, LOW, LOW, LOW, LOW, LOW, LOW, LOW},
{ LOW, HIGH, LOW, LOW, LOW, LOW, LOW, LOW},
{ LOW, LOW, HIGH, LOW, LOW, LOW, LOW, LOW},
{ LOW, LOW, LOW, HIGH, LOW, LOW, LOW, LOW},
{ LOW, LOW, LOW, LOW, HIGH, LOW, LOW, LOW},
{ LOW, LOW, LOW, LOW, LOW, HIGH, LOW, LOW},
{ LOW, LOW, LOW, LOW, LOW, LOW, HIGH, LOW},
{ LOW, LOW, LOW, LOW, LOW, LOW, LOW, HIGH}
};
#define FRAMES (sizeof(flickbook)/(sizeof(flickbook[0])))
int sensorPin = 0;
void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(buttonPin, INPUT);
for (int led=0; led<NUMBER_OF_LEDS; led++) {
pinMode(leds[led], OUTPUT);
}
}
void loop(){
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin1, HIGH);
}
else {
long time = millis();
for (int frame=0; frame<FRAMES; frame++) {
for (int led=0; led<NUMBER_OF_LEDS; led++) {
digitalWrite(leds[led], flickbook[frame][led]);
}
int sensorValue = map(analogRead(sensorPin), 0, 1023, 0, 1000);
while (sensorValue >= (millis() - time)) {
sensorValue = analogRead(sensorPin);
}
time = millis();
}
}
}
Wow that's a much longer post than I intended I hope my first post isn't either too green or too troublesome thanks for looking.
J*