Attiny 85 getting stuck with New Ping library

Status
Not open for further replies.

esapode688

Well-known member
I know is a bit O.T. but the project involves and will always involve a teensy 3.1 and maybe someone here is able to help ( I posted the same on the arduino forum and sent also an email to Tim)

So...

The Attiny 85 get stuck and very quickly using the New Ping library


My setup:

Teensy 3.1 hooked up to an Attiny 85 using I2c

Arduino Version: 1.6.1
Teensyduino Version: 1.21
Sensor: HY-SRF05 Hooked up on PB3 of the attiny
OS: Windows 8.1
Programmer: Tiny programmer
Attiny bootloader: Attiny HIgh Low Tech
Attiny 85 bootloaded at 8mhz internal clock and powered at 5 volt.

On the Attiny I'm using the TinyWire S library : Rambo Tinywire S

I'm running the following Sketch on the attiny 85

Code:
#include <TinyWireS.h>              // Requires fork by Rambo with onRequest support
#include <NewPing.h>            // NewPing library modified for ATtiny

#define SensorPin 3         // Sensor  is connected to PB3
#define ledPin 4            // Led is connected to PB4
#define I2CSlaveAddress 1   // I2C Address.

unsigned int Distance;                   // Where the Distance is stored 
unsigned long previousScan;     // Here we store the last time we checked the cycle

#define MAX_DISTANCE 180 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
#define POLL_TIME 33     // How long do we wait before polling the sensor again in milliseconds
NewPing sonar (SensorPin, SensorPin, MAX_DISTANCE);       // Define the Sensor
 
void setup()
{ 
  delay(I2CSlaveAddress + 2);         // Join the I2C protocol with a slight Delay to avoid latch up
  TinyWireS.begin(I2CSlaveAddress);   // Begin I2C Communication
  TinyWireS.onRequest(transmit);      // When requested, call function transmit()
  previousScan = millis();            // Initialize millis
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  if (millis() - previousScan > POLL_TIME){ //if we are over the last time we checked the sensor
    Distance = sonar.ping_cm();         //We  Get distance from it in cm. 
    if (Distance > 180) Distance = 180;       //Lets limit the distance to avoid latch up
    previousScan = millis();
    if (Distance < 10){
      digitalWrite(ledPin, HIGH);
     }
    else {
      digitalWrite(ledPin, LOW);
     }
  } 
}  //end loop()

void transmit()
{
  TinyWireS.send(Distance);                 // Send last recorded distance for current sensor
}


And I'm Running the following sketch on teensy 3.1 to check the Distance value

Code:
#include <Wire.h>

#define THRESHOLD 10  // Minimum distance to trigger the led (in cm)
#define REQ_ADDRESS 1 // Address to ping for information

byte Distance;        // Unsigned 8 bit variable to store the distance value
byte prevDistance =0;    // Store the previous distance value

void setup(){
  Wire.begin();
  Serial.begin(57600);
  while (!Serial) {
  }
  Serial.println("Setup");
  pinMode(13, OUTPUT);
}

void loop(){ 
  Wire.requestFrom(REQ_ADDRESS, 1);  // The TinyWire library only allows for one byte to be requested at a time
  if (Wire.available() == 1)  ;   // Wait until there is data in the I2C buffer
  Distance = Wire.read();                  // Read the first (and hopefully only) byte in the I2C buffer
  if ((Distance > 0) && (Distance < THRESHOLD)){
     digitalWrite(13, HIGH);
   }
  else {
     digitalWrite(13, LOW);
   } 
  
  //if The current distance value has changed we print it on serial
  if (Distance != prevDistance){
    Serial.print("Sensor ");
    Serial.print(REQ_ADDRESS);
    Serial.print(" distance--> ");
    Serial.println(Distance);
    prevDistance= Distance;
  }
}


When I open the serial monitor I get values: Sometimes for *a second; sometimes for 30, sometimes I don't get them. * * I tried also to constrain the distance value on the attiny but it still get stuck.


Thinking it could be an I2C problem; I wrote an I2C stress test sketch to be run on the attiny

Code:
#include <TinyWireS.h>

#define ledPin 4            // Led is connected to PB4
#define I2CSlaveAddress 1   // I2C Address.

int arguga =0;

void setup()
{
  TinyWireS.begin(I2CSlaveAddress);   // Begin I2C Communication
  TinyWireS.onRequest(transmit);      // When requested, call function transmit()
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  arguga ++;                             //increment the variable
  if (arguga > 254) arguga=0;      //reset the variable if it reaches the 8 bit limit
  digitalWrite(ledPin, HIGH);   
}  //end loop()

void transmit()
{
  TinyWireS.send(arguga);                 // Send the variable if requested
}

But this sketch kept running four hours without getting stuck and I've been reading values on the serial monitor for almost 40 minutes without any problem.

I tried also using an Arduino uno instead of teensy and using an HC-SR04 instead of the HY. * Same problem.

How do I fix This ?

NOTE that if I hookup the sensor directly to: teensy 3.1, arduino, arduino mega etc... Everything works fine
 
Last edited:
It looks like something between your source (where you copied from) and your destination (the code you pasted above) has changed all your tabs into asterisks - whatever did that has decreased your chances of getting (even semi-reasonable, let alone) decent help with this imho.

Just trying to parse your code with my tired eyes, doing my best to ignore all them asterisks, I don't spot the problem yet - annoyingly enough the only thing I feel qualified to comment on in your code so far is that in the last block of code; if you declare arguga as unsigned char or uint8_t then you won't need the line "if (arguga>254) arguga=0;" because the natural thing for an 8 bit variable to do when it is =255 and you ++ it is to wrap back to =0

If you can repost sans asterisks so it can be compiled after just being copied and pasted into a suitably primed IDE I will at least re-read it as carefully as I can and apologise if I become sure I cannot really help :)
 
I may have spotted it;
Code:
#include <Wire.h>

#define THRESHOLD 10  // Minimum distance to trigger the led (in cm)
#define REQ_ADDRESS 1 // Address to ping for information

byte Distance;        // Unsigned 8 bit variable to store the distance value
byte prevDistance =0;    // Store the previous distance value

void setup(){
  Wire.begin();
  Serial.begin(57600);
  while (!Serial) {
  }
  Serial.println("Setup");
  pinMode(13, OUTPUT);
}

void loop(){ 
  Wire.requestFrom(REQ_ADDRESS, 1);  // The TinyWire library only allows for one byte to be requested at a time
  [B]/* if (Wire.available() == 1)  ; */[/B]  // Wait until there is data in the I2C buffer
  while(!Wire.available()); // no, srsly, wait! - possibly better constructed with a time-out but nvm for now.
  Distance = Wire.read();                  // Read the first (and hopefully only) byte in the I2C buffer
  if ((Distance > 0) && (Distance < THRESHOLD)){
     digitalWrite(13, HIGH);
   }
  else {
     digitalWrite(13, LOW);
   } 
  
  //if The current distance value has changed we print it on serial
  if (Distance != prevDistance){
    Serial.print("Sensor ");
    Serial.print(REQ_ADDRESS);
    Serial.print(" distance--> ");
    Serial.println(Distance);
    prevDistance= Distance;
  }
}
Please let me know if I have hit the target there :)
 
Nope it didn't but is a great sketch improvement :)

If you look at the last sketch of the first post I ran an I2c stress test by cutting out the new ping library and replacing the distance variable with a fake 8 bit one that continuosly increment till 254, on the attiny.

It worked for hours even with multiple attiny and 5 meters I2C Line lenght.

The problem occurs only when I try to read the ultrasonic sensor
 
UPDATE 2

Ok I think I'm on something.


So...

I finally placed an Led on PB4 and ran other tests.

Its interesting

If I run the I2C stress test only (1st post) It keeps running and printing properly on the serial monitor for hours.

Instead if I load the sketch using the New ping Library

Code:
#include <TinyWireS.h>              // Requires fork by Rambo with onRequest support
#include <NewPing.h>            // NewPing library modified for ATtiny
/*
 * Divarobot 1.1 SensorBoard firmware
 * Processor: Atmel Attiny 85  with 8mhz Internal clock
 * Functions:   -read value from an HY-SRF05 ultrasonic range finder
 *              -blink an led as the object gets closer
 *              -send the distance over I2C if Requested
 *              
 * NOTE: Assign to every Tiny a different address based on the one wrote on the board.
 *
 *IDEATED, PROGRAMMED and TESTED by Alessandro Zippilli -Zipporobotics on 13th march 2015.  
 *Copyright Zipporobotics All rights Reserved
 */

#define SensorPin 3         // Sensor  is connected to PB3
#define ledPin 4            // Led is connected to PB4
#define I2CSlaveAddress 1   // I2C Address.

unsigned int Distance;                   // Where the Distance is stored 
unsigned long previousScan;     // Here we store the last time we checked the cycle

#define MAX_DISTANCE 180 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
#define POLL_TIME 33     // How long do we wait before polling the sensor again in milliseconds
NewPing sonar (SensorPin, SensorPin, MAX_DISTANCE);       // Define the Sensor
 
void setup()
{ 
  delay(I2CSlaveAddress + 2);         // Join the I2C protocol with a slight Delay to avoid latch up
  TinyWireS.begin(I2CSlaveAddress);   // Begin I2C Communication
  TinyWireS.onRequest(transmit);      // When requested, call function transmit()
  previousScan = millis();            // Initialize millis
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  if (millis() - previousScan > POLL_TIME){ //if we are over the last time we checked the sensor
    Distance = sonar.ping_cm();         //We  Get distance from it in cm. 
    if (Distance > 180) Distance = 180;       //Lets limit the distance to avoid latch up
    previousScan = millis();
    if (Distance > 0 && Distance < 10){
      digitalWrite(ledPin, HIGH);
     }
    else {
      digitalWrite(ledPin, LOW);
     }
  } 
}  //end loop()

void transmit()
{
  TinyWireS.send(Distance);                 // Send last recorded distance for current sensor
}


The values keep printing for some seconds (sometimes 10 sometimes 50) then the I2C freezes but the attiny still run the program and the led light on and off if I get closer or further with the object in front of the sensor.


Is there maybe an interrupt latching the line ?
 
Although I've some experience with AtTiny chips it was never through arduino, any chance you could link to that newping library that has been modified for AtTiny? (Soz if you already did, can't find it.)
 
UPDATE 3: On Request is getting stuck

In the middle of the frustration caused by this problem: I tried changing the point where the led turns.

On the Attiny side I changed it to: the on request function.

Basically it will turn on everytime the byte is requested

See the sketch

Code:
#include <TinyWireS.h>              // Requires fork by Rambo with onRequest support
#include <NewPing.h>            // NewPing library modified for ATtiny
/*
 * Divarobot 1.1 SensorBoard firmware
 * Processor: Atmel Attiny 85  with 8mhz Internal clock
 * Functions:   -read value from an HY-SRF05 ultrasonic range finder
 *              -blink an led as the object gets closer
 *              -send the distance over I2C if Requested
 *              
 * NOTE: Assign to every Tiny a different address based on the one wrote on the board.
 *
 *IDEATED, PROGRAMMED and TESTED by Alessandro Zippilli -Zipporobotics on 13th march 2015.  
 *Copyright Zipporobotics All rights Reserved
 */

#define SensorPin 3         // Sensor  is connected to PB3
#define ledPin 4            // Led is connected to PB4
#define I2CSlaveAddress 1   // I2C Address.

unsigned int Distance;                   // Where the Distance is stored 
unsigned long previousScan;     // Here we store the last time we checked the cycle

#define MAX_DISTANCE 180 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
#define POLL_TIME 30     // How long do we wait before polling the sensor again in milliseconds
NewPing sonar (SensorPin, SensorPin, MAX_DISTANCE);       // Define the Sensor
 
void setup()
{ 
  delay(I2CSlaveAddress + 2);         // Join the I2C protocol with a slight Delay to avoid latch up
  TinyWireS.begin(I2CSlaveAddress);   // Begin I2C Communication
  TinyWireS.onRequest(transmit);      // When requested, call function transmit()
  previousScan = millis();            // Initialize millis
  pinMode(ledPin, OUTPUT);
}

