Code Error

I have a problem with my code , it say "ReadDistance" was not declared in this scope. I dont understand , i tried put " int x; " but didnt work.
Can someone help , I have to make a presentation for my project next week bruhhh

#include <Wire.h>
 
unsigned char ok_flag;
unsigned char fail_flag;
 
unsigned short lenth_val = 0;
unsigned char i2c_rx_buf[16];
unsigned char dirsend_flag=0;


const int echoPin = 2;
const int trigPin = 3;
const int buzzer1 = 4;
const int buzzer2 = 5; 
const int ledRed = 8;
const int ledYellow = 9;
const int ledGreen = 10;
const int relay = 11;

void setup() {
  Wire.begin(); 
  Serial.begin(9600,SERIAL_8N1); 
  printf_begin();          
 
}
 
void loop() {
  
 
   int x=ReadDistance();
   
   Serial.print(x);
   Serial.println(" mm");
  
}
 
int serial_putc( char c, struct __file * )
{
  Serial.write( c );
  return c;
}
 
void printf_begin(void)
{
  fdevopen( &serial_putc, 0 );
}
 
if (x <=30 && x >20 )
{
  digitalWrite(10,HIGH);
}
else if (x <= 20 && x >10)
{
  digitalWrite(10,LOW);
  digitalWrite(buzzer1,HIGH);
  digitalWrite(9,HIGH);
}
else if(x <=10)
{
  digitalWrite(buzzer1,LOW);
  digitalWrite(9,LOW);
  digitalWrite(buzzer2,HIGH);
  digitalWrite(8,HIGH);
  digitalWrite(11,HIGH);
}
else
{
  digitalWrite(buzzer2,LOW);
  digitalWrite(8,LOW);
  digitalWrite(11,LOW);
}
delay (100);
}

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
  }
}
 
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(300); 
    return lenth_val;
}
if (x <= 30 && x > 20 )

This program statement, and many more, are not in a function

You also may want to try to define your functions above setup and loop. The code is looking for a function that has not yet been defined. Or could initiate the function above setup and loop.

queenlexi13:
You also may want to try to define your functions above setup and loop. The code is looking for a function that has not yet been defined. Or could initiate the function above setup and loop.

You're new to Arduino, aren't you?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.