loading

Arduino on off switch for LEDS

Hi All,
I'm about to do a project that involves using Arduino to create an LED 'chase' effect. However I want the 'chase' to be triggered when someone removes an object from the switch. For example the lights would be on and running with a small rock (or similar) holding down a switch which then goes into the off position when the weight is removed. I can create a chase and I can operate the switch but I need help in writing the code that will combine these two features in to one event.

Any suggestions, esp help with code will be warmly welcomed.
Regards,
Frank

sort by: active | newest | oldest
1-10 of 12Next »
bertus52x116 years ago
I have used a similar routine in my I'ble "reaction time tester" (as long as the button was held down, a routine was executed)


int Button=1; //button assigned to pin 1

void setup()
{
pinMode(Button, INPUT);
}


void loop()
{
while (digitalRead(Button)==HIGH)// This loop is repeated while button is held down.
{
delay(10);
}


rest of the sketch
}
frankhiggins (author)  bertus52x116 years ago
Hi Bertus52x11
I tried the code but can't get it to work with the 'chase' code. Can you have a look at this tell me what I'm doing wrong?
Many thanks,

Frank.

/*




The circuit:
* LEDs from pins 2 through 9 to ground

created 2006
by David A. Mellis
modified 5 Jul 2009
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/Array
*/
int Button=1; //button assigned to pin 1
int timer = 100; // The higher the number, the slower the timing.
int ledPins[] = {
2,3,4,5,6,7,8,9 }; // an array of pin numbers to which LEDs are attached
int pinCount = 8; // the number of pins (i.e. the length of the array)

void setup() {
pinMode(Button, INPUT);
int thisPin;
// the array elements are numbered from 0 to (pinCount - 1).
// use a for loop to initialize each pin as an output:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}

void loop() {
// loop from the lowest pin to the highest:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
{
digitalWrite(ledPins[thisPin], LOW);
}
while (digitalRead(Button)==HIGH);
digitalWrite (Button)= HIGH);
}else{ (digitalRead(Button)==LOW;
digitalWrite (Button)= LOW);

}

// loop from the highest pin to the lowest:
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
}
Let me first try to understand it...
An object lies on a switch (so that it is pressed IN).
Then when you remove the weight, the LED chase starts?
frankhiggins (author)  bertus52x116 years ago
Hi, absolutely right then the chase should stop when the weight is replaced.
Thanks for your help,
Frank
First of all, check if your Chase routine works without the push button included (I haven't checked that part of your routine).

Then you need to change the order slightly (the PB condition is in the wrong place and you should add a second condition:

int Button=1; //button assigned to pin 1
***insert INT of your Chase routine***

void setup()
{
pinMode(Button, INPUT);
*** insert setup of the CHASE routine***
}


void loop()
{
while (digitalRead(Button)==HIGH)
{
delay(10);
}

while (digitalRead(Button)==LOW)
{
*** INSERT THE CHASE ROUTINE HERE***
}

}


// now it should work as follows:
While the PB is pressed IN (=high), a simple routine is executed (delay 10ms and then checking the condition again).
Once the PB is released (= low) the program jumps to the next part of the program: as long as the PB is low, the Chase routine is executed.
Once the PB is pressed again, the program starts over again with the first routine.
frankhiggins (author)  bertus52x116 years ago
Hi Again,
I tried the code (the chase does work on its own) like you said (I think) but now all the LEDS are on, not flashing and the button makes no difference.
Sorry to be a pain but if you have any more time to spare I would appreciate it.

Many thanks,

Frank. ps I moved the pins to make it easier to lay out on the breadboard and changed the code accordingly

/*The circuit:
* LEDs from pins 3 through 10 to ground

created 2006
by David A. Mellis
modified 5 Jul 2009
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/Array
*/
int Button=2; //button assigned to pin 2
//int timer = 100; // The higher the number, the slower the timing.
int ledPins[] = {
3,4,5,6,7,8,9,10 }; // an array of pin numbers to which LEDs are attached
int pinCount = 8; // the number of pins (i.e. the length of the array)

void setup() {
pinMode(Button, INPUT);
int thisPin;
// the array elements are numbered from 0 to (pinCount - 1).
// use a for loop to initialize each pin as an output:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}

void loop() {
while (digitalRead(Button)==HIGH);
{
delay (10);
}
while (digitalRead(Button)==LOW)
// loop from the lowest pin to the highest:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
//delay(timer);
// turn the pin off:
{
digitalWrite(ledPins[thisPin], LOW);
}
while (digitalRead(Button)==HIGH);
//digitalWrite (Button)= HIGH);
//}else{ (digitalRead(Button)==LOW;
//digitalWrite (Button)= LOW);

}

// loop from the highest pin to the lowest:
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
//delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
}
I'll have a look at the code this evening. Meantime, are you sure that you have rigged your button properly?
You should have 3 connections:
+5V
Ground (with pull down resitor)
pin 2
frankhiggins (author)  bertus52x116 years ago
Hi,
sadly I have, I too thought that might be the answer. I am still trying to solve it and look forward to your thoughts later this evening.

many thanks,

Frank
You should really focus on your syntax (I know it's boring, but otherwise it doesn't work). This requires some discipline...
A quick review showed me that on 1 or 2 occasions, you started a line with "//" which means you're not giving any commands, but merely stating a comment.
Secondly, the original (wrongly) PB sequence was still in the routine.

Try this and let me know what happens:

int Button=2; //button assigned to pin 2
int timer = 100; // The higher the number, the slower the timing.
int ledPins[] = {
3,4,5,6,7,8,9,10 }; // an array of pin numbers to which LEDs are attached
int pinCount = 8; // the number of pins (i.e. the length of the array)

void setup() {
pinMode(Button, INPUT);
int thisPin;
// the array elements are numbered from 0 to (pinCount - 1).
// use a for loop to initialize each pin as an output:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}

void loop() {
while (digitalRead(Button)==HIGH);
{
delay (10);
}

while (digitalRead(Button)==LOW)
{
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
digitalWrite(ledPins[thisPin], LOW);
}

for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
digitalWrite(ledPins[thisPin], LOW);
}
}
}
frankhiggins (author)  bertus52x116 years ago
Bertus52x11,
You are a star, it works, there are some slight hiccups - ie not running each and every time I push the button however I think this is the wiring. I haven't yet trimmed any of the leads or resistors.

Many, many thanks and you are ever in London I will buy you a large drink of your choice, or ice cream, toffee, chocolate or whatever.

I take on board the comment re syntax, I'm aware of this. I'm learning processing at the same time both from scratch - enjoyable but tiring and confusing.

Nice one,

Frank
1-10 of 12Next »