void loop()
{ 
  digitalWrite(ledPin, LOW);
  if (millis() - previousScan > POLL_TIME){ //if we are over the last time we checked the sensor
    Distance = sonar.ping_cm();         //We  Get distance from it in cm. 
    if (Distance > 180) Distance = 180;       //Lets limit the distance to avoid latch up
    previousScan = millis();
    if (Distance > 0 && Distance < 10){
      //digitalWrite(ledPin, HIGH);
     }
    else {
      //digitalWrite(ledPin, LOW);
     }
  } 
}  //end loop()

void transmit()
{
  TinyWireS.send(Distance);                 // Send last recorded distance for current sensor
  digitalWrite(ledPin, HIGH);

}

Quite surprisingly with this code: I got data coming on the serial monitor for about 2 minutes but then it got stuck again.

BUT ! The led on the attiny turned of when i stopped getting data and this means that the sketch into the tiny was still running and the function:

Code:
void transmit()
{
  TinyWireS.send(Distance);                 // Send last recorded distance for current sensor
  digitalWrite(ledPin, HIGH);

}

was simply not being called anymore.

Tried powercycling only the attiny but didn't work.
Tried powercycling only the teensy 3.1 but didn't work
Tried powercycling both at the same time and it started to work back for another 2 minutes.

Then it stuck again.


Thinking the problem is not on the attiny; I went to investigate on the teensy 3.1 and found out that is randomly hanging at this line:

Code:
  Wire.requestFrom(REQ_ADDRESS, 1);  // The TinyWire library only allows for one byte to be requested at a time


I think we are closer
 
Teensy hanging on requestfrom indicates the other end to me (ie, AtTiny code) but I can easily be wrong, that has happened before ;)

I had quick scan of newping code (am at work and am assigned away from PC for today) and didn't spot an obvious flaw in the few seconds I could afford atm, about 8 hours till I can look closer :(
 
UPDATE 4 : Rob might be Right

Teensy hanging on requestfrom indicates the other end to me (ie, AtTiny code) but I can easily be wrong, that has happened before ;)

I had quick scan of newping code (am at work and am assigned away from PC for today) and didn't spot an obvious flaw in the few seconds I could afford atm, about 8 hours till I can look closer :(

you may be right actually.

I've done some further test

UPDATE 4

I got an arduino instead of the teensy and loaded this sketch on it

Code:
#include <Wire.h>
/*
 * Divarobot 1.1 Head Controller I2C Single sensor ping
 * Processor:   K20 Teensy 3.1 at 72 mhz Optimized clock
 * Functions:   -request the one byte distance value from one of the attiny on the sensorboard
 *              -compare the distance value with a set Threshold
 *              -print the distance value on the serial port
 *              -light up an led if the value is lower than Threshold
 * I2C Addresses on the Board: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
 *              
 *IDEATED, PROGRAMMED and TESTED by Alessandro Zippilli -Zipporobotics on 13th march 2015.  
 *Copyright Zipporobotics All rights Reserved
 */

#define THRESHOLD 10  // Minimum distance to trigger the led (in cm)
#define REQ_ADDRESS 1 // Address to ping for information

byte Distance;        // Unsigned 8 bit variable to store the distance value
byte prevDistance =0;    // Store the previous distance value

void setup(){
  Wire.begin();
  Serial.begin(57600);
  while (!Serial) {
  }
  Serial.println("Setup");
  pinMode(13, OUTPUT);
}

void loop(){ 
  Wire.requestFrom(REQ_ADDRESS, 1);  // The TinyWire library only allows for one byte to be requested at a time
  while(!Wire.available());
  Distance = Wire.read();                  // Read the first (and hopefully only) byte in the I2C buffer
  if ((Distance > 0) && (Distance < THRESHOLD)){
     digitalWrite(13, HIGH);
   }
  else {
     digitalWrite(13, LOW);
   } 
  
  //if The current distance value has changed we print it on serial
  if (Distance != prevDistance){
    Serial.print("Sensor ");
    Serial.print(REQ_ADDRESS);
    Serial.print(" distance--> ");
    Serial.println(Distance);
    prevDistance= Distance;
  }
}

And the same thing happene again. The uno got stuck at this line

Code:
 Wire.requestFrom(REQ_ADDRESS, 1);  // The TinyWire library only allows for one byte to be requested at a time

BUT ! Pressing the reset button got it working again for few seconds this time.



I tried then to load this sketch on the attiny

