Read/Write

Hi I want to read digital pin 13 and the results print on pin 1. This attempt is not working. How to fix it ?

int led = 13;
int out = digitalRead(led);

void setup() {                
  
  pinMode(led, OUTPUT); 
  pinMode(1, OUTPUT);    
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
  digitalWrite(1, out);
}
int led = 13;
int out;

void setup() {                
  
  pinMode(led, INPUT); 
  pinMode(1, OUTPUT);    
}

// the loop routine runs over and over again forever:
void loop() {
  out = digitalRead(led);
  digitalWrite(1, out);
}

It is not obvious what you are trying to achieve.

If this is an UNO board, pin 1 is the TX pin. Suggest you avoid this pin.

“I want to read digital pin 13 and the results print on pin 1.” How will you be wiring the input to the sensor?

Vik321: Hi I want to read digital pin 13 and the results print on pin 1. This attempt is not working. How to fix it ?[/quote

First switch to a blink-without-delay technique, so the blinking of one LED doesn't tie up the entire processor's 16 million instructions per second.

Yes, you can read back from an output pin to find out what was written to that pin. I don't like to do that because I think it's better to keep your own record of what was written. The code below uses that value in two places, so it could have read from the pin in 2 places or it could use the variable.

int ledPin = 13;
int outPin = 2;      //I changed the output pin; pin 0 and 1 are Serial pins on most Arduinos and should be reserved for that
//don't attempt to do a digitalRead() here - the full system has not started up at this point
bool outLEDValue = LOW;  //start the LED LOW
const unsigned long onPeriodLED = 200; //milliseconds, the time that the LED is on for
const unsigned long offPeriodLED = 400; //milliseconds, the time that it is dark

void setup() {

 pinMode(ledPin, OUTPUT);  pinMode(1, OUTPUT);  digitalWrite(ledPin, outLEDValue); }

// the loop routine runs over and over again forever: void loop() {

 blinkyLED();

 digitalWrite(outPin, outLEDValue);   }

//--------------------------------- // blinkyLED // // If it's time to turn the LED on // or off, then do so. //--------------------------------- void blinkyLED() {  static unsigned long lastLEDChangeTime = 0;  //use this to record when it was last changed from on to off or off to on

 if (outLEDValue) {    if (millis() - lastLEDChangeTime > onPeriodLED) {      //It's time to turn it off      outLEDValue = LOW;      digitalWrite(ledPin, outLEDValue);      //record the time that we turned it off      lastLEDChangeTime = millis();    }  } else {    if (millis() - lastLEDChangeTime > offPeriodLED) {      //It's time to turn it on      outLEDValue = HIGH;      digitalWrite(ledPin, outLEDValue);      //record the time that we turned it on      lastLEDChangeTime = millis();    }  } }

This is a LED blink code, on pin 13 I have impulses = output, this output I want to read it then write on another pin. Normally can be done by wire jumper from pi 13 to pin 2, read pin 2 and write on pin 1. My goal is to eliminate the wire jumper.

If I understand what you're asking, you can read the current state of an output pin using the digitalRead() command for that pin -- ie. bool state = digitalRead(13);

Hope this helps

Vik321: This is a LED blink code, on pin 13 I have impulses = output, this output I want to read it then write on another pin. Normally can be done by wire jumper from pi 13 to pin 2, read pin 2 and write on pin 1. My goal is to eliminate the wire jumper.

Simplest statement to do that would be:

digitalWrite(1, digitalRead(13) );

although in the code you posted, it isn't necessary to read the state of pin 13, just write the same thing to both pins:

void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(1,HIGH);
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(1,LOW);
  delay(1000);               // wait for a second
}

As was pointed out, pin 1 on several of the arduino boards is the serial output pin, using that in a sketch may lead to problems, such as making it difficult to load another sketch onto the board.

This is the idea. Let say you have a sine wave generator and want to convert it to square the simplest will be voltage comparator = additional hardware, reading sine by digitalRead I am expecting convert sine to square.

But it will be very difficult to make the square wave 50% duty cycle. A dedicated analog comparator should be used before relying on the digital thresholds.