Go Down

### Topic: problem with millis() (Read 1 time)previous topic - next topic

#### zhyf1991

#15
##### Feb 26, 2013, 08:11 pm

Code: [Select]

void loop()
{
int i=0;
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (i<255){
...
}

You are declaring and initializing i to 0 at the beginning of every loop, how can it ever not be 0? You need to make i either static or global.

Also, why not try it yourself instead of asking people to check your code for you? I would run through a couple of the Serial communication examples so that you know to communicate with the Arduino IDE's Serial monitor; this can be used as a valuable debugging tool.

sorry for this, but my professor won't give me materials until Friday, and this must be done on Friday. so I don't have any opportunities to test it......or I wouldn't do this

#### zhyf1991

#16
##### Feb 27, 2013, 04:16 pm
Code: [Select]
long previousMillis = 0;
long interval = 195;
int i=0;
void setup()
{
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);

}

void loop()
{

unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (i<255){
i=i+1;}
else{
i=0;}
PORTD=i;

}

}

#### Arrch

#17
##### Feb 27, 2013, 04:19 pm
Code: [Select]

if (i<255){
i=i+1;}
else{
i=0;}

Looks ugly.

Code: [Select]

i++; //increment i by one
if (i>=255) // if we've gone past our boundary
{
i=0; // go back to 0
}

Looks better.
(Notice the brackets on their own lines)

#### zhyf1991

#18
##### Feb 27, 2013, 04:30 pm

Code: [Select]

if (i<255){
i=i+1;}
else{
i=0;}

Looks ugly.

Code: [Select]

i++; //increment i by one
if (i>=255) // if we've gone past our boundary
{
i=0; // go back to 0
}

Looks better.
(Notice the brackets on their own lines)

Thx! I'm really really a rookie to C language. Helped me a lot

#### AWOL

#19
##### Feb 27, 2013, 04:33 pm
If you've got an eight bit wide port and you're writing the value directly, there's no need to reset anything, as I hinted above - you'll only ever write the LS eight bits.
"Pete, it's a fool 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.

#### zhyf1991

#20
##### Feb 27, 2013, 04:36 pm

If you've got an eight bit wide port and you're writing the value directly, there's no need to reset anything, as I hinted above - you'll only ever write the LS eight bits.

I get it. Thx.

Go Up

Please enter a valid email to subscribe