Go Down

### Topic: value j not increment properly (Read 260 times)previous topic - next topic

#### aimmet

##### Feb 11, 2020, 03:40 am
Hello all, I noticed when im trying to incrememnt j every 100 milliseconds using the Millis(); function, I realized j is not going up by 1. it just stays at 0. Whats the proper way to get the j to increment by 1 every 100 milliseconds? (Ill only post the part of the code where its happening to make reading more simple)

Code: [Select]
`  int i;  static int j = 0;  unsigned long interval = 100;  unsigned long currentMillis = millis();  unsigned long previousMillis;  if (currentMillis - previousMillis >= interval)        {            j + 1;                 previousMillis = currentMillis;              if (j == 256)                 {                  j = 0;                }             }     {     for(i=0; i< 2; i++) {      strip.setPixelColor(i, j, j, j);    }    strip.show();    Serial.println(j);  }}`

#### sterretje

#1
##### Feb 11, 2020, 04:14 am
Use on of the below
Code: [Select]
`j++;j+=1;j=j+1;`
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

#### vaj4088

#2
##### Feb 11, 2020, 05:18 am
Code snippets are not very helpful.

j+1 does not store its result anywhere.  j++ does.

#### aimmet

#3
##### Feb 13, 2020, 04:03 am
I replaced it with both j+=1 and j== yet the value still remains zero. is it an issue with the millis counter?

#### gfvalvo

#4
##### Feb 13, 2020, 04:06 amLast Edit: Feb 13, 2020, 04:06 am by gfvalvo
I replaced it with both j+=1 and j== yet the value still remains zero. is it an issue with the millis counter?
No, it's an issue with your code that we can't see. Show us this latest attempt.
No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

#### vaj4088

#5
##### Feb 13, 2020, 04:08 am
No one said to try j==.
Please post good code that does not increment j.  The whole thing please, not just a snippet.  There is a good chance that the problem is in the part that we cannot see.

#### boolrules

#6
##### Feb 13, 2020, 07:14 amLast Edit: Feb 13, 2020, 07:25 am by boolrules
If this is in loop( ) you could likely be setting currentMillis( ) faster than 100mS.
You also have a block for maybe no good reason.
Why do you have so many spaces in your code?

#### aimmet

#7
##### Feb 17, 2020, 04:47 am
Sorry, heres the entire code, the j incrementing happens in rainbowcycle function
Code: [Select]
`#include <Adafruit_NeoPixel.h>#define NUM_LEDS 3#define BRIGHTNESS 75#define PIN A0//const int LED = A0;const int Xs = 9;// pins the buttons are attached tooconst int A = 4;//const int B = 5;//const int X = 8;//const int Y = 7;//const int DL = 6;//int lastButtonState;int buttonState;double long aPressed; // values to insert millis valuedouble long bPressed;double long xPressed;double long yPressed;double long dlPressed;double long xsPressed;byte R; // byte variablesbyte G;byte V;int zero = 0;unsigned long previousMillis = 0;unsigned long currentMillis = millis();Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);  int a = digitalRead(A); //read digital inputs into integers  int b = digitalRead(B);  int x = digitalRead(X);  int y = digitalRead(Y);  int dl = digitalRead(DL);  int xs = analogRead(Xs); // 0-360-728void setup() {  pinMode(A, INPUT);  pinMode(B, INPUT);  pinMode(X, INPUT);  pinMode(Y, INPUT);  pinMode(DL, INPUT);  pinMode(Xs, INPUT);  Serial.begin(9600);  strip.setBrightness(BRIGHTNESS);  strip.begin();  strip.show(); // Initialize all pixels to 'off'  byte Wheel = 0;}void loop() {  static int pattern = 0, lastReading;  double long Millis = millis();  int a = digitalRead(A);  int b = digitalRead(B);  int x = digitalRead(X);  int y = digitalRead(Y);  int dl = digitalRead(DL);  int xs = analogRead(Xs); // 0-360-728    //------------------------CONDITIONS BEGIN HERE------------------------------------------//    if (b == 0) //when pressed, turn on LEDs to RED    {      strip.clear();      bPressed = Millis;      strip.setPixelColor(2, 255, 0, 0);      strip.show();      strip.setPixelColor(1, 255, 0, 0);      strip.show();      strip.setPixelColor(0, 255, 0, 0);      strip.show();      strip.clear();              }  else if (30 + bPressed > Millis) // bPressed will be higher than Millis momentarily, execute color fade    {      uint16_t ib, jb;      for (jb = 255; jb > 0; jb--) {        for (ib = 0; ib < 2; ib++) {        strip.setPixelColor(ib, jb, 0, 0);         }      strip.show();          //Serial.println(j);      strip.clear();      }   }   if (x == 0)     {      xPressed = Millis;      for (int ixx=0; ixx<3; ixx++)      strip.setPixelColor(ixx, 0, 255, 255);      strip.show();    }  else if (30 + xPressed > Millis)    {      uint16_t ix, jx;      for (jx = 255; jx > 0; jx--) {        for (ix = 0; ix < 2; ix++) {        strip.setPixelColor(ix, 0, jx, jx);         }      strip.show();          //Serial.println(j);      strip.clear();      }      }     else if (x == 1)     {             for (int I = 0; I < 2; I++)        strip.setPixelColor(I, 0, 0, 0);       strip.show();     }          strip.clear();           if ((a && b && x && y && dl && (xs == 360 || xs == 361)) == 1)      {         rainbowCycle();      }}///////////////////////////////////// // functions for future use////////void rainbowCycle() {  byte *c;  int i;  static int j = 0;  unsigned long interval = 100;  if (currentMillis - previousMillis >= interval)          {            j++;                 previousMillis = currentMillis;            if (j == 256)               {              j = 0;              }               }     {     for(i=0; i< 2; i++) {      strip.setPixelColor(i, j, j, j);    }    strip.show();    Serial.println(j);  }}byte * Wheel(byte WheelPos) {  static byte c[3];   if(WheelPos < 85) {   c[0]=WheelPos * 3;   c[1]=255 - WheelPos * 3;   c[2]=0;  } else if(WheelPos < 170) {   WheelPos -= 85;   c[0]=255 - WheelPos * 3;   c[1]=0;   c[2]=WheelPos * 3;  } else {   WheelPos -= 170;   c[0]=0;   c[1]=WheelPos * 3;   c[2]=255 - WheelPos * 3;  }  return c;}`

#### evanmars

#8
##### Feb 17, 2020, 05:10 am
When do you ever update currentMillis?

#### aimmet

#9
##### Feb 18, 2020, 05:52 am
When do you ever update currentMillis?
its at the top when defining the terms.

#### vaj4088

#10
##### Feb 18, 2020, 05:59 am
currentMillis is declared at the top and given an initial value but it never changes after that.  Does it need to get a fresh value at the top of the loop() function?

Go Up