Hello!
I'm not sure if that belongs here but I have a problem with my code execution.I'm a newbie in arduino and I may be in over my head but I would really like to finish it. For a bit of context: I am trying to make an RC car with arduino NANO and nRF24L01 moduls and had a little problem with libraries for them so I downloaded one from here>> https://tmrh20.github.io/RF24/ . Now I can't seem to use any buttons, switches and such, but antenas comunicate perfectly(when sending/reciving text). If I try to use buttons(look code below) it doesent do anything and it just randomly switches betwen 0 and 1. Even if there is nothing on input pin it will just work on it's own. On the other hand blinking light works just fine. I don't think it's arduinos fault because it is same thing with UNO.
To fix the problem I tried deleating and reinstalling library(also tried switching them but others were even worse) and switching protoboards. I didn't mess with arduinos because I fear that I'll destroy them. Code should be correct, at least compiler didn't detect any faults.
#include <SPI.h>
#define B 5
#define LED 7
int stanje = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(B, INPUT);
pinMode(LED, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
stanje = digitalRead(B);
Serial.print(stanje);
if(stanje == HIGH)
{
digitalWrite(LED, HIGH);
}
else
{
digitalWrite(LED, LOW);
}
}
Hope it's making sense because I can't translate well, also sorry for any misspells.
I will accept any help.
Thank you in advance!
randomly switches between 0 and 1
Sounds like a "floating" input, ie. not connected to either GND or VCC,
Connect the button to GND, and change pinMode(B, INPUT);
to pinMode(B, INPUT_PULLUP);
Erik_Baas:
randomly switches between 0 and 1
Sounds like a "floating" input, ie. not connected to either GND or VCC,
Connect the button to GND, and change pinMode(B, INPUT);
to pinMode(B, INPUT_PULLUP);
Hi,
so I tried changing from "INPUT" to "INPUT_PULLUP" and now it is not random anymore, but now if there is something even touching pin 5 LED will turn off.
I forgot to mention before that my button is getting power from 5V pin but I didn't think that could be the issue. Even if I change supply to 3V3 pin it still does not change a thing. It does however change if I remove resistor from button to GND it changes to constant 1. But button still does not effect anything. One mor thing upon closer inspection TX light is blinking really fast. Not shure if that means anything.
Thank you for reply.
With INPUT_PULLUP, you need to wire the button between pin and GND. You also need to be aware that the logic is reversed (so LOW indicates that the button is pressed).
And of course the tx light is flashing like crazy because you're printing all the time
sterretje:
With INPUT_PULLUP, you need to wire the button between pin and GND. You also need to be aware that the logic is reversed (so LOW indicates that the button is pressed).
And of course the tx light is flashing like crazy because you're printing all the time
Hi again,
so I have been playing with my circuit for a little bit and found out that if I switch resistor(connection from button to GND) from between button and pin to button and Vcc it works. Iam not shure why or if I'm just blind but it works.
Sorry to have been a bother.
Thank you for your time .
You don't need an additional resistor if you use INPUT_PULLUP.
Advise for future: show a schematic diagram, we did not know from your OP that you had a pull-down resistor, possibly incorrectly wired. Photo of paper/pencil drawing is fine.
Hi,
Can you please post a copy of both of your circuits, in CAD or a picture of a hand drawn circuit in jpg, png?
Please include power supplies.
Thanks.. Tom...