Show Posts
|
|
Pages: [1] 2 3 ... 16
|
|
2
|
Using Arduino / Programming Questions / Re: A 60 second timer
|
on: May 08, 2013, 09:43:48 pm
|
As Toby says you know when you start just wait sixty seconds. Arduino keeps the time. It just keeps it in a simple count. Trying to keep track of seconds is over thinking the problem. /* watches for LOW signal at alarmPin when detected delays for alarmDelay then turns on relay opening the circuit at alarmPin resets anytime */
const byte alarmPin = 2; const byte relayPin = 6; const unsigned long alarmDelay = 60000UL;
void checkAlarm(){ static boolean tripped = false; static unsigned long trippedTime; if (tripped && digitalRead(alarmPin) == HIGH) { tripped = false; digitalWrite(relayPin, LOW); } if (tripped && millis() - alarmDelay > trippedTime) { digitalWrite(relayPin, HIGH); } if (!tripped && digitalRead(alarmPin) == LOW) { tripped = true; trippedTime = millis(); } }
void setup(){ pinMode (alarmPin, INPUT_PULLUP); pinMode (relayPin, OUTPUT); }
void loop(){ checkAlarm(); }
|
|
|
|
|
3
|
Using Arduino / Programming Questions / Re: [SOLVED] "Hourglass" without delay - understanding if, while, & for loops
|
on: May 07, 2013, 08:20:26 am
|
I don't change up the order for display. The leds are all in a nice line it's just that the pins aren't wired consecutively. I started with them mostly nice and in order at one time  . To adapt your Arduino setup to my code only requires putting in your particular led pin values into the ledPin[] array declaration. The reason for using an array like this is that it becomes portable. Say that in your project you decided that you want to use interrupts for switches. Interrupts on UNO only work on pins 2 and 3. You would have to move those leds which breaks your code. With the array approach you would just move the leds to new pins and change those pin values in the array. Also notice how my code can be changed easily for the number of leds. If you wanted this to work with 7 leds you would just remove a value from the array. The sketch will recalculate both the count of leds and the interval. To randomly rearrange them you would have to change the ledPin array to just byte not constant. Then write a small function that rearranges the values in the array after each cycle.
|
|
|
|
|
4
|
Using Arduino / Programming Questions / Re: [SOLVED] "Hourglass" without delay - understanding if, while, & for loops
|
on: May 06, 2013, 09:26:32 pm
|
The only issue I have after seeing it run is that the last pin (11) turns off and then back on so fast it appears to stay lit. My sketch turns it off for the entire interval time before turning all of them back on. That simply requires moving a piece of code. I had it your way at first but didn't like waiting for it to turn on. LOL const byte ledPin[] = { 9, 7, 4, 5, 6, 8, 10, 11}; const byte ledCount = sizeof(ledPin) / sizeof(ledPin[0]);
void setup(){ for (byte n = 0 ; n < ledCount; n++) pinMode(ledPin[n], OUTPUT); }
void loop(){ static byte pointer = ledCount; static unsigned long lastChangeTime; const unsigned long interval = 60000 / ledCount; unsigned long currentTime = millis(); if (currentTime - lastChangeTime > interval) { lastChangeTime = currentTime; if (pointer == ledCount) { for (byte n = 0; n < ledCount; n++) { digitalWrite(ledPin[n], HIGH); } } if (pointer < ledCount) { digitalWrite(ledPin[pointer], LOW); } if (++pointer > ledCount) { pointer = 0; } } }
I just moved the bit that turns on the leds to inside the timing condition.
|
|
|
|
|
5
|
Using Arduino / Programming Questions / Re: [SOLVED] "Hourglass" without delay - understanding if, while, & for loops
|
on: May 06, 2013, 08:16:34 pm
|
Hey, I solved it too. The biggest problem I see with the OP's code is that it expects all the led pins to be sequential and he makes absolute reference to them. With an array the physical sequence of the pins doesn't matter. As you'll see in my sketch. const byte ledPin[] = { 9, 7, 4, 5, 6, 8, 10, 11}; const byte ledCount = sizeof(ledPin) / sizeof(ledPin[0]);
void setup(){ for (byte n = 0 ; n < ledCount; n++) pinMode(ledPin[n], OUTPUT); }
void loop(){ static byte pointer = ledCount; static unsigned long lastChangeTime; const unsigned long interval = 6667; unsigned long currentTime = millis(); if (pointer == ledCount) { for (byte n = 0; n < ledCount; n++) { digitalWrite(ledPin[n], HIGH); } } if (currentTime - lastChangeTime > interval) { lastChangeTime = currentTime; if (pointer < ledCount) { digitalWrite(ledPin[pointer], LOW); } if (++pointer > ledCount) { pointer = 0; } } }
|
|
|
|
|
6
|
Using Arduino / Programming Questions / Re: Program game does not work on chip, whats wrong with program
|
on: May 02, 2013, 09:21:15 pm
|
|
When you say "presses the button" do you mean press and hold it or press and release it? Could I, as a player simply hold the button down?
I can see your description in your code but would suggest that you rename some variables. "State22" might be better as "player2buttonCounter". Use descriptive variable names and use copy / paste to avoid typos and undeclared variable errors.
|
|
|
|
|
11
|
Using Arduino / Programming Questions / Re: Numbered Variables In For Loop
|
on: April 13, 2013, 02:39:01 pm
|
You're trying to use a technique for referencing an array on non-array type variables. // an array declaration int encoder[4];
//
for (byte j = 0; j < 4; j++) { Serial.print("Encoder "); Serial.print(j + 1); // array index start at 0 Serial.print(" ["); Serial.print(encoder[j]); // the encoder value you are looking for Serial.print("] "); // you may want to make this one println() }
// spot the difference?
You may want to read up more on using arrays. Try cplusplus.com
|
|
|
|
|
13
|
Using Arduino / Programming Questions / Re: How to trigger Relays
|
on: April 12, 2013, 02:29:05 pm
|
|
It might help you to think of your requirements in simpler terms. Instead of thinking of it as relay A on for 30 then AB on for 30 then B on for 30. I just see two relays running for a minute each with the one starting 30 seconds after the other. Problem is that you can't see them this way until you come to grips with blink without delay.
|
|
|
|
|
14
|
Using Arduino / Programming Questions / Re: LED blink sequence accordingly to array
|
on: April 12, 2013, 12:09:16 pm
|
I recently breadboarded up a row of ten leds controlled by an UNO. One failed so now it's nine. So I've been playing with this type of thing a bunch lately as a form of mental exercise. I love solving puzzles. Your sketch looks okay other than it will only run through the pattern once. Which is fine if that's what you want. If it should run over and over it belongs in loop. It is also poor technique with Arduino to use delays like that. Using the chip's clock is preffered. Here's how I'd take on your problem. (Not tested. Written off the top if my head.) // first an array of our led pins declared globally const byte ledPin[] = {2,3,4}; // I don't like to make the variable plural // how many leds do we have byte ledCount = sizeof(ledPin) / sizeof(ledPin[0]); // now the sequence const byte sequence[] = {1,2,0,1,0,2}; // it will help to know how many elements this array holds byte sequenceCount = sizeof(sequence) / sizeof(sequence[0]); // time between changes const unsigned long duration = 400; // it's best to use unsigned longs with timing variables
// now we're ready to set our pins void setup(){ for (byte index = 0; index < ledCount; index++) { pinMode(ledPin[index], OUTPUT); } }
// loop will do all the work and display the sequence repeatedly void loop(){ // we'll need some variables static byte pointer = 0; // we'll use this to point to the current sequence static unsigned long lastChangeTime = 0; // for timing purposes unsigned long currentTime = millis();
// we'll display all the leds then advance the pointer if it's time to if (currentTime - lastChangeTime > duration) { // we have to remember this time lastChangeTime = currentTime; // basically all leds are off except one for (byte index = 0; index < ledCount; index++) { if (index == sequence[pointer]) digitalWrite(ledPin[index], HIGH); else digitalWrite(ledPin[index], LOW); } // now we'll deal with the pointer // use a programming calculator to see how this works. It's cool. pointer = ++pointer % sequenceCount; // this will roll the pointer over to display the sequence again } }
I haven't compiled or tested this but it should work. Notice that you could add leds or sequences to those arrays and the sketch will adjust because I calculate the array size. The way you've done it with constants means that you would have to edit any for loop that accesses those arrays with the new max value. In mine the only constant I use is 0. When the code gets large a constant can be a magic number or a number with no context. It could be a mystery where 6 comes from rather than sequenceCount which gives the number context. Does that make sense or am I rambling? LOL
|
|
|
|
|
15
|
Using Arduino / Programming Questions / Re: I Get These Errors?
|
on: April 12, 2013, 10:09:47 am
|
|
You are also attempting to return a value in a function you have declared as void. A void function isn't expecting to return a value. You are trying to return pingdec which is already global.
A good way to avoid mismatched brackets is to always type them out at the start. For example when I start a for statement I often type the following:
for() {}
Then I hit ctrl-t for formatting, then I go back and put in code.
|
|
|
|
|