Go Down

Topic: Small programming problem. Might be me. (Read 8443 times) previous topic - next topic

PaulS

Quote
And I need to wait 7+ weeks for what?

Suppose interval had a value of 1000, and previousMillis had a value of 0.

Suppose that millis() returned 13, 17, 20, 25, .... 995, 998, 1001, 1004...

998 + 0 is less than interval, so wait some more. 1001 - 0 is not equal to interval, either, so wait some more. How long? Well, you need to wait until the value returned by millis() will no longer fit in an unsigned long, so it rolls over, and returns small numbers again. The time that takes is greater than 49 days, or 7+ weeks.

Even then, you aren't guaranteed an exact match. So, you should never use == in a time test. You want to quit waiting (or doing) when the time waiting (or doing) equals or exceeds the time to wait (or do) whatever.

As I pointed out earlier, you can use a for loop to call the pinMode function and to call the digitalWrite function, and have many fewer lines of code.
The art of getting good answers lies in asking good questions.

Soapy29

I still don't get this millis function. It just doesn't make sence that I need to use millis to create a loop to do 2 things... Isn't there an easier way to do it?

AWOL

#17
Jan 08, 2011, 10:27 am Last Edit: Jan 08, 2011, 11:08 am by AWOL Reason: 1
There is a much easier way - write your sketch in occam, a language specifically designed to express concurrency.
Or maybe look at one of the timed action libraries.
But please - use loops and simplify your code.
You think it is long now, but add a couple of LEDs, and the way you're going, it'll end up huge.
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

robtillaart


@Soapy
I think you have to spend time reading the examples in the tutorial section to get understanding of how the Arduino works, and how things can be done.

Furthermore there is a section about arrays in the reference section very worth reading - http://www.arduino.cc/en/Reference/Array -

As you are new and eager to learn, please spend some time in the tutorial section as there is so much information. Takes maybe 2 weeks but the comments made here will make so much more sense to you. I know as I have been a completely newbie once myself - and still am in lot of areas :)
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Soapy29

It's good to know that someone knows what I'm going through because I'm new to this platform and typing code. I will look at the site as soon as posible. I have been reading some of the examples, but they dont make the greatest sence.

AWOL

#20
Jan 08, 2011, 07:24 pm Last Edit: Jan 08, 2011, 07:27 pm by AWOL Reason: 1
We ALL know what you're going through, but until you start listening to advice and doing stuff for yourself and not just bleating that there must be a simpler way, you're not going to get anywhere.

If you tell us what the gap between your understanding and what is before you, and tell us exactly what doesn't make sense, we can help, otherwise you're just an irritating buzz.
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Soapy29

#21
Jan 08, 2011, 07:25 pm Last Edit: Jan 08, 2011, 07:28 pm by Soapy29 Reason: 1
@rob,
Ok well that made some sence. I'll have to look at it more on my computer. (right now I'm on my iPod..) lol

@AWOL, sorry if you never gave me the link to that site on arrays...

Soapy29

So is this a start?:

Code: [Select]

/*-----------------------------------------
 The main purpose of this code is to
flash 2 RGB led's and 4 scrolling led's
at the same time. The problem is, I can
only have one of them on at a time.
Eaither the 4 scrolling led's, are the
RGB led's.

Hope you like my code cause It took a
little time to create!  I mean it works
just fine accept for the 4 led's.
     -Soapy29
------------------------------------------*/
int potPin= 5;
int potVal= 0;
int delay1= 50;
int delay2= 60;
int pinArray[]= {5, 6, 7, 8, 9, 10, 11, 12};
int count= 0;