Code:
#include <TinyWireS.h>
/*
 * Divarobot 1.1 SensorBoard stress test
 * Processor: Atmel Attiny 85  with 8mhz Internal clock
 * Functions:   -Declare and increment a numerical variable
 *              -blink an led everytime the variable is requested
 *              -send the variable value to the host
 *              
 * NOTE: Assign to every Tiny a different address based on the one wrote on the board.
 *
 *IDEATED, PROGRAMMED and TESTED by Alessandro Zippilli -Zipporobotics on 13th march 2015.  
 *Copyright Zipporobotics All rights Reserved
 */

#define ledPin 4            // Led is connected to PB4
#define I2CSlaveAddress 2   // I2C Address.

int arguga =0;

void setup()
{
  TinyWireS.begin(I2CSlaveAddress);   // Begin I2C Communication
  TinyWireS.onRequest(transmit);      // When requested, call function transmit()
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  arguga ++;
  if (arguga > 254) arguga=0;
  digitalWrite(ledPin, HIGH);   
}  //end loop()

void transmit()
{
  TinyWireS.send(arguga);                 // Send last recorded distance for current sensor
}

and it worked like a charm for several minutes

So the problem only occur when i include the new ping library in the sketch and it seems to be on attiny side

Any guesses on what it could be ?
 
my lunch break :)

My basic suspicion atm is that newping is conflicting with tinywires by hogging a resource or trashing (as far as tinywires is concerned) something they share or perhaps don't even really so much share.

Please retry your project with following code executing in AtTiny, if the problem goes away we got lucky with a guess of mine - if the problem takes longer to manifest then maybe using the 16 bit form of the variable I have introduced will make it even better but readings may be a fair bit slower.
Code:
#include <TinyWireS.h>              // Requires fork by Rambo with onRequest support
#include <NewPing.h>            // NewPing library modified for ATtiny
/*
 * Divarobot 1.1 SensorBoard firmware
 * Processor: Atmel Attiny 85  with 8mhz Internal clock
 * Functions:   -read value from an HY-SRF05 ultrasonic range finder
 *              -blink an led as the object gets closer
 *              -send the distance over I2C if Requested
 *              
 * NOTE: Assign to every Tiny a different address based on the one wrote on the board.
 *
 *IDEATED, PROGRAMMED and TESTED by Alessandro Zippilli -Zipporobotics on 13th march 2015.  
 *Copyright Zipporobotics All rights Reserved
 */

#define SensorPin 3         // Sensor  is connected to PB3
#define ledPin 4            // Led is connected to PB4
#define I2CSlaveAddress 1   // I2C Address.

unsigned int Distance;                   // Where the Distance is stored 
unsigned long previousScan;     // Here we store the last time we checked the cycle

#define MAX_DISTANCE 180 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
#define POLL_TIME 30     // How long do we wait before polling the sensor again in milliseconds
NewPing sonar (SensorPin, SensorPin, MAX_DISTANCE);       // Define the Sensor
 
void setup()
{ 
  delay(I2CSlaveAddress + 2);         // Join the I2C protocol with a slight Delay to avoid latch up
  TinyWireS.begin(I2CSlaveAddress);   // Begin I2C Communication
  TinyWireS.onRequest(transmit);      // When requested, call function transmit()
  previousScan = millis();            // Initialize millis
  pinMode(ledPin, OUTPUT);
}

uint8_t slowYouDown=0;
// uint16_t slowYouDown=0; // this form will make it 256 times less resource hogging than it is using above form...

void loop()
{ 
  if(!(slowYouDown++)) {
    digitalWrite(ledPin, LOW);
    if (millis() - previousScan > POLL_TIME){ //if we are over the last time we checked the sensor
      Distance = sonar.ping_cm();         //We  Get distance from it in cm. 
      if (Distance > 180) Distance = 180;       //Lets limit the distance to avoid latch up
      previousScan = millis();
      if (Distance > 0 && Distance < 10){
        //digitalWrite(ledPin, HIGH);
       }
      else {
        //digitalWrite(ledPin, LOW);
       }
    }
  }
}  //end loop()

void transmit()
{
  TinyWireS.send(Distance);                 // Send last recorded distance for current sensor
  digitalWrite(ledPin, HIGH);

}
Sorry if I have typo'd or something in it. Also, may as well slow the loop down on the Teensy 3.1 as well - if this works as a fix.
 
