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
And I'm Running the following sketch on teensy 3.1 to check the Distance value
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
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
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: