it was the first portion of the code, since it was too long, I have to post it in 2 parts..
below is the rest of the code..
void loop() {
static int rangeVal;
{
// Start a range measurement in single shot mode//
WriteByte(0x018, 0x01);
// poll for new sample ready ready //
VL6180X_Poll_Range();
// delay(50);
Serial.print("Stage:");
Serial.println(next_stage);
switch (next_stage)
{
case getreading: // stage 1
rangeVal = readByte(0x062); //read the sensor value
Serial.print(rangeVal); //print the sensor value
Serial.println("mm"); //print the mm
if (rangeVal<=150 && rangeVal>50 ) { //turn on the pump below 15 cm
counter++; //starts counter at 0 and increase it by 1 step // prevents falls triggering when something swipes below the sensor above some speed
Serial.println(counter);
Serial.print("millis() :");
Serial.println(millis());
//digitalWrite(42,HIGH);
//digitalWrite(42,LOW);
rangeVal = readByte(0x062); //read the sensor value
if(counter == 12) // hand should remain in sensor area for counterx27 milliseconds
{
counter = 0; //make the counter 0
digitalWrite(D4,HIGH); //pump LED high
digitalWrite(D8,HIGH); //pump high
Serial.println("pump is high");
next_stage= valveon;}
}
else {
counter = 0; //makes the counter 0
digitalWrite(D4,LOW); //pump LED low
digitalWrite(D8,LOW); //pump low
digitalWrite(D6,LOW); //turn off valve LED
digitalWrite(D10,LOW); //turn off valve
next_stage=getreading;}
break;
case wait: //stage 2
digitalWrite(D4,LOW); //pump LED low
digitalWrite(D8,LOW); //pump low
counter2 = 0; //makes the counter2 0
rangeVal = readByte(0x062);
if (rangeVal <100){ // to spray even if pump is turned off
next_stage= get_time;
}
if (rangeVal > 180)
{
next_stage = getreading; //if no hand, jumps to stage 1
}
break;
case valveon: //stage 3
rangeVal = readByte(0x062); //read the value from sensor
Serial.print(rangeVal); //print the sensor value
Serial.println("mm"); //print mm
if (rangeVal<=100 ){ //&& rangeVal>=50 ) {
next_stage=get_time; // to turn the valve on
}
else if (rangeVal<150 && rangeVal>100)
{
next_stage=valveon; //will stay in same stage
counter2++; //turn off the pump after sometime if hand remain between 10 and 15cm
Serial.println(counter2);
Serial.print("millis()stage3 :");
Serial.println(millis());
if (counter2 >= 15) // to turn the pump off after counter2x42 milliseconds
{
next_stage = wait; //jumps to stage 2
}
}
else
{
next_stage=getreading; //jumps to stage 1
}
break;
case get_time: //stage 4
if (analogRead(A0)>700 ){ //valve open for 250 milliseconds
targettime=millis()+250;
}
else if (500<analogRead(A0) && analogRead(A0)<600 ){ //valve open for 500 milliseconds
targettime=millis()+500;
}
else if (300<analogRead(A0) && analogRead(A0)<400 ){ //valve open for 750 milliseconds
targettime=millis()+750;
}
else if (200<analogRead(A0) && analogRead(A0)<300){ //valve open for 1000 milliseconds
targettime=millis()+1000;
}
else if (100<analogRead(A0) && analogRead(A0)<200 ){ //valve open for 1250 milliseconds
targettime=millis()+1250;
}
else if (analogRead(A0)<100 ){ //valve open for 1500 milliseconds
targettime=millis()+1500;
}
next_stage=check_time;
digitalWrite(D6,HIGH); //valve high LED
digitalWrite(D10,HIGH); //valve high
break;
case check_time: //stage 5
rangeVal = readByte(0x062);
Serial.print (rangeVal);
Serial.println (" mmstage5" ); // reads the sensor value
if (millis() > targettime || rangeVal>=150 || rangeVal<50) // set the condition when valve goes off
{//Serial.print("millis 1:"); //checks when valves goes off
//Serial.println(millis());
digitalWrite(D6,LOW); //valve LED off
digitalWrite(D10,LOW); //Valve off
next_stage=pumpoff;
}
break;
case pumpoff: //stage 6
rangeVal = readByte(0x062); // measureas the sensor value
Serial.print (rangeVal);
Serial.println (" mmstage6" );
if (counter4 == 0) // checks the counter4 if it is 0 counter 4: evertime ,turn off the pump after valve closes
{
waittime = millis() + 100; // set wait time for 100 milliseconds
counter4 = 1; // makes the counter4 equal to 1
}
if ( millis() >= waittime) // sets the condition when pump goes off after valve goes off
{//Serial.print("millis 2:"); // prints millis
//Serial.println(millis());
digitalWrite(D8,LOW); //pump off
digitalWrite(D4,LOW); //pump led off
if (rangeVal > 180) // if no hand
{
counter4 = 0; // counter4 equal to 0
next_stage=getreading; //jumps to stage 1
}
}
break;
default:
// clear interrupts //
WriteByte(0x015,0x07);
delay (100); // wait 0.1 seconds}
}}} // end of loop
vl6180x application note.pdf (690 KB)