on second thoughts; don't resort to 16 bit variable.

The loop in the Teensy could do with similar treatment as I have made in your loop for AtTiny and that treatment could probably be of greater benefit with a 1ms delay as an else to executing the original code of the loops - able to show in about hour and a half (worst case ~5 hours) if you don't get what I mean by this.

Contemplating while I ate I realised your Teensy code may be bashing the AtTiny for wire response while it has been bashing itself over the sensor where there is no real benefit from having the tasks performed thousands of times per second where humans cannot perceive change that quickly.
 
oh (facepalm), so embarrassing; I missed where you already implemented a matter of not polling the sensor in a 'bashing' way in your AtTiny code. Sorry, my last two posts aren't worth much at all.

Just reviewing all the code a little more carefully I will now declare your code for AtTiny may have been perfectly good all along and it may be the Teensy 3.1 code 'bashing' the AtTiny85 via I2C requests.

This is an at least slightly neater fix for the Teensy code (at least, if I have spotted the real culprit).
Code:
#include <Wire.h>

#define THRESHOLD 10  // Minimum distance to trigger the led (in cm)
#define REQ_ADDRESS 1 // Address to ping for information

#define WAITMS 50

byte Distance;        // Unsigned 8 bit variable to store the distance value
byte prevDistance =0;    // Store the previous distance value

void setup(){
  Wire.begin();
  Serial.begin(57600);
  while (!Serial) {
  }
  Serial.println("Setup");
  pinMode(13, OUTPUT);
}
elapsedMillis timing1;

void loop(){ 
  if (timing1>WAITMS) { // (1000/WAITMS) times a second will do, no?
    timing1=0; // clear it here so the time to execute following block isn't added to interval.
	Wire.requestFrom(REQ_ADDRESS, 1);  // The TinyWire library only allows for one byte to be requested at a time
    while (!Wire.available());
    Distance = Wire.read();                  // Read the first (and hopefully only) byte in the I2C buffer
    if ((Distance > 0) && (Distance < THRESHOLD)) {
       digitalWrite(13, HIGH);
    }
    else {
       digitalWrite(13, LOW);
    } 
  
    //if The current distance value has changed we print it on serial
    if (Distance != prevDistance){
      Serial.print("Sensor ");
      Serial.print(REQ_ADDRESS);
      Serial.print(" distance--> ");
      Serial.println(Distance);
      prevDistance= Distance;
    }
  }
}
This compiled for me, hopefully it meets my expectations when(/if) you test it.

You could make life more serene for the AtTiny85 (imho) if you implement similar to following in the code for it.
Code:
#include <TinyWireS.h>              // Requires fork by Rambo with onRequest support
#include <NewPing.h>            // NewPing library modified for ATtiny
/*
 * Divarobot 1.1 SensorBoard firmware
 * Processor: Atmel Attiny 85  with 8mhz Internal clock
 * Functions:   -read value from an HY-SRF05 ultrasonic range finder
 *              -blink an led as the object gets closer
 *              -send the distance over I2C if Requested
 *              
 * NOTE: Assign to every Tiny a different address based on the one wrote on the board.
 *
 *IDEATED, PROGRAMMED and TESTED by Alessandro Zippilli -Zipporobotics on 13th march 2015.  
 *Copyright Zipporobotics All rights Reserved
 */

#define SensorPin 3         // Sensor  is connected to PB3
#define ledPin 4            // Led is connected to PB4
#define I2CSlaveAddress 1   // I2C Address.

unsigned uint16_t Distance;                   // Where the Distance is stored 
unsigned long previousScan;     // Here we store the last time we checked the cycle

#define MAX_DISTANCE 180 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar (SensorPin, SensorPin, MAX_DISTANCE);       // Define the Sensor
 
void setup()
{ 
  /* not sure why you are delaying for a count of 3 here, 10 is a more common choice */
  delay(I2CSlaveAddress + 2);         // Join the I2C protocol with a slight Delay to avoid latch up
  TinyWireS.begin(I2CSlaveAddress);   // Begin I2C Communication
  TinyWireS.onRequest(transmit);      // When requested, call function transmit()
  previousScan = millis();            // Initialize millis
  pinMode(ledPin, OUTPUT);
}

