I am trying to learn various ways of reducing the number of lines in a sketch and want to use a function that can be called with different variables. I have been struggling with part of this sketch for a while.
//undersatanding functions
int a[]={1,2,4,8,16};
int i;
int b = 0;
int c = 0;
int d = 0;
//int test;
void setup() {
// put your setup code here, to run once:
Serial.begin (9600);
}
void loop() {
// put your main code here, to run repeatedly:
b++;
c=(b+7);
for (int d=0;d<5;d++){
test (b,c,d);
}
}
void test(int numb, int numb2, int numb3){
for (i=0;i<5;i++) {
Serial.print (a[i]);
Serial.print (" ");
}
Serial.print (numb);
Serial.print (" ");
Serial.print (numb2);
Serial.print (" ");
Serial.print (numb3);
Serial.print (" ");
Serial.print (b);
Serial.print (" ");
Serial.println (d);
delay (500);
}
variable d increments in the loop function and when sent to the test function as part of 'test(b,c,d)' is replaced with numb3 which prints out as an incrementing number 0 to 4 as it should.
I get
1 2 4 8 16 1 8 0 1 0
1 2 4 8 16 1 8 1 1 0
1 2 4 8 16 1 8 2 1 0
1 2 4 8 16 1 8 3 1 0
1 2 4 8 16 1 8 4 1 0
1 2 4 8 16 2 9 0 2 0
1 2 4 8 16 2 9 1 2 0
Serial.print(b) prints the same as 'numb' but the last 'Serial.println' should just print 'd' the same as 'numb3' (the variable d, 0 to 4) but it just stays at 0, anyone know why?