I want make it doing.. If Pin2 == High then LED 'ON'
and Pin2 == Low then LED 'OFF'
but it is working too late when Pin2 transit High-->Low
I don't know what's problem.. plz review below code..
const int LEDPin = 13;
const int inputPin = 2;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(LEDPin,OUTPUT);
pinMode(inputPin,INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int val = digitalRead(inputPin);
if(val == HIGH){
Serial.print("HIGH Voltage was delivered to INPUT PIN = ");
Serial.println(val);
digitalWrite(LEDPin,HIGH);
}else{
Serial.print("LOW Voltage was delivered to INPUT PIN = ");
Serial.println(val);
digitalWrite(LEDPin,LOW);
}
}
but it is working too late when Pin2 transit High-->Low
I don't know what's problem.. plz review below code..
What does "too late" mean? How many microseconds after the level change on pin 2 are you registering activity on the LED pin? How do you change the voltage level on pin 2?
The prints start blocking; there is only space for 64 bytes on the software serial buffer. Once that is full, your code will block till all characters that you wanted to print have been moved into that buffer.
Make your messages shorter and/or increase the baudrate should make a difference in your observations.
And only reacting when the pin goes high or low (state change detection) instead of when the pin is high or is low is the preferred solution here.