unit8_t flags=1; // using this method we only bother doing our thing when they ask...

void loop()
{ 
  digitalWrite(ledPin, LOW);
  if (flags&1) { //if we are over the last time we checked the sensor
    Distance = sonar.ping_cm();         //We  Get distance from it in cm. 
    if (Distance > 180) Distance = 180;       //Lets limit the distance to avoid latch up
    previousScan = millis();
    if (Distance > 0 && Distance < 10){
      //digitalWrite(ledPin, HIGH);
    }
    else {
      //digitalWrite(ledPin, LOW);
    }
	flags&=254; // you can signal other events with the other bits if you want :)
  } 
}  //end loop()

void transmit()
{
  TinyWireS.send((uint8_t)Distance);                 // Send last recorded distance for current sensor
  digitalWrite(ledPin, HIGH);
  flags|=1; // tell the contents of the loop to fetch us a nice new reading
}
So, provided this compiles, the sensor reading rate can effectively be controlled by the Teensy 3.1 master, you can probably poll heaps quicker than 50ms using this methodology.
 
Last edited:
Robsoles GOT IT amazing!!!

Man YOU GOT IT !!!

YOU GOT IT ! :D

I tidied up the code a bit for attiny

But you got it !!


It worked for an hour and was still running when I stopped it.


Amazing!! I need to try it with multiple attinys but it seems to work
finally

After One year ! It seems to be solved !

Let me know how I can add you in the list of the greatest people that contributed to: Divarobot :D

Code:
#include <TinyWireS.h>              // Requires fork by Rambo with onRequest support
#include <NewPing.h>            // NewPing library modified for ATtiny
/*
 * Divarobot 1.1 SensorBoard firmware
 * Processor: Atmel Attiny 85  with 8mhz Internal clock
 * Functions:   -read value from an HY-SRF05 ultrasonic range finder
 *              -blink an led as the object gets closer
 *              -send the distance over I2C if Requested
 *              
 * NOTE: Assign to every Tiny a different address based on the one wrote on the board.
 *
 *IDEATED, PROGRAMMED and TESTED by Alessandro Zippilli -Zipporobotics on 13th march 2015.  
 *Copyright Zipporobotics All rights Reserved
 */

#define SensorPin 3         // Sensor  is connected to PB3
#define ledPin 4            // Led is connected to PB4
#define I2CSlaveAddress 1   // I2C Address.

boolean flags= true; // using this method we only bother doing our thing when they ask...
uint16_t Distance;                   // Where the Distance is stored 
unsigned long previousScan;     // Here we store the last time we checked the cycle

#define MAX_DISTANCE 180 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar (SensorPin, SensorPin, MAX_DISTANCE);       // Define the Sensor
 
void setup()
{ 
  /* not sure why you are delaying for a count of 3 here, 10 is a more common choice */
  delay(I2CSlaveAddress + 2);         // Join the I2C protocol with a slight Delay to avoid latch up
  TinyWireS.begin(I2CSlaveAddress);   // Begin I2C Communication
  TinyWireS.onRequest(transmit);      // When requested, call function transmit()
  previousScan = millis();            // Initialize millis
  pinMode(ledPin, OUTPUT);
}


void loop(){ 
  digitalWrite(ledPin, LOW);
  if (flags == true) { //if we are over the last time we checked the sensor
    Distance = sonar.ping_cm();         //We  Get distance from it in cm. 
    previousScan = millis();
    flags= false; // you can signal other events with the other bits if you want :)
  }
}  //end loop()

void transmit(){
  TinyWireS.send((uint8_t)Distance);                 // Send last recorded distance for current sensor
  digitalWrite(ledPin, HIGH);
  flags = true; // tell the contents of the loop to fetch us a nice new reading
}

Thank you :)
 
You are welcome, I can see fair possibility I'd contribute more, if you are interested.

I do not require attribution but if you want to mention me you could add 'with contrib by robsoles' or something in your notice at the top.

If using my screen name (which I use everywhere) seems somehow that wrong then my real name (much less cool than my screen name imho) is Robert Thompson.
 
Status
Not open for further replies.
Back
Top