As already mentioned post your sketch, well formated, with comments and in so called code tags "</>" and a none-Fritzing schematic to see how we can help.
Have a nice day and enjoy programming in C++ and learning.
Well, the code is here, extracted from the zip file in post #1, but the OP has not cleaned up the compiler errors yet:
#include <Wire.h>
unsigned short lenth_val = 0;
unsigned char i2c_rx_buf[16];
int manualbut = 4;
int valveon = 8;
int valveoff = 9;
int buttonState = 0;
int PreparingToFlush = 0;
int count = 0;
void setup()
{
Wire.begin();
//Serial.begin(9600);
pinMode(valveon, OUTPUT);
pinMode(valveoff, OUTPUT);
pinMode(microwave, INPUT);
pinMode(manualbut, INPUT_PULLUP);
digitalWrite(valveon, LOW);
digitalWrite(valveoff, HIGH);
delay(1000);
digitalWrite(valveon, HIGH);//LOW = ON / HIGH = OFF
digitalWrite(valveoff, HIGH);//LOW = ON / HIGH = OFF
//Serial.println("Initializing... Valve OFF!");
}
//###################### ADJUST SENSOR READING TIME
int ReadDistance() {
SensorRead(0x00, i2c_rx_buf, 2);
lenth_val = i2c_rx_buf[0];
lenth_val = lenth_val << 8; lenth_val |= i2c_rx_buf[1];
delay(500);
return lenth_val;
}
void loop() {
//###################### SENSOR MEASURING ALGORITHM
int x = ReadDistance();
//Serial.print(x);
//Serial.println(" mm");
//###################### ADJUST TRIGGER DISTANCE
if (x < 500) {
//Serial.println("DETECTED HUMAN, AUTHORIZE TO FLUSH");
PreparingToFlush = 1;
}
//###################### ADJUST DISTANCE CONSIDER HUMAN LEFT
if (PreparingToFlush == 1 && x > 500) { //if a person has been detected
//Serial.println(" HUMAN LEFT,AUTO FLUSHING for 5 SEC!!");
digitalWrite(valveon, HIGH);
digitalWrite(valveoff, LOW);
//###################### ADJUST FLUSHING TIME
delay(5000);
digitalWrite(valveon, LOW);//LOW = ON / HIGH = OFF
digitalWrite(valveoff, HIGH);//LOW = ON / HIGH = OFF
delay(500);
digitalWrite(valveon, HIGH);//LOW = ON / HIGH = OFF
digitalWrite(valveoff, HIGH);//LOW = ON / HIGH = OFF
//Serial.println("FINISH AUTO FLUSHING!!");
PreparingToFlush = 0; //reset the trigger
//Serial.println("TIMEOUT for 3 second before next trigger!");
//###################### ADJUST MINIMUM TIME WHEN USER ABLE TO TRIGGER THE SENSOR AGAIN
//delay(3000);
x = 2000;
}
//###################### MANUAL FLUSHING
int sensorVal = digitalRead(4);
if (sensorVal == LOW)
{
//Serial.println("MANUAL FLUSHING!!");
digitalWrite(valveon, HIGH);
digitalWrite(valveoff, LOW);
delay(8000);
digitalWrite(valveon, LOW);
digitalWrite(valveoff, HIGH);
//Serial.println("FINISH MANUAL FLUSHING!!");
delay(500);
digitalWrite(valveon, HIGH);//LOW = ON / HIGH = OFF
digitalWrite(valveoff, HIGH);//LOW = ON / HIGH = OFF
PreparingToFlush = 0; //reset the trigger
x = 2000;
}
}
void SensorRead(unsigned char addr, unsigned char* datbuf, unsigned char cnt)
{
unsigned short result = 0;
// step 1: instruct sensor to read echoes
Wire.beginTransmission(82); // transmit to device #82 (0x52)
// the address specified in the datasheet is 164 (0xa4)
// but i2c adressing uses the high 7 bits so it's 82
Wire.write(byte(addr)); // sets distance data address (addr)
Wire.endTransmission(); // stop transmitting
// step 2: wait for readings to happen
delay(1); // datasheet suggests at least 30uS
// step 3: request reading from sensor
Wire.requestFrom(82, cnt); // request cnt bytes from slave device #82 (0x52)
// step 5: receive reading from sensor
if (cnt <= Wire.available()) { // if two bytes were received
*datbuf++ = Wire.read(); // receive high byte (overwrites previous reading)
*datbuf++ = Wire.read(); // receive low byte as lower 8 bits
}
}
That looks OK as far as I can see. I'd say though that that the code in ReadDistance() should be in SensorRead() and ReadDistance() dropped altogether.
Since it appears to work, but erratically, I'd first start suspecting I2C pull up resistors. Without knowing what the I2C sensor is I can only say that using an int16_t (instead of a uint16_t) to swap two bytes looks odd but may be correct.
Can you please post links to spec of the sensor and the output device you are controlling?
Thanks for the diagram, but a proper schematic labeling all components and labels on pins would be helpful.
Please include your power supply and its specs.
It's ok to post a Fritzing schematic, if Fritzing is what you prefer to use. What @paulpaulson should have said was to use the Schematic View of Fritzing, not the Breadboard View, which many beginners post because they do not understand the difference between the two views.
Hi, here are the specs and working flow for my toilet flush project
Automatic Flush Mode
1)TOF10120 Sensor measuring distance every 500ms ( range of detection 500mm)
2)If human detected at < 500mm, prepare to flush
3)Human left, detection > 500mm, Relay switch +5v to solenoid valve and flush for 5sec
4)After 5 seconds, Relay switch -5v to solenoid valve to close the valve
Manual Flush mode
1)Human pressed manual flush button
2)Relay switch +5v to solenoid valve and flush for 8sec
3) After 8 seconds, Relay switch -5v to solenoid valve to close the valve
#include <Wire.h>
unsigned short lenth_val = 0;
unsigned char i2c_rx_buf[16];
int manualbut = 4;
int valveon = 8;
int valveoff = 9;
int buttonState = 0;
int PreparingToFlush=0;
int count=0;
void setup()
{
Wire.begin();
//Serial.begin(9600);
pinMode(valveon, OUTPUT);
pinMode(valveoff, OUTPUT);
pinMode(manualbut, INPUT_PULLUP);
digitalWrite(valveon, LOW);
digitalWrite(valveoff, HIGH);
delay(1000);
digitalWrite(valveon, HIGH);//LOW = ON / HIGH = OFF
digitalWrite(valveoff, HIGH);//LOW = ON / HIGH = OFF
//Serial.println("Initializing... Valve OFF!");
}
//###################### ADJUST SENSOR READING TIME
int ReadDistance(){
SensorRead(0x00,i2c_rx_buf,2);
lenth_val=i2c_rx_buf[0];
lenth_val=lenth_val<<8; lenth_val|=i2c_rx_buf[1];
delay(500);
return lenth_val;
}
void loop() {
//###################### SENSOR MEASURING ALGORITHM
int x=ReadDistance();
//Serial.print(x);
//Serial.println(" mm");
//###################### ADJUST TRIGGER DISTANCE
if(x < 500) {
//Serial.println("DETECTED HUMAN, AUTHORIZE TO FLUSH");
PreparingToFlush=1;
}
//###################### ADJUST DISTANCE CONSIDER HUMAN LEFT
if (PreparingToFlush==1 && x > 500){ //if a person has been detected
//Serial.println(" HUMAN LEFT,AUTO FLUSHING for 5 SEC!!");
digitalWrite(valveon, HIGH);
digitalWrite(valveoff, LOW);
//###################### ADJUST FLUSHING TIME
delay(5000);
digitalWrite(valveon, LOW);//LOW = ON / HIGH = OFF
digitalWrite(valveoff, HIGH);//LOW = ON / HIGH = OFF
delay(500);
digitalWrite(valveon, HIGH);//LOW = ON / HIGH = OFF
digitalWrite(valveoff, HIGH);//LOW = ON / HIGH = OFF
//Serial.println("FINISH AUTO FLUSHING!!");
PreparingToFlush=0; //reset the trigger
//Serial.println("TIMEOUT for 3 second before next trigger!");
//###################### ADJUST MINIMUM TIME WHEN USER ABLE TO TRIGGER THE SENSOR AGAIN
//delay(3000);
x = 2000;
}
//###################### MANUAL FLUSHING
int sensorVal = digitalRead(4);
if(sensorVal == LOW)
{
//Serial.println("MANUAL FLUSHING!!");
digitalWrite(valveon, HIGH);
digitalWrite(valveoff, LOW);
delay(8000);
digitalWrite(valveon, LOW);
digitalWrite(valveoff, HIGH);
//Serial.println("FINISH MANUAL FLUSHING!!");
delay(500);
digitalWrite(valveon, HIGH);//LOW = ON / HIGH = OFF
digitalWrite(valveoff, HIGH);//LOW = ON / HIGH = OFF
PreparingToFlush=0; //reset the trigger
x = 2000;
}
}
void SensorRead(unsigned char addr,unsigned char* datbuf,unsigned char cnt)
{
unsigned short result=0;
// step 1: instruct sensor to read echoes
Wire.beginTransmission(82); // transmit to device #82 (0x52)
// the address specified in the datasheet is 164 (0xa4)
// but i2c adressing uses the high 7 bits so it's 82
Wire.write(byte(addr)); // sets distance data address (addr)
Wire.endTransmission(); // stop transmitting
// step 2: wait for readings to happen
delay(1); // datasheet suggests at least 30uS
// step 3: request reading from sensor
Wire.requestFrom(82, cnt); // request cnt bytes from slave device #82 (0x52)
// step 5: receive reading from sensor
if (cnt <= Wire.available()) { // if two bytes were received
*datbuf++ = Wire.read(); // receive high byte (overwrites previous reading)
*datbuf++ = Wire.read(); // receive low byte as lower 8 bits
}
}
Here's the compile message:
In file included from XXXXXXXX.ino:1:0:
C:\Users\XXXXXXX\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\Wire\src/Wire.h: In function 'void SensorRead(unsigned char, unsigned char*, unsigned char)':
C:\Users\XXXXXXX\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\Wire\src/Wire.h:68:13: note: candidate 1: uint8_t TwoWire::requestFrom(int, int)
uint8_t requestFrom(int, int);
^~~~~~~~~~~
C:\Users\XXXXXXX\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\Wire\src/Wire.h:65:13: note: candidate 2: uint8_t TwoWire::requestFrom(uint8_t, uint8_t)
uint8_t requestFrom(uint8_t, uint8_t);
^~~~~~~~~~~
Sketch uses 3510 bytes (11%) of program storage space. Maximum is 30720 bytes.
Global variables use 245 bytes (11%) of dynamic memory, leaving 1803 bytes for local variables. Maximum is 2048 bytes.
Switch case
Waiting
{do whatever to sense humans and poll buttons and if detected state = detected or flush
Human detected
{once human no longer detected and whatever other conditions are met or button pressed state =flush}
Flush
{whatever to flush and state = waiting}