writing into pin - it takes effect after several attempts?

Hi, I am new to arduino and have written a very small code - I would like actually to perform an action when I send signal to one pin (I mean, it will be 0V on the pin connected and then I send 5V signal - I want the pin to react on that). In order to play with it, because I don't have here something which could generate me this voltage, I have done it with reading from console:

int eingang = 2;
int ausgang = 13;

void start();
void stop1();

void setup(){

void loop(){
  int incomingByte=0;
if (Serial.available() > 0) {
       incomingByte = Serial.read();
        Serial.print(incomingByte, DEC);
        if (incomingByte==102) {digitalWrite(eingang,HIGH);};
        if (incomingByte==115) {digitalWrite(eingang,LOW);};
  if (digitalRead(eingang) == HIGH) {
if (digitalRead(eingang) == LOW) {

void start(){

void stop1(){

but I am wondering. The byte 102 is letter f, 115 is letter s. And I press f and the diode is turned on. I press s and the diode stays. I press s several times and then it finally turns the diode out. With turning on it works immediately, with turning out I have to press the s many times... why it is so? many thanks

digitalWrite(eingang,HIGH); All you are doing here is enabling and disabling the input's built-in pull up resistor. Did you mean ausgang?

       if (incomingByte==102) {digitalWrite(eingang,HIGH);};
        if (incomingByte==115) {digitalWrite(eingang,LOW);};

why? should it not be

       if (incomingByte==102) digitalWrite(eingang,HIGH);
       if (incomingByte==115) digitalWrite(eingang,LOW);



is an input so why are you trying to write to it with


All you are doing is turning the internal pull up resistor on and off.

Wouldn't it be easier to make incomingByte a char and have it as incomingByte == 'f' [u]or[/u] incomingByte == 's' ?


These can be simplified

if (digitalRead(eingang) == HIGH) { Serial.println("Start!"); start(); }; if (digitalRead(eingang) == LOW) { Serial.println("Stop!"); stop1(); }; . . . void start(){ digitalWrite(ausgang,HIGH); };

void stop1(){ digitalWrite(ausgang,LOW); };

to just

if (digitalRead(eingang) == HIGH) {
   digitalWrite(ausgang, HIGH); 
else {
    digitalWrite(ausgang, LOW); 

In order to play with it, because I don't have here something which could generate me this voltage

Really? All you need is a wire to connect pin 2 to 5V or gnd on the arduino's pins.

Well just to ground if you enable the pull up resistors.

Why is there a semi colon after every curly right bracket in your code ?

Many thanks!

Can I ask a question concerning this? I have already thanks to you programmed this part, now the system sends the signal to pin 2 when pin 1 gets an external signal and I can input duration of signal sent by microprocessor.
Now I am wondering if I can hold the short proceeding times: I would like, in the moment when an external signal comes to pin 1, that the microprocessor sends a signal to pin 2 for a given given duration (int zeit) and I want to know it - I use library dTxfrUSB which does the communication with Qt interface. How can I achieve, that:

  • the system will know about coming external signal quickly (< 2 ms), so the delay between pin 1 is High → pin 2 is High is under 2 ms
  • the system will get me know about it paralelly so that the duration of the signal on pin 2 is not affected

this is my function start:

void loop(){

dT.Run();  // send the data to GUI

// timer
   if (digitalRead(pin1) == LOW) {
   if (digitalRead(pin1) == HIGH) {
      dT.C[1] = FALSE

// then another stuff to read from console


void start(){
  dT.C[1] = digitalRead(pin2);    dT.Run();
  dT.C[1] = digitalRead(pin2);    dT.Run();

How to do it that both the time between pin1 is LOW → pin2 is HIGH is short (under 2 ms) and that the duration (time between terms pin2 is HIGH and pin2 is LOW) is exactly of “zeit”. I think that the dT.Run() inbetween will delay it a bit but I don’t know how to do it that the GUI knows about it instantly (I cannot wait the whole “zeit”).

Using code to change a pin so that other code can read the pin (even if you did ground pin 2 through a big resistor to make it stop floating) is … redundant to say the least.

AVR I/O pins used for INPUT have an option to turn power on through a resistor (20k to 50k) so if not grounded it reads HIGH and if grounded it reads LOW. Use this and you can signal by grounding (button down) or not (button up). Since the power flows through the pullup resistor there will never be too much used, you can’t burn it out by grounding like you could with OUTPUT straight to ground. Using pullups can save you lots of resistors especially when you multiplex pins to read many buttons.

2 ms is a long time to Arduino. 32000 cycles long where short commands run in 1 or 2 cycles. How about that?

To make things happen on time (at least very close depending on your code, maybe 1 usec close) there is the BlinkWithoutDelay example showing how. But… I have problems with the example provided as it has some simple errors that let it work but teach bad habits.

I provide a cleaned up version. Use the version you want.
If you take the comments out and don’t count braces, mine is 12 lines of code to understand. With comments, a small book.

/* Blink without Delay -- with minor fixes by GFS

  Turns on and off a light emitting diode(LED) connected to a digital  
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.
 The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.
 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 This example code is in the public domain.
 GFS fixes and modifications -- May 5 2013:
 . changed time variables to be ALL unsigned longs, as they should be.
 . added UL to numbers being assigned to unsigned longs as should be.
 . changed the variable name 'interval' to 'blinkTime' as interval is now a
 word used by the IDE, it shows up red (like the word 'if') instead of black.
 . changed the if-else logic to change the ledState variable to 1 line XOR logic.
 . added comments about adding more tasks to the sketch.

// constants won't change. Used here to 
// set pin numbers:
const byte ledPin =  13;      // the number of the LED pin

// Variables will change:
byte ledState = LOW;             // ledState used to set the LED

unsigned long previousMillis = 0UL;  // will store last time LED was updated

unsigned long blinkTime = 1000UL;  // interval at which to blink (milliseconds)

void setup() 
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);      

void loop()
  unsigned long currentMillis = millis();
  // here is where you'd put code that needs to be running all the time.
  // GFS adds -- you see the if() { } block below? You can add more blocks
  // whether if() or switch-case or whatever to do other tasks and as long
  // as they run quick without delays or prolonged loops, your sketch will
  // be responsive as if everything runs at the same time.
  // just as the blink runs on time, another task can run when a button or 
  // sensor or serial data comes in or changes. 
  // simple commands run in less than a millionth of a second so you can pack
  // a good bit of process into a block and still run quick. analog read takes
  // longer, about 9 per millisecond so it's best not to do a bunch of those 
  // in a row but instead 1 analog read per time through loop() so other tasks
  // can get a chance in between analog reads. 
  // it's also good to avoid using floating-point as that is slooowww and avoid
  // using C++ Strings as they mess with your RAM and suck up CPU cycles doing it.

  // Back to the original program:
  // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.
  if(currentMillis - previousMillis >= blinkTime) // is it time to change the led? 
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    ledState = ledState ^ 1; // ^ is logical XOR, true if the values are different
    // using logic operations can save a lot of tedious, pain to debug if's

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);