Sending then reading 5V

I'm new to the Arduino code and would like some basic help. I'm using the Arduino Uno PCB.

I'm confused how to read if an input is high when an output pin is sending out 5V. It seems to me that when I would read the input pin when the output is sending a signal, the output would stop sending the signal when the read process started and the input would always be low. Also any debouncing information on the start pin would be awesome!

If the connection between pin 1 and 2 is good, light an LED.

In english what I'm looking for:

  • if startButton is pressed and held the input pin monitoring it sees it as high
  • then send 5V out pin 1
  • then read input pin (pin 2) to see if voltage is successfulling making it (through the jumper wire from pin 1 to pin 2).

So far I've got:

int pin1 = 0; int pin2 = 0; int ledGreen = 16; int startButton = 18;

void setup() { // put your setup code here, to run once: pinMode (pin1, OUTPUT); pinMode (pin2, INPUT); pinMode(ledGreen, OUTPUT);

void loop() { // put your main code here, to run repeatedly:

if (startButton == HIGH); digitalWrite (pin 1); digitalRead (pin 2);

If (pin2 == HIGH); digitalWrite(ledGreen);

else (ledGreen = LOW);

}

Get rid of the ; at the end of the "for, if" lines. Double check if you want braces? {}

if (startButton == HIGH); no ;

If is not if

I give up digitalWrite(ledGreen); :o

At least compile the code before posting it.

Code tags are your friends (slash inside angle brackets, above smilyfaces).

I see a few coding problems right off the bat. There are at least 3 syntax errors, and 4 reference errors.

Some of this should have been VERY obvious had you put it into the IDE and attempted to compile it. You might want to try actually using your code before posting it, so you know what questions to ask.

As an exercise, you should take the code you posted here, verbatim, and attempt to compile it. Then open up a couple of basic examples (blink might be a good start) and do a little comparing to see why you get the errors you will. Then come back and ask any questions you may still have about why it wont compile.

Use code tags [ code ] [ / code ] in future.

int pin1 = 0;
int pin2 = 0;
int ledGreen = 16;
int startButton = 18;



void setup() {
  // put your setup code here, to run once:
  pinMode (pin1, OUTPUT);
  pinMode (pin2, INPUT);
  pinMode(ledGreen, OUTPUT);


void loop() {
  // put your main code here, to run repeatedly:

if (startButton == HIGH);
digitalWrite (pin 1);
digitalRead (pin 2);

If (pin2 == HIGH);
digitalWrite(ledGreen);

else (ledGreen = LOW);


}

Couple of things:

if (startButton == HIGH);
digitalWrite (pin 1);
digitalRead (pin 2);

Should read:

if (startButton == HIGH){
digitalWrite (pin 1);
digitalRead (pin 2);
}

The issue with the code here, is you ARE reading the pin, but doing nothing with the value. You want to store it in a variable...

if (startButton == HIGH){
digitalWrite (pin 1);
byte pin_2_state=digitalRead (pin 2); // variable pin_2_state will be 0 if it was 0v and 1 is 5v.
}

Then you can do a comparison:

if (pin_2_state == 1){
digitalWrite(ledGreen);
}

else{
(digitalWrite(ledGreen,LOW);
}

Better still would be simpler code. All you are trying to achieve is reading a pin state when pressing a button and turning on an LED dependent on the pins state.

PS! NOT CHECKED THIS AND JUST WROTE IT WITHOUT TESTING...

byte pin1 = 0;      //values will not go over 0-255...so a byte is more than enough!
byte pin2 = 0;      //values will not go over 0-255...so a byte is more than enough!
byte ledGreen = 16; //values will not go over 0-255...so a byte is more than enough!
byte startButton = 18; //values will not go over 0-255...so a byte is more than enough!

void setup() {
  pinMode (pin1, OUTPUT);
  pinMode (pin2, INPUT);
  pinMode(ledGreen, OUTPUT);
}

void loop() {


  while (!digitalRead(startButton)) {}                     // While startButton is not High, do nothing.

  digitalWrite(pin1, random(0, 1));         // Make the pin1 HIGH OR LOW randomly. 1=HIGH, 0=LOW.

  if (digitalRead(pin2)) {                //If pin2 is high (equal to 1 / returns 1)...
    digitalWrite(ledGreen, 1);             // Turn on the LED...
  }
  delay(2000);                             // Wait 2 seconds before turning the LED off it it were on...and stops the button being repressed for 2 seconds.
  digitalWrite(ledGreen, 0);               // turn the LED off.

}