Hi
Just wondering if someone can assist me with what I think should be fairly simple, but has me
perplexed.
In the code below I am setting output pin 13 (RedLED) to a HIGH state.
I am then testing using the IF statement for the variable RedLED being in the HIGH state and
performing functions based on this...... if (RedLED == HIGH)
I am finding the IF statement is never true and hence the code following the IF statement
never operates.
I am using the UNO V3.0 R3
Does anyone know why?
Thanks and Regards
/*
testing purposes only - trying to familiarise myself with the programming style.
*/
int RedLED=13;
int YellowLED = 12;
int GreenLED = 11;
int Button = 10;
//int ButtonState;
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(RedLED, OUTPUT);
pinMode(YellowLED, OUTPUT);
pinMode(GreenLED, OUTPUT);
pinMode(Button, INPUT);
digitalWrite(RedLED, HIGH);
digitalWrite(GreenLED, LOW);
digitalWrite(YellowLED, LOW);
}
// the loop function runs over and over again forever
void loop()
{
digitalWrite(RedLED, HIGH);
//digitalWrite(YellowLED, HIGH);
int ButtonState = digitalRead(Button);
// if (ButtonState==HIGH && RedLED==HIGH)
//if (ButtonState==HIGH)
if (RedLED == HIGH){
delay(3000);
digitalWrite(RedLED, LOW);
digitalWrite(YellowLED, HIGH);
delay(3000);
digitalWrite(YellowLED, LOW);
digitalWrite(GreenLED, HIGH);
delay(3000);
digitalWrite(RedLED, HIGH);
digitalWrite(GreenLED, LOW);
} else {
delay(100);
}
}
RedLED is defined as having the valuue 13. HIGH is defined as having the value 1. 13 will never equal 1.
RedLED is a pin number, not the state of that pin.
RobCb:
Hi
Just wondering if someone can assist me with what I think should be fairly simple, but has me
perplexed.
In the code below I am setting output pin 13 (RedLED) to a HIGH state.
I am then testing using the IF statement for the variable RedLED being in the HIGH state and
performing functions based on this...... if (RedLED == HIGH)
I am finding the IF statement is never true and hence the code following the IF statement
never operates.
I am using the UNO V3.0 R3
Does anyone know why?
Thanks and Regards
/*
testing purposes only - trying to familiarise myself with the programming style.
*/
int RedLED=13;
int YellowLED = 12;
int GreenLED = 11;
int Button = 10;
//int ButtonState;
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(RedLED, OUTPUT);
pinMode(YellowLED, OUTPUT);
pinMode(GreenLED, OUTPUT);
pinMode(Button, INPUT);
Delta_G:
Why read an output? You should know what state you set it to. Just save that in another variable and test against it.
I tend to think that having all the pins states already stored in a port register as binary values and accessing them the same exact way as you would test the input value of the any pin is just as efficient and possibly more reliable that a global variable that you have to keep set to the same exact value as the output. But I'm sure up for your opinion as to why it would be better to store a duplicate value.
Source code for reading a digital pin state weather it is set as an input or output.
int digitalRead(uint8_t pin)
{
uint8_t timer = digitalPinToTimer(pin);
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
if (port == NOT_A_PIN) return LOW;
// If the pin that support PWM output, we need to turn it off
// before getting a digital reading.
if (timer != NOT_ON_TIMER) turnOffPWM(timer);
if (*portInputRegister(port) & bit) return HIGH;
return LOW;
}
I just thought of a sweet macro for this! I'm not at home to test this but this should be even faster than the function above, and it compiles smaller... further testing would be needed with multiple uses to verify its effectiveness.
#define DigRead(pin) (*portInputRegister(digitalPinToPort(pin)) & digitalPinToBitMask(pin)) ? HIGH : LOW // Macro for reading digital pin based on the digitalRead() function
int RedLED=13;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(RedLED, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
// 2,160 bytes
Serial.print(DigRead(RedLED)); // read the value of pin using macro defined at top.
digitalWrite(RedLED,DigRead(RedLED));
/*
// 2,228 bytes
Serial.print(digitalRead(RedLED)); // read the value of pin using digitalRead() function
digitalWrite(RedLED,digitalRead(RedLED));
*/
delay(100);
}
zhomeslice:
I tend to think that having all the pins states already stored in a port register as binary values and accessing them the same exact way as you would test the input value of the any pin is just as efficient and possibly more reliable that a global variable that you have to keep set to the same exact value as the output.
AFAIK reading a single byte variable is a lot faster than digitalRead() and the byte can be kept perfectly in sync with the output pin by using the byte to set the pin as in