My code recently started messing up a few days ago, and after 3 days of debugging I figured out it wasn't the code at all. It's some kind of weird interference. The board is a mega 2560. The code I am using to debug is below
/*
Button
Turns on and off a light emitting diode(LED) connected to digital
pin 13, when pressing a pushbutton attached to pin 2.
The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground
* Note: on most Arduinos there is already an LED on the board
attached to pin 13.
created 2005
by DojoDave <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Button
*/
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 25; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
Serial.println("HIGH");
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
Basically what is happening is the mega is saying the pin is high, but it shouldn't be high! If i move to put myself between the back door, and the mega (about two-three feet from the mega), the light cuts off. (acts like it should act). If I move a few inches it will turn on again. This has nothing to do with light. I did a few experiments with shinning a flash light on it while doing this, and it didn't change anything.
I know electronics are supposed to be insulated so they don't cause interference with other devices. I think this is what i am experiencing. I don't like the idea of taking my arduino somewhere and it possibly not work because of witchery signals from other devices.
Any suggestions? I can post a video of this happening on youtube if that would help.
Thomas
So what is attached to pin 25 besides the pushbutton connected from the pin to 5v? Is there also a 10K resistor connected from the pin to ground?
edit: I presume this is a magic pushbutton that detects light? Or is it an unmentioned device connected in an unmentioned way that you want someone to help you with? Let me look into my crystal ball....WAIT!! I see something...it's coming into focus.....AAAHHHHH!!!
Since I posted this, It started rain very hard, first time it's rained in a while, and I will note that while it was raining very hard, it was working perfectly like it's suppose to, now that the rain has slowed down it's starting to fluctuate.
Online data sheet for WTV-020-16P sound module Would a pull up or pulldown resistor effect the module though? Can you tell me which one I need based on the datasheet? I have the resistors that came with the ArduinoStarterKit are those pull up or pull down?
According to the data sheet Busy is high when playing, so..
If there is a 10K resistor in your starter kit (there should be 20 of them) then do Pin 25 > 10K > Ground, hook your incoming wire between Pin 25 and 10K.
So sort of like
Pin 25 -10K - Gnd
|
Signal from module
Wow I am terrible at drawing with characters....
This should keep pin 25 from "floating" high giving false triggers, but still allow an actual high signal to give a valid trigger.
According to the data sheet Busy is high when playing, so..
But the code I was testing it with, that I posted in the first post, shouldn't have played anything. Right?
Wow I am terrible at drawing with characters....
I tried to do that once. It was an epic failure! Don't feel bad.
If there is a 10K resistor in your starter kit (there should be 20 of them) then do Pin 25 > 10K > Ground, hook your incoming wire between Pin 25 and 10K.
I have a hot date tonight, I need to be leaving soon. I'll do this tomorrow morning and will post the results.
Thanks for the link! Is there a way to make it a logical zero, and not logical zero at the same time?
Looking at your code it should have just been doing nothing, therefore the LED should indeed have stayed off.
As for Zero/Not-Zero, AFAIK you are stuck with one state or the other, you could use an analog read to get the voltage level and make your own determination if it is high or low based off of experimentation, but likely the result would end the same as it is now.
If the module (or button) you are connected to is not "actively" low when not playing, meaning it just basically disconnects the pin, it doesn't turn it into a ground, then the input on the Arduino is going to float around a bit (and perhaps you are using a long wire that is picking up some noise from somewhere), the 10K should cure the issue if that is indeed the problem.
As an alternative experiment if the 10K does not solve the issue a bypass capacitor might be the key if it truly is induced noise, but someone much better at this than myself will probably need to help you out with sizing based off of what you have on hand (I use .1uf for everything) using some fancy math...
Good luck on your date, maybe run the 10K idea by her/him, if he/she throws in some useful advice then might be a keeper!
and perhaps you are using a long wire that is picking up some noise from somewhere
I was using the longest Solid core jumper wire that came in the ArduinoStarterKit The length of the wire is about 12 inches (estimated without measuring) - Are you saying the longer wires are more likely to act like an antenna?
the 10K should cure the issue if that is indeed the problem.
It did. Before I go any father do I need to start using 10k resistors for every connection that takes a reading to prevent this from happening again? If so, will that effect precision readings like the TEMP36 Temperature reading sensor readings?
Good luck on your date, maybe run the 10K idea by her/him, if he/she throws in some useful advice then might be a keeper!
It was a her. I'll consider your advice. If anyone on here has a in shape daughter let me know. I'm 27, white, in shape, located in SC America, but willing to move for more resources, and love arduino
For the record; I need a permanent fix to keep this from happening again. Allowing witchery signals to humiliate me in front of potential investors; ultimately effecting my destiny is unacceptable! Do they make any wires similar to size, with multiple colors, that are resistant to these sadistic interference signals?
Thomas499:
It did. Before I go any father do I need to start using 10k resistors for every connection that takes a reading to prevent this from happening again? If so, will that effect precision readings like the TEMP36 Temperature reading sensor readings?
I do not think you will need to use a 10K on every connection that takes a reading, the reason you needed it here is that when the button in not pressed the circuit is acting as if nothing is connected to the pin, if nothing is connected to the pin then it will have some noise (you can actually do a read on an unoccupied pin and output it to serial, you will see that it will float around some). So if say a sensor varies output voltage based on temperature there will always be "something" on the pin and I suspect that it will function correctly without the resistor, it is totally disconnected pins that seem to cause issues in my experience. Also size of the pull-up/pull-down resistor can be optimized based on the circuit, but math...someone else...
My logic says a pull-up or pull-down would most likely be required when one is trying to ascertain a digital state, the resistor basically makes sure there is a clean transition, no waffling in between.
And yes, the longer the wire (unless it is shielded, and properly grounded, and blah blah) the more susceptible it is going to be in regards to interference.
And yes, the longer the wire (unless it is shielded, and properly grounded, and blah blah) the more susceptible it is going to be in regards to interference.
I would prefer to never have this happen again. 3 days is a long time to spend debugging code when the problem isn't the code at all. Can you provide a link to shielded wire that would be ideal for arduino? preferably a combo pack that comes with different lengths and colors?
Wait.... are you saying the wire itself would need to be grounded?
The shield around the wire needs to be grounded, and only at one end.
Home Depot for example carries 4-strand 26 AWG with foil insulator, intended for wiring up security systems/cameras. I bought a 500 foot roll, you can buy a shorter length and cut off pieces the length you need.
Or you can find similar at Digikey.com, different AWG conductors and different number of conductors (I have used up to 15 in a DB15 connector).
Pullup or pulldown resistor will solve a lot of your problem tho as BH72 addressed already.
Using starter code, everything seemed to work fine using the 10k resistor fix. When main sketch was loaded, it didn't function properly. After about 8 hours of experiments, I think I figured out the problem, but request advice on the solution.
#include <Wtv020sd16p.h>
int resetPin = 28; // The pin number of the reset pin.
int clockPin = 27; // The pin number of the clock pin.
int dataPin = 26; // The pin number of the data pin.
int busyPin = 25; // The pin number of the busy pin.
/*
Create an instance of the Wtv020sd16p class.
1st parameter: Reset pin number.
2nd parameter: Clock pin number.
3rd parameter: Data pin number.
4th parameter: Busy pin number.
*/
Wtv020sd16p wtv020sd16p(resetPin,clockPin,dataPin,busyPin);
boolean DOonce=LOW;
void setup() {
//Initializes the module.
wtv020sd16p.reset();
Serial.begin(9600);
pinMode(busyPin,INPUT);
}
void loop() // only play once for debugging purposes
{ if (DOonce==LOW)
{ Once();
DOonce=HIGH;
}
}
void Once(){
if (digitalRead(busyPin)==LOW)
wtv020sd16p.asyncPlayVoice(1);
//delay(100);
if (digitalRead(busyPin)==HIGH)
Serial.print("HIGH");
//Plays audio file number 1 during 2 seconds.
}
The reason I wrote it so it checks to see if busyPin is low is so that I could play one file after the other, when first file was finished playing.
When this is loaded, The serial monitor says "HIGH" once, but the file isn't actually played (no sound). I do not understand why.
To make things more interesting, when I comment out the DOonce, so that it continuously loops, it appears to work(the sound is played). But I don't understand why.
void loop() // only play once for debugging purposes
//{ if (DOonce==LOW)
{ Once();
DOonce=HIGH;
}
//}
Could this have something to do with the 10k resistor fix?