Hey guys, i'm new to arduino, I'm trying to understand how arrays are used in C++, i've seen a lot of threads on this topic but I can't seem to find an answer to my issue
My goal is to write a simple library for LEDs and Switches, i wanna pass an array as the first argument and the size as the second, then call a function that will turn off all leds except the one i passed in the argument.
For starters i wrote a library just to get an idea on how to work with arrays but for some reason the items in the array are all messy
this is my code for the ArrayTest Object
ArrayTest.h
#ifndef ArrayTest_h
#define ArrayTest_h
#include "Arduino.h"
class ArrayTest {
public:
ArrayTest(int _items[], int _n);
int *items[];
int n;
int getItem(int _index);
};
#endif
ArrayTest.cpp
#include "Arduino.h"
#include "ArrayTest.h"
ArrayTest::ArrayTest (int _items[], int _n){
*items = _items;
//n = _n;
}
int ArrayTest::getItem(int _index){
return items[_index];
}
.ino
#include <ArrayTest.h>
int vars[] = {20,22,24,26,28,30};
int count = 6;
ArrayTest arr = ArrayTest(vars,count);
void setup() {
Serial.begin(9600);
Serial.println("running");
for(int i =0; i <6;i++){
Serial.println(arr.getItem(i));
}
}
void loop() {
// put your main code here, to run repeatedly:
}
With that code the serial output will be
running
512
32379
-12866
-1128
-20746
-8649
Then if i uncomment "//n = _n;" in the ArrayTest.cpp file my output becomes
running
6
32379
-12866
-1128
-20746
-8649
now the first item in the array is the value of "n" (6)
I'm coming from PHP and javascript so the concept of pointers is still new to me but based on some threads i've read i saw that you have to add a pointer for "items[]"
If you guys could point me in the right direction it would be greatly appreciated
Thanks in advance!