Compare two arrays

Hello everyone.

I am working with an ADJDS311 from Sparkfun Electronics to make a turn counter, I have a black wheel with a white strip in it and what I am trying to do is to count the turns, when the sensor detects the white color count =1 if detect it again: count =2, but if the sensor stops in the white strip stop counting and keeps with the last value. My attempt to do this is, first, declare variables, I want to declare x as the index with a value of 0, also I declare my array as myArray[0];. In void loop() I use an if to compare the actual value of the sum of color.red + color.blue with the value before (x-1).

#include <ADJDS311.h>
#include <Wire.h>


int myArray[0];
int count=0;
int x=0;
int sensorLed_pin = 2; //LED on the ADJDS-311
ADJDS311 colorSensor(sensorLed_pin);

//if using an RGB LED (Needs PWM Pins)
//int redPin = 3;
//int greenPin = 5;
//int bluePin = 6;

void setup(){
 Serial.begin(9600);
 
 colorSensor.init();
colorSensor.ledOn(); //turn LED on
 
 //Calibrate white 
 //Need to hold white card in front (1-3mm) of it to calibrate from
 colorSensor.calibrate(); 
 Serial.print(count);
}

void loop(){

 RGBC color = colorSensor.read(); //read the color
  int colors = color.red + color.blue;
 lightLED(color); //send color to the LED
 
 delay(1000); //just here to slow down the serial output
 
myArray[x]=colors;
 
 if (myArray[x] > 2000 && myArray[x] != myArray[x-1]){
   
unsigned long count=count+1;
 }
unsigned long x=x+1;
 Serial.print(count);

}

void lightLED(RGBC color){
 //RGBC is an array of red/green/blue/clear readings 
 //Take a RGBC, and try to reproduce it on an RGB LED
 //This does not work very well as is because of how colors/our eyes work
 
// analogWrite(redPin, map(color.red, 0, 1024, 0, 255));
// analogWrite(greenPin, map(color.green, 0, 1024, 0, 255));
// analogWrite(bluePin, map(color.blue, 0, 1024, 0, 255));
}

But the results I receive are unexpected I do not understand why they appear.

init02046
2046
2046
2046
2046
2046
2046
2046 //It seems that this value is the sum of color.red+color.blue of the white strip in the wheel
2046
2046
2046
2046
2046
2046
408
409
408
410
410
410
409
411  //It seems that this is the sum of the color.red+color.blue of the black color of the wheel 
410
410
409
408
2046
2046
2046
2046
2046
2046
2046
2046
2046
2046
2046
2046

The code I use as reference is in http://bildr.org/2012/01/adjd-s311_arduino/

I post here because I need help to solve this problem and because this is a project that, if works, would have applications like an easy attachable speedometer in a customized car.

Thanks.

Hello,

Read about arrays and variable declaration, you got it all wrong :slight_smile:

http://www.cplusplus.com/doc/tutorial/arrays/
http://www.cplusplus.com/doc/tutorial/variables/

int myArray[0];

An array with 0 elements, let us know how that works out...oh hang on, you have :slight_smile:


Rob