void setup() {
       //----------------------------------------
     //  Sets up the leds to work.
     //----------------------------------------
       for (count=0;count<8;count++) {
         pinMode(pinArray[count], OUTPUT);
         digitalWrite(pinArray[count], HIGH);
}

//=======================================
//  Please don't change anything below.
//=======================================

void loop() {
       delay1= analogRead(potPin);
     rR();
     rR();
     rR();
     rL();
     rL();
     rL();
     bR();
     bR();
     bR();
     bL();
     bL();
     bL();
       rR();
     rR();
     rR();
     rL();
     rL();
     rL();
     bR();
     bR();
     bR();
     bL();
     bL();
     bL();
     blink1();
     blink2();
       blink3();
       blink4();
       blink1();
       blink2();
       blink3();
       blink4();
       blink1();
       blink2();
       blink3();
       blink4();
}

void rR() {
     //-------------------------------------
     //  Blinks the right led red.
     //-------------------------------------
     delay1= analogRead(potPin);
     digitalWrite(12, LOW);
     delay(delay1);
     digitalWrite(12, HIGH);
     delay(delay1);
}

void rL() {
     //-----------------------------------
     //  Blinks the left led red.
     //-----------------------------------
     delay1= analogRead(potPin);
     digitalWrite(11, LOW);
     delay(delay1);
     digitalWrite(11, HIGH);
     delay(delay1);
}

void bR() {
     //--------------------------------------
     //  Blinks the right led blue.
     //--------------------------------------
     delay1= analogRead(potPin);
     digitalWrite(10, LOW);
     delay(delay1);
     digitalWrite(10, HIGH);
     delay(delay1);
}

void bL() {
     //------------------------------------
     //  Blinks the left led blue.
     //------------------------------------
     delay1= analogRead(potPin);
     digitalWrite(9, LOW);
     delay(delay1);
     digitalWrite(9, HIGH);
     delay(delay1);
}
//------------------------------------------------
//  Blinks the led's different colors.
//------------------------------------------------
void blink1() {
       //rR
     delay1= analogRead(potPin);
     digitalWrite(12, LOW);
     delay(delay1);
     digitalWrite(12, HIGH);
}
void blink2() {
       //rL
       delay1= analogRead(potPin);
     digitalWrite(11, LOW);
     delay(delay1);
     digitalWrite(11, HIGH);
}
void blink3() {
       //bR
       delay1= analogRead(potPin);
     digitalWrite(10, LOW);
     delay(delay1);
     digitalWrite(10, HIGH);
}
void blink4() {
       //bL
       delay1= analogRead(potPin);
     digitalWrite(9, LOW);
     delay(delay1);
       digitalWrite(9, HIGH);
}

void scroll() {
 digitalWrite(8, LOW);
 digitalWrite(7, HIGH);
 delay(delay2);
 digitalWrite(7, LOW);
 digitalWrite(6, HIGH);
 delay(delay2);
 digitalWrite(6, LOW);
 digitalWrite(5, HIGH);
 delay(delay2);
 digitalWrite(5, LOW);
 digitalWrite(6, HIGH);
 delay(delay2);
 digitalWrite(6, LOW);
 digitalWrite(7, HIGH);
 delay(delay2);
 digitalWrite(7, LOW);
 digitalWrite(8, HIGH);
 delay(delay2);
}


Kinda...

PaulS

Code: [Select]
void rR() {
     //-------------------------------------
     //  Blinks the right led red.
     //-------------------------------------
     delay1= analogRead(potPin);
     digitalWrite([glow]12[/glow], LOW);
     delay(delay1);
     digitalWrite([glow]12[/glow], HIGH);
     delay(delay1);
}

void rL() {
     //-----------------------------------
     //  Blinks the left led red.
     //-----------------------------------
     delay1= analogRead(potPin);
     digitalWrite([glow]11[/glow], LOW);
     delay(delay1);
     digitalWrite([glow]11[/glow], HIGH);
     delay(delay1);
}

The only difference between these two functions is the pin number.

Code: [Select]
void f(int pin)
{
     //-----------------------------------
     //  Blinks the LED on the specified pin.
     //-----------------------------------
     delay1= analogRead(potPin);
     digitalWrite([glow]pin[/glow], LOW);
     delay(delay1);
     digitalWrite([glow]pin[/glow], HIGH);
     delay(delay1);
}

Then, replace rR(); and rL(); with f(12); and f(11);

Look for other places where very similar blocks of code exist, and fix them.

Before posting code, it's generally a good idea to at least try to compile it.
The art of getting good answers lies in asking good questions.

AWOL

#24
Jan 08, 2011, 09:40 pm Last Edit: Jan 08, 2011, 09:43 pm by AWOL Reason: 1
Quote
@AWOL, sorry if you never gave me the link to that site on arrays...

http://arduino.cc/en/Reference/Array

As ever, RTFM

Code: [Select]
void bx()
{
  delay1= analogRead(potPin);
  digitalWrite(x, LOW);
  delay(delay1);
  digitalWrite(x, HIGH);
  [glow]delay(delay1);[/glow]
}

void blinkx()
{
  delay1= analogRead(potPin);
  digitalWrite(xx, LOW);
  delay(delay1);
 digitalWrite(xx, HIGH);
}

Notice any similarities?
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Soapy29

Quote
RTFM

Is that some sorta of radio station?

PaulS

RTFM = Read The Fun Manual. (or something similar to that; I'm sure I got it at least 75% right)
The art of getting good answers lies in asking good questions.

AWOL

#27
Jan 08, 2011, 09:47 pm Last Edit: Jan 08, 2011, 09:47 pm by AWOL Reason: 1
Code: [Select]
//=======================================
//  Please don't change anything below.
//=======================================
[glow]// whyever not?[/glow]
void loop() {
      ┬ádelay1= analogRead(potPin); [glow]// why not get rid of this line?[/glow]


And loops.
I really like loops.
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Soapy29

rob a few posts up told me what I was doing wrong. He gave me a solution to fix my code. You guys just yell and say "Shorten your code newb!" Yeah, that helps a lot. Yelling at noobs.

AWOL

He also told you to come back in two weeks.
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Go Up