I have an Arduino Mega 2560. I have an LED grounded and connected to pin 12 by breadboard and a switch connected to pin 7 and pin 2 by breadboard. I have written this program so that a button press changes the LED's state between on and off. The components all seem to be working so I believe it is a coding issue. Here's my code:
boolean running = false;
boolean ledon = true;
void statechange() {
if(running == false) {
running = true;
ledon = !ledon;
if(led on) {
digitalWrite(12, HIGH);
} else {
digitalWrite(12, LOW);
}
delay(1000);
running = false;
}
}
void setup() {
pinMode(12, OUTPUT);
pinMode(7, OUTPUT);
digitalWrite(7, HIGH);
}
void loop() {
attachInterrupt(0,statechange,CHANGE);
}
I don't receive any errors, it just doesn't work, the LED stays off no matter what.
Are you sure this is right?
if(ledon)
{
digitalWrite(12, HIGH);
}
else
{
digitalWrite(12, LOW);
}
It looks like "If the LED is ON, turn it ON, otherwise, If the LED is OFF, turn it OFF."
Shouldn't it be:
if(ledon)
{
digitalWrite(12, LOW);
ledon = false;
}
else
{
digitalWrite(12, HIGH);
ledon = true;
}
If you're using ledon to keep track of state, but change the state independently of your if statement, the two could potentially become out of sync. Especially if code somewhere else can change the state of ledon.
The other thing that bothers me is this line: if(running == false)
If that is true (e.g. your hardware? is not running), then what is the point of trying to change I/O states at that time?
boolean ledon = true;
void statechange()
{
ledon = !ledon;
digitalWrite(12, ledon ? HIGH : LOW);
}
void setup()
{
pinMode(12, OUTPUT);
pinMode(7, OUTPUT);
digitalWrite(7, HIGH);
}
void loop()
{
attachInterrupt(0,statechange,CHANGE);
}
Related
I just started getting into Arduino and I put together a very simple led control with one button to change the on and off state. When I push the button the light will come on, but it will only stay on about 50% of the time (so I have to push it multiple times until it actually stays on), and The same thing happens when I try to turn it off.
Is there some problem with my code? Or is it likely to be a wiring issue?
//define variables
int ledPin = 5;
int btnOnPin = 9;
bool isItOn = false;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(btnOnPin, INPUT_PULLUP);
}
void loop() {
if(digitalRead(btnOnPin) == LOW)
{
if(isItOn == false)
{
digitalWrite(ledPin, HIGH);
isItOn = true;
}
else if (isItOn == true)
{
digitalWrite(ledPin, LOW);
isItOn = false;
}
}
}
The button is connected to ground and pin9, the led is connected to ground, and pin5 through a 220ohm resistor.
This should be super simple, but for some reason I can't get it to work properly.
Thanks for any help
try putting the arduiton to sleep for a few milli seconds after changing its val
void loop() {
if(digitalRead(btnOnPin) == LOW)
{
if(isItOn == false)
{
digitalWrite(ledPin, HIGH);
isItOn = true;
}
else if (isItOn == true)
{
digitalWrite(ledPin, LOW);
isItOn = false;
}
sleep(K);
}
I'm just getting started with my Arduino and I want to operate a relay for a certain amount of time once I press a contact switch, but still be able to stop the it again at any time by pressing the button again. I've got the press the button to start and stop working Ok, but can't get the timer bit to work at all. Below is what I've got working so far.
TIA.
void setup()
{
Serial.begin(9600);
pinMode(7, INPUT);
pinMode(6, INPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}
void loop()
{
Serial.print("Pin 7 ");
Serial.println(digitalRead(7));
if (digitalRead(6) == LOW && digitalRead(7) == HIGH) {
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
delay(250); // Wait for 250 millisecond(s)
}
if (digitalRead(6) == HIGH && digitalRead(7) == HIGH) {
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
delay(250); // Wait for 250 millisecond(s)
}
}
bool buttonState = 0, buttonStateBefore = 0;
bool toggle = 0;
unsigned long timeNow = 0;
buttonState = digitalRead(buttonPin);
if(buttonState > buttonStateBefore) {
toggle = !toogle; // negates the toogle
timeNow = millis(); // milliseconds to the time you press the button
}
buttonStateBefore = buttonState;
if(toggle && millis() < timeNow + delayTime) {
// relay on
}
else {
// relay off
}
Explanation:
When you press the button, buttonState beign now one, is bigger than the buttonStateBefore. So the if-statement is true. The variable toggle is now true and the time is set to the amount of milliseconds after starting the arduino. After the first if the buttonStateBefore is set to be the buttonState, to not toggle the toggle variable every circle. The second if-statement checks if the toggle variable is true and if the time has not exceeded. If one of the two things is not the case, the relay is turned off.
I hope this is helpful.
P.s. You can shorten your code if you use a for-loop for the digitalWrite
for(int i = 3; i < 7; i++) {
digitalWrite(i, true);
}
I write some very basic code to start on the relay and wait for 5 to 3 sec and then turn off it and exit the loop.
relay starting on but its not turning off and also relay taking the same time to start as I set the delay time (delay time = starting time) I don't know why.
void setup() {
pinMode(7, OUTPUT);
}
void loop() {
digitalWrite(7, HIGH);
delay(3000);
digitalWrite(7, LOW);
exit(0);
}
One possible reason is , loop running continuously with out exit. Which turns ON the relay immediately after OFF. Try code below
int count=0;
void setup()
{
pinMode(7, OUTPUT);
}
void loop()
{
if(count<1)
{
digitalWrite(7, HIGH);
delay(3000);
digitalWrite(7, LOW);
count++;
}
}
If you are facing the problem again, try swapping HIGH and LOW in above code,
it is active LOW for some boards.
Traffic light just stays on red rather than alternating.
Wanted it to stay on for 10s then off for 10s, continuing ad infinitum.
Dont want to use the delay function cos need to do other stuff while the LED continues to alternate.
Thanks
int red = 10; // red traffic light LED on pin 10
int redcounter;
// the setup routine runs once when you press reset:
void setup()
{
// initialize the digital pin as an output.
pinMode(red, OUTPUT);
digitalWrite(red, LOW);
redcounter = 0;
}
// the loop routine runs over and over again forever:
void loop()
{
redcounter = redcounter +1;
if(redcounter==1000)
{
redcounter=0;
if(digitalRead(red)==HIGH)
{
digitalWrite(red, LOW);
}
if(digitalRead(red)==LOW)
{
digitalWrite(red, HIGH);
}
}
You try to read a port which is configured as an OUTPUT. I don't know if this is supposed to work, but it would be more clear if you simply use another port as INPUT and feedback the signal you want to check in that port. I'm not sure however if it makes much sense to check the state of a signal you generate yourself (?). Moreover your redcounter is just "Active waiting", and arduino provides a delay function which does exactly that.
int red=10;
int signal=11;
void setup()
{
pinMode(red, OUTPUT);
pinMode(signal, INPUT);
digitalWrite(red, LOW);
}
void loop()
{
delay(1000);
if(digitalRead(signal)==HIGH)
{
digitalWrite(red, LOW);
}
if(digitalRead(signal)==LOW)
{
digitalWrite(red, HIGH);
}
}
Use elseif instead of if here:
if(digitalRead(red)==HIGH)
{
digitalWrite(red, LOW);
}
else if(digitalRead(red)==LOW)
{
digitalWrite(red, HIGH);
}
In your old solution every time red turned low, it was turned high a moment later.
Two issues in your code are that digitalread will not read an output pin and if you use an increment counter you won't be able to accurately denote time. Sorry if I missed a bracket or something I was doing this on the mobile app.
Use this:
int red = 10; // red traffic light LED on pin 10
int redcounter;
boolean pinState = false;
int delayTime = 10000;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(red, OUTPUT);
digitalWrite(red, LOW);
redcounter = millis();
}
// the loop routine runs over and over again forever:
void loop() {
if((millis() - red counter) > delayTime) {
redcounter=millis();
if(pinState) {
digitalWrite(red, LOW);
pinState = false;
}
else {
digitalWrite(red, HIGH);
pinState = true;
}
}
}
My Code:
*int led = 13;
void setup(){
Serial.begin(9600);
pinMode(led, OUTPUT);
while (!Serial) {
//My code get stack here!
//it stay here looping on endlessly!
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
}
void loop() {
}*
So this is the problem. the simple program waiting for Serial and the while loop continue forever.
how to fix this. is it a known problem?
int led = 13;
void setup(){
Serial.begin(9600);
pinMode(led, OUTPUT);
while (!Serial1) {
//My code get stack here!
//it stay here looping on endlessly!
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
}
}
void loop() {
}
I think that is a missing bracket closing setup()
Your loop condition is wrong. If you want to wait for something to appear on the serial port then you need to change it to this:
while (Serial.available() == 0) {
Your code is fine, it looks like your serial port is not connecting. If I modify your code to watch a counter up to 5, and when the counter is 5 then myconnection is true, then !myconnection is false and the loop stops (watch it in Tools>Serial Monitor):
int led = 13;
int counter = 0;
boolean myconnection = false;
void setup(){
Serial.begin(9600);
pinMode(led, OUTPUT);
while (!myconnection) {
//My code get stack here!
//it stay here looping on endlessly!
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
//here's my debug stuff
counter++;
if (counter ==5 ){
myconnection = true;
}
Serial.println("counter: " + String(counter) + ", myconnection: " + String(myconnection));
}
}
void loop() {
}