/* This sketch runs on an ATtiny85 connected to an Arduino running w13_satshakit_master.ino The data received is from a HC-SR04 sensor */ #include #include // Requires fork by Rambo with onRequest support const int I2CSlaveAddress = 7; const int RX = 1; // NO RX for Sonar application const int TX = 2; // TX (data sent by the ATTiny85) SoftwareSerial serial(RX, TX);//rx pin of ftdi header as receiver and mosi as tx const int trigPin = A3; // defines pins numbers const int echoPin = A2; // defines pins numbers long duration; // defines variables int distance; // defines variables #define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. void setup() { pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input pinMode(TX,OUTPUT); // TX serial.begin(9600); // Starts the serial communication delay(100); TinyWireS.begin(I2CSlaveAddress); // Begin I2C Communication TinyWireS.onRequest(transmit); // When requested, call function transmit() } void loop() { // serial.println("Hello"); digitalWrite(trigPin, 0);// Clears the trigPin delayMicroseconds(2000); digitalWrite(trigPin, 1); delayMicroseconds(5); // Sets the trigPin on HIGH state for 5 micro seconds digitalWrite(trigPin, 0); duration = pulseIn(echoPin, 1); // Reads the echoPin, returns the sound wave travel time in microseconds distance = duration * 0.034 / 2; // Calculating the distance if (distance > MAX_DISTANCE) { serial.println("out of range"); } else { serial.print("Distance: "); // Prints the distance on the Serial Monitor serial.print(distance);// Prints the distance on the Serial Monitor serial.println(" Cm");// Prints the distance on the Serial Monitor } delay(2000); } void transmit() { TinyWireS.send(distance); // Send last recorded distance for current sensor }