Hello,
I'm trying to create a simple circuit with piezo sensors, using thresholding to detect when a "knock" is generated (very close to the "Knock" example code from Arduino). This is working fine on its own, but as soon as I try to incorporate Ethernet into the code (the goal is to be able to transmit when a knock is registered), the code refuses to loop.
Specifically, after the "Ethernet begins" print I get no more information via the serial monitor, nor do I get any UDP packets sent as expected. I've narrowed the problem down to the Udp.endPacket() line. The program gets stuck right there and refuses to continue:
I have my computer and the Teensy hooked up to a small switch and have given them the static IPs and such. I've double-checked my wiring with the ethernet adapter and can't find any glaring issues. I've done this before with buttons and the like and have had success so I'm not sure what's so different about this time unless I made a silly error that is eluding me.
Any ideas?
Thanks in advance for the help.
I'm trying to create a simple circuit with piezo sensors, using thresholding to detect when a "knock" is generated (very close to the "Knock" example code from Arduino). This is working fine on its own, but as soon as I try to incorporate Ethernet into the code (the goal is to be able to transmit when a knock is registered), the code refuses to loop.
Specifically, after the "Ethernet begins" print I get no more information via the serial monitor, nor do I get any UDP packets sent as expected. I've narrowed the problem down to the Udp.endPacket() line. The program gets stuck right there and refuses to continue:
Code:
// these constants won't change:
const int knockSensor = A8; // the piezo is connected to analog pin 8
const int potentiometer = A9;
int threshold = 100; // threshold value to decide when the detected sound is a knock or not
// these variables will change:
int sensorReading = 0; // variable to store the value read from the sensor pin
int highestReading = 0;
int lowestReading = 1000;
int difference = 0;
int potReading = 0;
elapsedMillis timeSinceLastInterval;
// ethernet
#include <Ethernet.h>
#include <EthernetUdp.h>
byte myMac[] = {0x98, 0x76, 0xB6, 0x10, 0x61, 0x0A};
IPAddress myIP(192, 168, 1, 81);
IPAddress myDNS(192, 168, 1, 1);
IPAddress myGW(192, 168, 1, 1);
IPAddress myMask(255, 255, 255, 0);
unsigned int localPort = 8888; // local port to listen on
IPAddress destIP(192, 168, 1, 103);
EthernetUDP Udp;
void setup() {
Serial.begin(9600); // use the serial port
//ethernet
Ethernet.begin(myMac, myIP, myDNS, myGW, myMask);
Udp.begin(localPort);
Serial.print("Ethernet begins");
}
void loop() {
// read the sensor and store it in the variable sensorReading:
sensorReading = analogRead(knockSensor);
if(sensorReading > highestReading){
highestReading = sensorReading;
}
if(sensorReading < lowestReading){
lowestReading = sensorReading;
}
potReading = analogRead(potentiometer);
threshold = potReading/8;
if(timeSinceLastInterval >= 50){
difference = highestReading-lowestReading;
Serial.println(difference);
if(difference > threshold){
Serial.println("KNOCK!");
}
highestReading = 0;
lowestReading = 1000;
timeSinceLastInterval = 0;
}
Udp.beginPacket(destIP, 7778); // used by comp
Udp.print("testing");
Udp.endPacket();
}
I have my computer and the Teensy hooked up to a small switch and have given them the static IPs and such. I've double-checked my wiring with the ethernet adapter and can't find any glaring issues. I've done this before with buttons and the like and have had success so I'm not sure what's so different about this time unless I made a silly error that is eluding me.
Any ideas?
Thanks in advance for the help.