Pages: [1] 2   Go Down
Author Topic: Unwanted shut down of outputs.  (Read 2916 times)
0 Members and 1 Guest are viewing this topic.
Mauritius
Offline Offline
Jr. Member
**
Karma: 2
Posts: 95
Learning Never Ends . . .
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,

Im actually writing some tutorial codes with my arduino UNO. . they were good at start now when i put some own modifications, it seems not working. Im quite afraid it is coming from hardware.

A simple sketch, is i pressed the pushbutton to light all my 5 leds, and i pressed the same to put them to low.

However, when the leds are on, they goes out without me pressing the button again after a while, as if an off delay is present.

I tried to figure out what is happening, and i wrote some codes to monitor within the serial monitor, when the serial monitor is opened, i can see all my variables being read, and my leds i.e. output work normally, no shut off delay.

As soon as i close my serial monitor, same thing happen leds goes out within a delay. quite weird. smiley

I tried to change my sketch with something else, a blinky state this time, same system, ON state with button and same push button to OFF mode. Again same thing happening, as if an off delay is present though it is not in my code.

Can someone please advise, Thanks . . .

The code below:
Quote
/*
 *  Blink ON & OFF with same switch . . . 
 */

 int switchPin = 2;            // switch is connected to pin 2
 
 int LED1 = 12;                // LED1 connected to pin 12
 int LED2 = 11;                // LED2 connected to pin 11
 int LED3 = 10;                // LED3 connected to pin 10
 int LED4 = 9;                 // LED4 connected to pin 9
 int LED5 = 8;                 // LED5 connected to pin 8
 
 int buttonState;              // variable to hold the last button state
 int val;                      // Variable to compare with buttonState
 int val2;                     // Variable to read the debounced status
 int lightMode = 0;            // Variable to set LEDs ON / OFF depending on mode.

void setup() {
 
   pinMode(switchPin, INPUT);   // Set the switch pin as input
 
   pinMode(LED1, OUTPUT);       // Set LED1 as ouput
   pinMode(LED2, OUTPUT);       // Set LED2 as ouput
   pinMode(LED3, OUTPUT);       // Set LED3 as ouput
   pinMode(LED4, OUTPUT);       // Set LED4 as ouput
   pinMode(LED5, OUTPUT);       // Set LED5 as ouput

Serial.begin(9600);                       // Set up serial communication at 9600bps
  buttonState = digitalRead(switchPin);  // read the initial state
  
}

void loop(){
  val = digitalRead(switchPin);      // read input value and store it in val
Serial.print("val=");
Serial.println (val);
delay(10);
 
 val2 = digitalRead(switchPin);      // Read input again to check for bounces
Serial.print("val2 =");
Serial.println (val2);
 if (val == val2){
  if (val != buttonState) {          // the button state has changed!
    if (val == LOW) {                // check if the button is pressed
    if (lightMode == 0){             // Check status of lightMode
  lightMode = 1;                     // Sets lightMode to high, LEDs ON
      digitalWrite(LED1,HIGH);
      digitalWrite(LED2,HIGH);
      digitalWrite(LED3,HIGH);
      digitalWrite(LED4,HIGH);
      digitalWrite(LED5,HIGH);
     }
   else{
   lightMode = 0;                    // Sets lightMode to Low, LEDs OFF
      digitalWrite(LED1,LOW);
      digitalWrite(LED2,LOW);
      digitalWrite(LED3,LOW);
      digitalWrite(LED4,LOW);
      digitalWrite(LED5,LOW);
    }
   }
  }
 
  buttonState = val;  // save the new state in our variable
 Serial.print("buttonState =");
Serial.println (buttonState);
 }
}


Looking forward ..
Thanks
Taz ...
Logged


tz
.....

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50115
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You haven't explicitly set the switch pin to INPUT.
You haven't enabled the pullup resistor on the switch pin.

You haven't told us how the switch is wired.

Code:
  buttonState = val;  // save the new state in our variable
Matching names, like currState and prevState, make a lot more sense, and really appear to be logically related, than buttonState and val.

Logged

Mauritius
Offline Offline
Jr. Member
**
Karma: 2
Posts: 95
Learning Never Ends . . .
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

{Hello Paul : u remember we had a small discussion on a doing a garduino, and having to do a scada along with the Arduino, to be able to change different customise settings. So i started . . smiley-draw}

- I did set it to Input:  pinMode(switchPin, INPUT);   // Set the switch pin as input

 Doing it explicitly . . how ??

- I did connect the switchButton, using the pull up resistor connection as below:

 "see attached file"

- Is there anything i need to enable on software side ?

It will certainly look good and make sense if i match the names of variables with these.

-------------
taz



* Pull up resistor.png (8.67 KB, 819x460 - viewed 26 times.)
Logged


tz
.....

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50115
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
- I did set it to Input:  pinMode(switchPin, INPUT);   // Set the switch pin as input
I missed that. Sorry.

Quote
- I did connect the switchButton, using the pull up resistor connection as below:
That looks fine. It uses two external resistors when none is really required, but it works.

Quote
- Is there anything i need to enable on software side ?
Not since you have external hardware.

I was suspecting a floating pin condition, but that does not appear to be your problem.

Perhaps seeing the serial output would be useful.
Logged

Mauritius
Offline Offline
Jr. Member
**
Karma: 2
Posts: 95
Learning Never Ends . . .
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset




Quote
That looks fine. It uses two external resistors when none is really required, but it works.

Can you kindly explain that part, though these external resistors are not required.
I taken a look on the schematic for pin 0 and 1 and noted a 1k ohm connected.

Quote
I was suspecting a floating pin condition, but that does not appear to be your problem.

I read some few things while using an IO pin as input and using serial communication that creates some trouble . . ???

Furthermore, my Arduino was powered through my usb, i just transfered to an external power supply (9 Vdc) and get my code running as perfect, and forever . . .

Any explanation on that ?
 


Logged


tz
.....

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 74
Posts: 7243
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you have resistors in series with your LEDs?
Logged


0
Online Online
Shannon Member
****
Karma: 214
Posts: 12420
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The code does debouncing with a delay of 10ms, but then uses the button state to set the LEDs directly, so they will only be on with the button is pressed (and upto 10ms afterwards due to the debounce delay).

You want the button being active to toggle the state, something like
Code:
  if (val != buttonState)
  {
    buttonState = val ;
    if (!val)  // button was pressed
      ledState = !ledState ; // toggle
    digitalWrite(LED1, ledState);
    digitalWrite(LED2, ledState);
    digitalWrite(LED3, ledState);
    digitalWrite(LED4, ledState);
    digitalWrite(LED5, ledState);
  }


Using an array of pin numbers will make the code less voluminous!
Logged

[ I won't respond to messages, use the forum please ]

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The code looks correct, although I had to correct that horrible indentation before I could convince myself about that. Isn't this clearer?
Code:
void loop()
{
val = digitalRead(switchPin);      // read input value and store it in val
Serial.print("val=");
Serial.println (val);
delay(10);

val2 = digitalRead(switchPin);      // Read input again to check for bounces
Serial.print("val2 =");
Serial.println (val2);
if (val == val2)
{
if (val != buttonState)
{
// the button state has changed!
if (val == LOW)
{               
// check if the button is pressed
if (lightMode == 0)
{             
// Check status of lightMode
lightMode = 1;                     // Sets lightMode to high, LEDs ON
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,HIGH);
digitalWrite(LED4,HIGH);
digitalWrite(LED5,HIGH);
}
else
{
lightMode = 0;                    // Sets lightMode to Low, LEDs OFF
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
digitalWrite(LED4,LOW);
digitalWrite(LED5,LOW);
}
}
}

buttonState = val;  // save the new state in our variable
Serial.print("buttonState =");
Serial.println (buttonState);
}
}

I suggest you remove the Serial.print statements at the top of loop, and put one inside the block of code where "the button state has changed!" so you can see whether you're getting spurious switch inputs. Maybe the switch wiring has the odd glitch. Also put a print statement in setup(), or make it go through an obvious LED flashing sequence, so that it's obvious if the board has reset.

Logged

I only provide help via the forum - please do not contact me for private consultancy.

Mauritius
Offline Offline
Jr. Member
**
Karma: 2
Posts: 95
Learning Never Ends . . .
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,

It seems i figured out what is happeninng exactly, i changed my sketch and observed the problem.

In fact my UNO board is being reset at every interval of approx. 3 mins, sometimes less, when connected through USB.

Please take note that i am using the MAKERSHIELD board on top.

Any idea about what really happening here.

Thanks

taz ...

Logged


tz
.....

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Does the reset only happen when the makershield is connected?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17301
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I read some few things while using an IO pin as input and using serial communication that creates some trouble . . ???

If you are talking about pin0 (rec comm) on the arduino, it is already pulled-up via the 1k resistor wired to the USB serial converter chip, so it will not see a 'floating' input condition.

Furthermore, my Arduino was powered through my usb, i just transfered to an external power supply (9 Vdc) and get my code running as perfect, and forever . . .

Any explanation on that ?
It could be (most likely) that your full setup of external hardware is just drawing too much +5 volt current. The USB 5 volt power coming from your PC is limited by the on-board thermofuse, rated at 500ma max. If however you are running using external power then the on-board 5 volt regulator is supplying the current and it has a little higher max current rating then when using USB power.

Lefty

Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 74
Posts: 7243
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you have resistors in series with your LEDs?

It is strange that you figured out the period of your problem (I wish my students were this good) and more but failed to answer such a simple question.
Logged


Mauritius
Offline Offline
Jr. Member
**
Karma: 2
Posts: 95
Learning Never Ends . . .
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,


Any explanation on that ?
It could be (most likely) that your full setup of external hardware is just drawing too much +5 volt current. The USB 5 volt power coming from your PC is limited by the on-board thermofuse, rated at 500ma max. If however you are running using external power then the on-board 5 volt regulator is supplying the current and it has a little higher max current rating then when using USB power.

Lefty

[/quote]

Thanks for the information.

However, i removed my maker shield board and tested my UNO R3 board again.

I applied a simple sketch of blinking 1 led only, here again my Board resets itself repeatedly after an interval of 1 min or less.

However when connected to an external power, and removed the USB connection, my board does not reset

????

Do you have resistors in series with your LEDs?

It is strange that you figured out the period of your problem (I wish my students were this good) and more but failed to answer such a simple question.
 

For now, i have only 1 LED connected, it comes from pin 13, set as output, goes through my LED from +ve pin to -ve via a 1K ohm resistor and eventually to the ground.

For my previous sketch, i was using 5 LEDS, each one connected seperately to their respective output pins, and each have their resistance through their LEDS.

Hope this answers the line, if my resistors were connected in series to my LEDs. . . Sorry for late reply. .  smiley-wink

Can someone kindly please help me, is there anything i can do to restore this problem of reseting through using the USB port?

I will be late in preparing my Garduino ..  smiley-cry , on top i need to submit that too.

Thanks

taz .. ..
Logged


tz
.....

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 495
Posts: 19036
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

However, i removed my maker shield board and tested my UNO R3 board again.

I applied a simple sketch of blinking 1 led only, here again my Board resets itself repeatedly after an interval of 1 min or less.

Just to clarify. You have a Uno board, no shield at all, connected to a single LED on pin 13, via a 1K resistor (to ground). Is that right? And running what sketch?

And it resets after 1 minute?

How do you know it resets?
Logged


Mauritius
Offline Offline
Jr. Member
**
Karma: 2
Posts: 95
Learning Never Ends . . .
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes an UNO R3 BOARD.. i removed my shield for trouble shooting purpose..

so as is now, my UNO board is without any shield at all and 1 LED is connected to pin 13, through a 1k resistor to ground..

Moreorless, it resets every 1 min or smetime less.

Please Note that i encountered this prob wen i was doing some other sketch.. see my first post.

For simplicity in troubleshooting, i change my circuit to a single led and watch my connected Led whch is blinks within a delay of 1000 ms..
Wen it resets you can see visually the blinking of my on board led connected to pin 13 n my led connected externally blinks just the same a wen as u press reset button.

If i try my code from my first post, my outputs goes out automatically and i need to press my switch again(input), clearly knowing that areset has been performed..


Taz ..
Logged


tz
.....

Pages: [1] 2   Go Up
Jump to: