trouble with code

Thread Starter

Killerbee65

Joined May 15, 2017
256
Hello all,

I recently started getting back to my antics and picked up an old project once again to finally finish, I came back to see a piece of code that just keeps giving me errors and I don't understand why?

Code:
#include <Stepper.h>
//Written By Nikodem Bartnik - nikodembartnik.pl
#define STEPPER_PIN_1 8
#define STEPPER_PIN_2 7
#define STEPPER_PIN_3 6
#define STEPPER_PIN_4 5
#define motorSteps 400

int pinArray[] = {13, 12};
int count = 0;
float minDelay = 125;
float maxDelay = 250;
const int redPin = 11;
const int bluePin = 10;
int step_number = 0;
unsigned long blinkStartMillis;
unsigned long headStartMillis;
unsigned long currentMillis;
const unsigned long blinkperiod = 1000;  //the value is a number of milliseconds, ie 1 second
const unsigned long headperiod = 1000;  //the value is a number of milliseconds, ie 1 second
void setup() {
  blinkstartMillis = millis();  //initial start time
  headstartMillis = millis();  //initial start time
    for (count=0;count<2;count++) {
    pinMode(pinArray[count], OUTPUT);
 // Start off with the LED off.
 setColourRgb(0,0,0);
    }
pinMode(STEPPER_PIN_1, OUTPUT);
pinMode(STEPPER_PIN_2, OUTPUT);
pinMode(STEPPER_PIN_3, OUTPUT);
pinMode(STEPPER_PIN_4, OUTPUT);
}

void loop() {
  currentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)
  Blink();
  Head();
}

void Blink(){
  
   if (currentMillis - blinkstartMillis >= blinkperiod)  //test whether the period has elapsed
  {
 delay(random(minDelay, maxDelay));
 unsigned int rgbColour[3];
 // Start off with red.
 rgbColour[0] = 255;
 rgbColour[1] = 0;
 rgbColour[2] = 0;  
 // Choose the colours to increment and decrement.
 for (int decColour = 0; decColour < 3; decColour += 1) {
   int incColour = decColour == 2 ? 0 : decColour + 1;
   // cross-fade the two colours.
   for(int i = 0; i < 255; i += 1) {                                                                                                                                                                                                                                                                                                                                                                          
     rgbColour[decColour] -= 1;
     rgbColour[incColour] += 1;
     setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
     delay(5);
   }
}
 for (count=0;count<1;count++) {
   digitalWrite(pinArray[count], HIGH);
   delay(random(minDelay, maxDelay));
   digitalWrite(pinArray[count + 1], HIGH);
   delay(random(minDelay, maxDelay));
   digitalWrite(pinArray[count], LOW);
   delay(random(minDelay, maxDelay));
  }
  for (count=1;count>0;count--) {
   digitalWrite(pinArray[count], HIGH);
   delay(random(minDelay, maxDelay));
   digitalWrite(pinArray[count - 1], HIGH);
   delay(random(minDelay, maxDelay));
   digitalWrite(pinArray[count], LOW);
   delay(random(minDelay, maxDelay));
  }
void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
 analogWrite(redPin, red);
 analogWrite(bluePin, blue);
  }
   blinkstartMillis = currentMillis;  //IMPORTANT to save the start time of the current LED state.
  }
}
 void Head(){
  if (currentMillis - headstartMillis >= headperiod)  //test whether the period has elapsed
  {
 for(int a = 0; a < 14000; a++){
  OneStep(false);
  delay(2);
 }
 for(int a = 0; a < 14000; a++){
  OneStep(true);
  delay(2);
    }
void OneStep(bool dir){
    if(dir){
switch(step_number){
  case 0:
  digitalWrite(STEPPER_PIN_1, HIGH);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 1:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, HIGH);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 2:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, HIGH);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 3:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, HIGH);
  break;
} 
  }else{
    switch(step_number){
  case 0:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, HIGH);
  break;
  case 1:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, HIGH);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 2:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, HIGH);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 3:
  digitalWrite(STEPPER_PIN_1, HIGH);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, LOW);
} 
  }
step_number++;
  if(step_number > 3){
    step_number = 0;
    }
  }
  headstartMillis = currentMillis;  //IMPORTANT to save the start time of the current LED state.
 }
 }

correct me if I'm wrong here but I correctly declared all the listed errors


these are my errors



C:\Users\ErikD\Documents\Arduino\final_headr2d2\final_headr2d2.ino: In function 'void setup()':
final_headr2d2:22:3: error: 'blinkstartMillis' was not declared in this scope
blinkstartMillis = millis(); //initial start time
^~~~~~~~~~~~~~~~
C:\Users\ErikD\Documents\Arduino\final_headr2d2\final_headr2d2.ino:22:3: note: suggested alternative: 'blinkStartMillis'
blinkstartMillis = millis(); //initial start time
^~~~~~~~~~~~~~~~
blinkStartMillis
final_headr2d2:23:3: error: 'headstartMillis' was not declared in this scope
headstartMillis = millis(); //initial start time
^~~~~~~~~~~~~~~
C:\Users\ErikD\Documents\Arduino\final_headr2d2\final_headr2d2.ino:23:3: note: suggested alternative: 'headStartMillis'
headstartMillis = millis(); //initial start time
^~~~~~~~~~~~~~~
headStartMillis
final_headr2d2:27:2: error: 'setColourRgb' was not declared in this scope
setColourRgb(0,0,0);
^~~~~~~~~~~~
C:\Users\ErikD\Documents\Arduino\final_headr2d2\final_headr2d2.ino: In function 'void Blink()':
final_headr2d2:43:24: error: 'blinkstartMillis' was not declared in this scope
if (currentMillis - blinkstartMillis >= blinkperiod) //test whether the period has elapsed
^~~~~~~~~~~~~~~~
C:\Users\ErikD\Documents\Arduino\final_headr2d2\final_headr2d2.ino:43:24: note: suggested alternative: 'blinkStartMillis'
if (currentMillis - blinkstartMillis >= blinkperiod) //test whether the period has elapsed
^~~~~~~~~~~~~~~~
blinkStartMillis
final_headr2d2:58:6: error: 'setColourRgb' was not declared in this scope
setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
^~~~~~~~~~~~
C:\Users\ErikD\Documents\Arduino\final_headr2d2\final_headr2d2.ino:58:6: note: suggested alternative: 'decColour'
setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
^~~~~~~~~~~~
decColour
final_headr2d2:78:76: error: a function-definition is not allowed here before '{' token
void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
^
C:\Users\ErikD\Documents\Arduino\final_headr2d2\final_headr2d2.ino: In function 'void Head()':
final_headr2d2:86:23: error: 'headstartMillis' was not declared in this scope
if (currentMillis - headstartMillis >= headperiod) //test whether the period has elapsed
^~~~~~~~~~~~~~~
C:\Users\ErikD\Documents\Arduino\final_headr2d2\final_headr2d2.ino:86:23: note: suggested alternative: 'headStartMillis'
if (currentMillis - headstartMillis >= headperiod) //test whether the period has elapsed
^~~~~~~~~~~~~~~
headStartMillis
final_headr2d2:89:3: error: 'OneStep' was not declared in this scope
OneStep(false);
^~~~~~~
final_headr2d2:93:3: error: 'OneStep' was not declared in this scope
OneStep(true);
^~~~~~~
final_headr2d2:96:23: error: a function-definition is not allowed here before '{' token
void OneStep(bool dir){
^
exit status 1
'blinkstartMillis' was not declared in this scope


can someone point me in the right direction? I'll update you if the program runs, and then again if it works in practice. Thank you for your time!

also FYI I'm sorta kinda a novice and I also haven't coded in awhile.
 

Thread Starter

Killerbee65

Joined May 15, 2017
256
The reference in the error has a lower case 's'. The declaration at the top of the program has an upper xase 'S'.

thank you for the fast reply, I did what you said and corrected the mistake (I'm so embarrassed). Apparently now my "setCoulourRGB" wont work, I realized I initialed the function after declaring it in the first function so I moved the whole function to the top of the code right before my "void setup" but it still says I need to declare it
 

Thread Starter

Killerbee65

Joined May 15, 2017
256
ps. this is for me just in case I lose the code again.

Code:
#include <Stepper.h>
//Written By Nikodem Bartnik - nikodembartnik.pl
#define STEPPER_PIN_1 8
#define STEPPER_PIN_2 7
#define STEPPER_PIN_3 6
#define STEPPER_PIN_4 5
#define motorSteps 400

int pinArray[] = {13, 12};
int count = 0;
float minDelay = 125;
float maxDelay = 250;
const int redPin = 11;
const int bluePin = 10;
int step_number = 0;
unsigned long blinkStartMillis;
unsigned long headStartMillis;
unsigned long currentMillis;
const unsigned long blinkperiod = 1000;  //the value is a number of milliseconds, ie 1 second
const unsigned long headperiod = 1000;  //the value is a number of milliseconds, ie 1 second

  void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
 analogWrite(redPin, red);
 analogWrite(bluePin, blue);
  }

void setup() {
  blinkStartMillis = millis();  //initial start time
  headStartMillis = millis();  //initial start time
    for (count=0;count<2;count++) {
    pinMode(pinArray[count], OUTPUT);
 // Start off with the LED off.
 setColourRgb(0,0,0);
    }
pinMode(STEPPER_PIN_1, OUTPUT);
pinMode(STEPPER_PIN_2, OUTPUT);
pinMode(STEPPER_PIN_3, OUTPUT);
pinMode(STEPPER_PIN_4, OUTPUT);
}

void loop() {
  currentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)
  Blink();
  Head();
}

