I'm working on a script that will drive led's and references to potentiometers and switches based on an interrupt. It's for a voltage controlled synth sequencer.
My idea is each time an interrupt is triggered the sequencer will move forward a step. The script above is for the led's only, but it is the same idea for the pots and switches.
So, basically, each time an interrupt is received I want the array to go from ledArray[0] to ledArray[1] to ledArray[2], etc. However, incrementing the array doesn't seem to be the problem. What data type to store the led variables is.
I know char is a single character, but the char* is confusing. How does it work? Also, why didn't you declare a number in the array brackets.
Making an array of ints that contain the pin numbers would be the best.
Something like:
int RedLED = 2, BlueLED = 4, GreenLED = 6;
int ledArray[] = { RedLED, BlueLED, GreenLED };
Example for loop:
for (int i=0; i < 3; i++)
digitalWrite(ledArray*, HIGH);* In C/C++ a char* is a string. The * means it is a pointer and points to the memory address of the first char of the string. As for the empty square brackets, if you are setting the values then the compiler is smart enough to insert the correct number inside at compile time.
Variable names only exist in the program source in C/C++, they have no meaning at runtime so the short answer is no.
However that's a subtle point, what you practically need to know is that you need to work with the addresses of variables, and learn about the & "address of" operator.
int led00 = 50;
int led01 = 51;
int led02 = 52;
int * ledArray[3] = {&led00, &led01, &led02};
"int *" is the type of a pointer-to-integer, here I create an array of three pointers. Using the pointers I can access the variables (read or write) using the * operator.
What you are doing is creating a series of two byte variables in memory, and loading them with a constant number (50, 51, 52).
Then you create an array of addresses, each entry of which takes two bytes, to hold the address of the two byte variables you created first.
Your code then steps through the array, extracts the address of the two byte variable, goes there, and gets the constant, which is the pin number.
pceric gave you the right answer. Just make an array of pin numbers. Then your loop just steps through the array picking up the pin number directly.
Your way takes twice as much memory, and takes some percentage, maybe 30% longer to execute, and you get nothing for it.
Learning to use pointers is valuable, but don't use pointers when the variable the pointer points to is always constant and all you ever do is run through the list in order extracting the value.
The stepPos() function, as I suspected, was where it was going wrong. By referencing the state of the "state" variable in that function, I was able to fix the problem.
brtech, are you saying I should skip defining the ledPins and just make an array of pin numers like this?