d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Anyone Good With Arduino Code? > This Is Not My Cup Of Tea
Add Reply New Topic New Poll
Member
Posts: 1,057
Joined: Feb 21 2009
Gold: 298.00
Dec 3 2014 09:47pm
this is my first coding experience and looking for someone to help me write some code for a Arduino id be willing to trade some fg or d2 items for some working code. iv been fucking with it for a week now cant seem to figure it out :wallbash:
iv posted on there forums but didn't get much help

so what i want to happen is when a button is pushed:
LED1 ON wait 500ms then LED2 ON wait 2000ms then LED3 ON wait 2000ms then LED4 ON wait 2000ms then LED4 ON wait 2000ms the LED5 ON wait 10000ms
then LED6 on wait 500ms then LED7 on
then keep the LEDs on until the button is released then turn them all OFF
and if the button "stutters" start from the beginning.

this is what i got so far

Code

const int buttonPin = 1;
const int LED1 = 2;
const int LED2 = 3;
const int LED3 = 4;
const int LED4 = 5;
const int LED5 = 6;
const int LED6 = 7;
const int LED7 = 8;

int buttonState = 0;

void setup ()
{
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
pinMode(LED7, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop (){
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {

digitalWrite(LED1, HIGH); // #1 on
delay(1000); //wait
digitalWrite(LED2, HIGH); // #2 on
delay(1500); //wait
digitalWrite(LED3, HIGH); // #3 on
delay(1500); //wait
digitalWrite(LED4, HIGH); // #4 on
delay(1500); //wait
digitalWrite(LED5, HIGH); // #5 on
delay(120000); //wait
digitalWrite(LED6, HIGH); // #6 on
delay(500); //wait
digitalWrite(LED7, HIGH); // #7 on
}
else {
// all off
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);
}
}


but because i was using the delay() function for the wait times it wont read the button pin state till it has made it past the delay() then shut off and this will not work for what i want to end up doing with this code witch is controlling a oxygen compressor and it would over shoot the set psi mark if that is the case.

any thoughts? my brain is about fried :wacko: thanks
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 3 2014 10:19pm
i dont know anything about arduino. but my understanding is you want to execute that flow (turning on lights then waiting to turn on the next light) as long as the button is pressed down? as soon as the button is released, shut everything off?

when i did some programming on the GBA, we didn't use any thread sleep. we just captured the time, then used a loop to compare the current time to start time to see how long has elapsed. the upside is your code isn't blocking like delay, so it sounds like it's what you want?

/edit: i'm assuming ofc that you can't register some sort of event to see if the button was released

This post was edited by carteblanche on Dec 3 2014 10:25pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 3 2014 10:33pm
Quote (carteblanche @ Dec 3 2014 11:19pm)
i dont know anything about arduino. but my understanding is you want to execute that flow (turning on lights then waiting to turn on the next light) as long as the button is pressed down? as soon as the button is released, shut everything off?

when i did some programming on the GBA, we didn't use any thread sleep. we just captured the time, then used a loop to compare the current time to start time to see how long has elapsed. the upside is your code isn't blocking like delay, so it sounds like it's what you want?

/edit: i'm assuming ofc that you can't register some sort of event to see if the button was released


giving you some pseudocode here

Code
void loop (){
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
if (startTime == null){
// user just now started pressing the button
// nothing turned on yet. turn on the first LED
digitalWrite(LED1, HIGH);
lastTurnedOn = LED1;

// start timer for when to turn on the next LED
startTime = now(); // whatever function gets current time
} else if (lastTurnedOn == LED1){
// wait 1000 ms to turn on LED2
if (now() - startTime >= 1000){
digitalWrite(LED2, HIGH);
lastTurnedOn = LED2;

// reset our timer for the next LED to turn on
startTime = now();
} else {
// do nothing. just chill for a while until it's been 1000 ms
}
}
// repeat for all LED
}
else {
// all off
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);

// reset start time so we know we gotta restart
startTime = null;
}
}


This post was edited by carteblanche on Dec 3 2014 10:39pm
Member
Posts: 1,057
Joined: Feb 21 2009
Gold: 298.00
Dec 3 2014 10:34pm
"i dont know anything about arduino. but my understanding is you want to execute that flow (turning on lights then waiting to turn on the next light) as long as the button is pressed down? as soon as the button is released, shut everything off?"
yes exactly

im pretty sure there is a way to do this using the on-board clock to compare like you were saying i just dont know how its done ill have to look into that
Member
Posts: 1,057
Joined: Feb 21 2009
Gold: 298.00
Dec 3 2014 10:36pm
cool thanks ill play around with that
Member
Posts: 7,324
Joined: Dec 22 2002
Gold: 1,261.00
Dec 4 2014 05:57pm
In general, you can never use delay or sleep for the full duration if you want to retain control and process other messages. You have to sleep for very short amounts in a loop to give your thread a chance to do other things.

Keep in mind that you SHOULD still sleep in the loop, which is one thing I disagree with the pseudocode carte posted. Having a proc spinning as fast as it can while waiting for an event is poor design, especially for embedded applications with battery and heat concerns. I would strongly suggest sleeping for, say, 100 ms at the end. Or whatever value is small enough to not affect the operation of your system. Depends on how quickly you must respond to events, obviously.

This post was edited by russian on Dec 4 2014 06:05pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll