Arduino uno receiving message and calculating distance

I am planning to let an ATTiny85 to:

  1. Send a message to an Arduino Uno by 315MHz module and VirtualWire
  2. Send a pulse to SR04
  3. If receive a pulse, then send a message to the Arduino Uno.

Code for ATTiny85:

#include <VirtualWire.h>

#define TRIG 2
#define MSG  4
#define ECHO A3

void setup() {
  // put your setup code here, to run once:
  vw_setup(1000);
  pinMode(ECHO,INPUT);
  pinMode(MSG,OUTPUT);
  pinMode(TRIG,OUTPUT);
  vw_set_tx_pin(MSG);
}

void loop() {
  // put your main code here, to run repeatedly:
  send("T1");
  digitalWrite(TRIG,LOW);
  delay(1000);
  digitalWrite(TRIG,HIGH);
  delay(200);
  digitalWrite(TRIG,LOW);
  if (digitalRead(ECHO) == HIGH)
  {
    send("0");
  }
  delay(10000);
}

void send (char *message)
{
vw_send((uint8_t *)message, strlen(message));
vw_wait_tx(); // Wait until the whole message is gone
}

But the Attiny isn't sending 0, just "T1". Where could be the problem?

This all happens long before the ECHO pin goes high, then the processor does nothing for 10 seconds.

Delays are way too long and you should be using pulseIn..
Check this..
ultrasonic sensor demo..

good luck.. ~q

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