void Blink(){
  
   if (currentMillis - blinkStartMillis >= blinkperiod)  //test whether the period has elapsed
  {
 delay(random(minDelay, maxDelay));
 unsigned int rgbColour[3];
 // Start off with red.
 rgbColour[0] = 255;
 rgbColour[1] = 0;
 rgbColour[2] = 0;  
 // Choose the colours to increment and decrement.
 for (int decColour = 0; decColour < 3; decColour += 1) {
   int incColour = decColour == 2 ? 0 : decColour + 1;
   // cross-fade the two colours.
   for(int i = 0; i < 255; i += 1) {                                                                                                                                                                                                                                                                                                                                                                          
     rgbColour[decColour] -= 1;
     rgbColour[incColour] += 1;
     setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
     delay(5);
   }
}
 for (count=0;count<1;count++) {
   digitalWrite(pinArray[count], HIGH);
   delay(random(minDelay, maxDelay));
   digitalWrite(pinArray[count + 1], HIGH);
   delay(random(minDelay, maxDelay));
   digitalWrite(pinArray[count], LOW);
   delay(random(minDelay, maxDelay));
  }
  for (count=1;count>0;count--) {
   digitalWrite(pinArray[count], HIGH);
   delay(random(minDelay, maxDelay));
   digitalWrite(pinArray[count - 1], HIGH);
   delay(random(minDelay, maxDelay));
   digitalWrite(pinArray[count], LOW);
   delay(random(minDelay, maxDelay));
  }
   blinkStartMillis = currentMillis;  //IMPORTANT to save the start time of the current LED state.
  }
}
void OneStep(bool dir){
    if(dir){
switch(step_number){
  case 0:
  digitalWrite(STEPPER_PIN_1, HIGH);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 1:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, HIGH);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 2:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, HIGH);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 3:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, HIGH);
  break;
} 
  }else{
    switch(step_number){
  case 0:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, HIGH);
  break;
  case 1:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, HIGH);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 2:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, HIGH);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 3:
  digitalWrite(STEPPER_PIN_1, HIGH);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, LOW);
} 
  }
step_number++;
  if(step_number > 3){
    step_number = 0;
    }
  }
 void Head(){
  if (currentMillis - headStartMillis >= headperiod)  //test whether the period has elapsed
  {
 for(int a = 0; a < 14000; a++){
  OneStep(false);
  delay(2);
 }
 for(int a = 0; a < 14000; a++){
  OneStep(true);
  delay(2);
    }

  headStartMillis = currentMillis;  //IMPORTANT to save the start time of the current LED state.
  }
 }
 

WBahn

Joined Mar 31, 2012
29,978
Perhaps your biggest mistake is writing well over a hundred lines of code and then seeing if it will even compile. You will be much more productive, to say nothing of less frustrated, if you compile much more frequently. If you've written more than a screen full of new code, you've probably gone too long without compiling. You should also test each non-trivial function once you've written -- in fact, write a test function for the function before you write the function. That will force you to think in very specific terms about the interface to the function and whether that interface can really accomplish what you need. Once you've written and tested the function, you now have a test function that you can run at anytime and a good strategy is to build a test suite that runs all of the individual test function and then periodically run that against your code base. That way if you change a function in a way that has an unintended consequence, your test suite has a high likelihood of catching it and pinpointing the function that is the culprit almost immediately.
 

Thread Starter

Killerbee65

Joined May 15, 2017
256
Perhaps your biggest mistake is writing well over a hundred lines of code and then seeing if it will even compile. You will be much more productive, to say nothing of less frustrated, if you compile much more frequently. If you've written more than a screen full of new code, you've probably gone too long without compiling. You should also test each non-trivial function once you've written -- in fact, write a test function for the function before you write the function. That will force you to think in very specific terms about the interface to the function and whether that interface can really accomplish what you need. Once you've written and tested the function, you now have a test function that you can run at anytime and a good strategy is to build a test suite that runs all of the individual test function and then periodically run that against your code base. That way if you change a function in a way that has an unintended consequence, your test suite has a high likelihood of catching it and pinpointing the function that is the culprit almost immediately.

I did do it that way, the problem came when I merged to separate codes together and tried to move blocks of code around. after this was a success I found both codes didn't function at the same now so I needed to come up with a way to have both codes run at the same time but in intervals of milliseconds (gives the illusion its all working at the same time). After also achieving this I needed to once again move blocks around and then I took a hiatus from coding. Present-day and I am playing catch up with my old self (ironic isn't it?). Also, I just learned about test suites so back then I just sat down and started coding. I made updates to the code since posting and it compiles just fine, the problem now is that the two separate codes (combined with the 3rd "joining code" that makes both codes run "at the same time") are a little buggy. The RGB led only transitions between red and blue when the step motor reaches its climax and there are these two blue LEDs I have that are supposed to light up at random times but only one lights up and it's at the same time as when the step motor climaxes. I should also note that while yes the RGB led does stay on throughout the run time, it only stays red and turns blue only when the step motor climaxes. I am currently investigating the problem and ask for your help!
 

WBahn

Joined Mar 31, 2012
29,978
It sounds like the crux of the problem might be in how you are getting the blocks of code to run concurrently. It might be worthwhile to set the current code aside and develop a new program that focuses on this specific aspect and in which the blocks being run are extremely simple -- an LED in one and a switch in the other, perhaps. That will let you focus on the concurrency issues and getting things to play nice in that regard. Once you have that licked, then review your other code with that new knowledge. Maybe you can save that code or maybe it will be better to start over and do it "right". This is known as "code refactoring" and it is a pretty common part of the software development process.
 
Top