Pages: 1 [2]   Go Down
Author Topic: problem with millis()  (Read 1046 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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;
   
  }
 
}
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3376
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Looks ugly.

Code:
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)
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Looks ugly.

Code:
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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 301
Posts: 26220
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: 1 [2]   Go Up
Jump to: