Go Down

Topic: Weird problem: digitalWrite(LED,LOW) but LED goes ON (Read 3544 times) previous topic - next topic

minitreintje

Hello,

If I use digtalWrite(LED,LOW), the LED does the other thing, it goes on?
There's only a resistor between the LED and the output.

Code: [Select]
#include <TimedAction.h>

// ArduSPI_slave

////////////////////////////////////////////////////////////////////
///  ONLY the following line needs to be changed for each slave !!!!
#define THIS_SLAVE 1
//// end of slave specifics
////////////////////////////////////////////////////////////////////

#define SLAVE_ID THIS_SLAVE
#define SELECT_PIN 2
#define ACK_PIN 3
#define LED_PIN 13

char data;
boolean ledAnalogON[] ={
  false,false,false,false,false,false,false};
boolean ledDigitalON[] ={
  false,false,false,false,false,false,false,false,false,false};
int LEDDigital[] = {
  4,5,6,7,8,9,10,11,12};
int LEDAnalog[] = {
  14,15,16,17,18,19};
int nbLEDDigital = 9;
int nbLEDAnalog = 6;
int nbLEDisON = 0; //LED's are at the beginning always OFF (see void setup()).
int dayWait = 1000;
int nightWait = 2000;
int transitionWait = 1500;
boolean day = false; //If these statement starts with 'true' then the slave will do stuff before there was any communication from the master.
boolean night = false;

TimedAction changeStateDayTimer = TimedAction(dayWait, changeStateDayActivator);
TimedAction changeStateNightTimer = TimedAction(nightWait, changeStateNightActivator);
TimedAction changeStateTransitionTimer = TimedAction(transitionWait, changeStateTransitionActivator);

void setup(){
  Serial.begin(9600);
  while(!Serial){
    ;
  }  // necessary on some Arduinos, may as well keep it.
  pinMode(SELECT_PIN, INPUT);  // we will read from the 'select' pin
  pinMode(ACK_PIN, OUTPUT);    // and write to the ACK pin
  digitalWrite(ACK_PIN, LOW);  // init it to NACK
  pinMode(LED_PIN, OUTPUT);    // set LED_PIN to writing
  digitalWrite(LED_PIN, HIGH);
  delay(100);
  digitalWrite(LED_PIN, LOW);  // and init to' off'

  for(int i; i < nbLEDDigital; i++)
  {
    pinMode(LEDDigital[i],OUTPUT);
    digitalWrite(LEDDigital[i],LOW);
  }

  for(int i; i < nbLEDAnalog; i++)
  {
    pinMode(LEDAnalog[i],OUTPUT);
    digitalWrite(LEDAnalog[i],LOW);
  }

}

void loop(){
  // all we do is get some data and do what it says
  // if there is data, do something, otherwise loop
  if(getData()){
    doSomethingWithData();
  }
  while(digitalRead(SELECT_PIN) == LOW && night == true) // repeat until there's serial available or night = false
  {
    changeStateDayTimer.check();
  }
  while(digitalRead(SELECT_PIN) == LOW && day == true) // repeat until there's serial available or day = false
  {
    changeStateNightTimer.check();
  }
  while(digitalRead(SELECT_PIN) == LOW && day == false && night == false) // repeat until there's serial available or day = false
  {
    changeStateTransitionTimer.check();
  }

  for(int i; i < nbLEDDigital; i++)
  {
    if(ledDigitalON[i] == true)
    {
      nbLEDisON++;
    }
    else
    {
      nbLEDisON--;
    }
  }

  for(int i; i < nbLEDAnalog; i++)
  {
    if(ledAnalogON[i] == true)
    {
      nbLEDisON++;
    }
    else
    {
      nbLEDisON--;
    }
  }
}

boolean getData(){
  // if not selected, ensure ACK_PIN is LOW and do nothing!
  if(digitalRead(SELECT_PIN)==LOW){
    digitalWrite(ACK_PIN,LOW);
    return false;
  }
  // if we got here, the the SELECT_PIN is HIGH,
  // so we need to read serial
  // first wait for something to be available to read
  while(!Serial.available()){
    ;
  }
  // then read 1 byte into the 'data' variable
  while(Serial.readBytes(&data,1) == 0) {
    ;
  }
  // ACK the serial read
  digitalWrite(ACK_PIN,HIGH);
  return true;
}

// just to demonstrate that the slave is doing something
// change the LED state at every reception of data.
void doSomethingWithData(){
  switch(data){
  case 'A': // Day
    digitalWrite(LED_PIN,HIGH);
    night = false;
    day = true;
    break;
  case 'B': // Night
    digitalWrite(LED_PIN,HIGH);
    day = false;
    night = true;
    break;
  case 'C': // Transition between case 'A' and case 'B'
    digitalWrite(LED_PIN,LOW);
    day = false;
    night = false;
  }
}

void changeStateDayActivator() //TimedAction can't have a number between '()' from a function, so I made a little conversionfunction :p
{
  changeState(12);
}

void changeStateNightActivator()
{
  changeState(5);
}

void changeStateTransitionActivator()
{
  changeState(8);
}



void changeState(int maxLEDON)
{
  int pinArrayIndexDigital = random(0,nbLEDDigital); // Choose randomly an output from the digital pins (D4 -> D12)
  ledDigitalON[pinArrayIndexDigital] = !ledDigitalON[pinArrayIndexDigital]; // change the state from the boolean variable

  int pinArrayIndexAnalog = random(0,nbLEDAnalog); // Choose randomly an output from the analog pins (A0 -> A5)
  ledAnalogON[pinArrayIndexAnalog] = !ledAnalogON[pinArrayIndexAnalog]; // change the state from the boolean variable

  // Digital pins
  if(ledDigitalON[pinArrayIndexDigital] & nbLEDisON < maxLEDON) // write the change to the correct pin
  {
    digitalWrite(LEDDigital[pinArrayIndexDigital],HIGH);
  }
  else
  {
    digitalWrite(LEDDigital[pinArrayIndexDigital],LOW);
  }

  // Analog pins
  if(ledAnalogON[pinArrayIndexAnalog] & nbLEDisON < maxLEDON) // write the change to the corect pin
  {
    digitalWrite(LEDAnalog[pinArrayIndexAnalog],HIGH);
  }
  else
  {
    digitalWrite(LEDAnalog[pinArrayIndexAnalog],LOW);
  }
}




Dylan

fungus


Hello,

If I use digtalWrite(LED,LOW), the LED does the other thing, it goes on?


How do you know you're doing "digtalWrite(LED,LOW)"? The code has "if" statements in it.
Advanced Arduino

minitreintje

If I start up... In setup.
I set all the pins low.


Dylan

fungus


If I start up... In setup.
I set all the pins low.


So the LEDs are on if you can delete everything in "loop()"?

Advanced Arduino

minitreintje

Yes, I had it also with an another sketch. If I add a transistor in the circuit (output pin - resistor - transistor - led) the problem is solved.
If I set the pin HIGH, the LED is OFF.


Dylan

minitreintje

Even with these ultra simple sketch (modified Blink):

Code: [Select]
/*
 Blink
 Turns on an LED on for one second, then off for one second, repeatedly.

 This example code is in the public domain.
*/

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 10;

// the setup routine runs once when you press reset:
void setup() {                
 // initialize the digital pin as an output.
 pinMode(led, OUTPUT);    
}

// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, LOW);

}


Dylan

fungus

Advanced Arduino

Tom Carpenter

Have you connected the LED anode to +5V and the cathode to the digital pin (through resistor)? That would explain why the LED would come on when you write low.
~Tom~

minitreintje

#8
Jul 16, 2013, 07:25 pm Last Edit: Jul 16, 2013, 07:27 pm by minitreintje Reason: 1
Yes I did. In Dutch we have this:

KNAP: Kathode Negatief Anode Positief

So I connect it like you said.
Why is that the problem? But it's not a big mistake. This was a test print. In the next definitive version I use transistor for switching more than 1 led on an output


Sorry for my bad English... And I designed the schematic wrong. The led have to be turned 180°

Dylan


fungus

It should go Arduino pin -> resistor -> LED -> GND.

Advanced Arduino

Grumpy_Mike

There is a forum rule that says that when ever a person says some behavior is weird it turns out to be perfectly normal.

Yes this will turn on the LED when the output pin is low. It is wired up in what is called a current sinking mode. If you want it the other way round wire it up for current sourcing. Connect the wire that goes to 5V instead to ground and reverse the LED, then a high will turn it on.

fungus


There is a forum rule that says that when ever a person says some behavior is weird it turns out to be perfectly normal.


I'll try to remember it next time.
Advanced Arduino

Tom Carpenter


Yes I did.


What a guess :D

When the pin is 'HIGH', then the voltage drop across the LED/resistor will be +5V minus a logic one (+5V), so there will be no voltage across the LED.
When the pin is 'LOW', then the voltage drop across the LED/resistor will be +5V minus a logic zero (0V), so there will be 5v across the LED.

In other words, the LED will be on only when the pin is LOW.
~Tom~

MAS3

Quote from: minitreintje

Yes I did. In Dutch we have this:

KNAP: Kathode Negatief Anode Positief

So I connect it like you said.
Why is that the problem? But it's not a big mistake. This was a test print. In the next definitive version I use transistor for switching more than 1 led on an output


Sorry for my bad English... And I designed the schematic wrong. The led have to be turned 180°


Knap is a Dutch word meaning pretty (as in beautiful) as well as smart, so it's an easy way to remember how to wire a diode.
As Grumpy_Mike already told you, simply wiring it different will make it work like you thought it would.
But most of the times components can do a better job by sinking (what you are doing now)as they do by sourcing (what you thought you were doing).

En voor het geval je dat nog niet bemerkt had, er is hier ook een Nederlandse sectie waar je je vragen in het Nederlands kunt stellen.
Wellicht dat je daar beter uit je woorden komt, maar je Engels is niet zo'n probleem hoor.
Have a look at "blink without delay".
Did you connect the grounds ?
Je kunt hier ook in het Nederlands terecht: http://arduino.cc/forum/index.php/board,77.0.html

Go Up