Receiving 5 volts as an input, and then sending 5 volts as an Output

Hello!

I am trying to use an Arduino as a multiplexer. It will receive 6 different inputs from a secondary arduino; those inputs are 5 volts. The inputs are being read by the Analog Pins.

Once the board receives the 5 volts as an input, I need it to output 5 volts on a digital pin.

I have uploaded the code below. Any critiques would be much appreciated. I have been trying to figure this out for the past 3 days, and have not been able to come to any conclusion as to why this is not working for me.

The only part of the code I am not confident with is the start of the if statement, as I am not sure if it should be an integer with a value of 0-1023 or a HIGH/LOW type of communication.

// I am using an Elegoo Mega 2560
// I need the board to receive 5 volts on the analog input pins, and then output 5 volts to specific output pins.
// No matter what I do, this will not work. Not sure if I somehow fried my board maybe?

int Ipin1 = A1; // Sets the analog input to be an integer, with a variable name of "Ipin1"
int pin2 = 2; // Sets the digital pin 2 to be an integer, with a variable name of "pin2"

void setup() {
// put your setup code here, to run once:

pinMode (Ipin1, INPUT); // Sets "Ipin1" to be an input
pinMode (pin2, OUTPUT); // Sets "pin2" to be an output
Serial.begin(9600); // Sets the baudrate to 9600

}

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

if(analogRead(Ipin1 > 500)) { // I also tried "if(analogRead(Ipin1 == HIGH))", it does the same thing. (also tried < and "LOW")
digitalWrite(pin2, HIGH); // If the above statement is true, set pin2 to output 5 volts
delay(2000); // Pauses the program for 2 seconds
Serial.print("AnalogPin1 was given a signal "); // Provides some kind of feedback for the serial monitor
digitalWrite(pin2, LOW); // Turns off the output of pin 2
}

}

"will not work" conveys no useful information. The code does something, what does it do?

Look at that statement very carefully comparing it to the syntax for the analogRead() function and the if structure. Note, especially, the placements of the parenthesis.

Read the forum guidelines to see how to properly post code and some hints on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

@daveypoo48, I'm not sure why you think that this relates to Avrdude, stk500 or Bootloader issues and hence your topic has been moved to a more suitable location on the forum.

Hi, @daveypoo48
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

This will help with advice on how to present your code and problems.

The 5volts input, is it 0V then 5V, if so then you can use a faster digital input.
0V = Low or False, 5V = HIGH or True.
The Arduinos that are supplying the signal would probably be digital outputs.

How far between the the controllers?

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

If they are always 5V why do you need to read them? Are they 0-5V analog ? or "logic level" signals

The info on the above page will show how you can decide if an input on an analog pin is a "logic 1" or "logic 0"

else ... ?

you cant with any validity test an integer value from an analog input against a boolean value.

Hello
Post a real schematic to see how we can help.
Have a nice day and enjoy coding in C++.

It looks like what you wanted to write was:

// I am using an Elegoo Mega 2560
// I need the board to receive 5 volts on the analog input pins, and then output 5 volts to specific output pins.
// No matter what I do, this will not work. Not sure if I somehow fried my board maybe?

int Ipin1 = A1;
int pin2 = 2;

void setup()
{
  // pinMode(Ipin1, INPUT);
  pinMode(pin2, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  if (analogRead(Ipin1) > 500) // About 2.44V or higher
  {
    Serial.println("AnalogPin1 was given a signal ");
    digitalWrite(pin2, HIGH);
    delay(2000);
    digitalWrite(pin2, LOW);
  }
}

Why use an analogue read and not a digital read?
Because your code is in a loop then once the signal has gone high and the output has gone high and then low again, then if the input is still high the output will immediately go high again. In other words you will never see the low signal.

On the face of it you don't need a processor just connect your inputs to your outputs.

Thank you, sorry about that. Making my way around the website for the first time was very difficult to navigate.

In the future I will do a better job of formatting a post.

Thank you so much for your response.

Sorry about the format of the code; In my experience, using the apostrophe button would automatically format my code on other websites. Regardless, I made that mistake, it was my fault, I should have double checked the formatting before I posted.

I was able to make the code work, I appreciate your help with pointing out my parentheses mistake.

I am not using the digital pins simply because I need almost all of them (50, to be exact) to output signals.

Can you tell me how I would format the analog pins to read HIGH or LOW?

I tried;

  if(analogRead(Ipin1) == HIGH) {

but it did not work; I would send the pin 5 volts and it would not recognize the signal was being sent, thus, it obviously did not output any signal.

Just treat your analog inputs as digital ones if you don't need them to measure truly analog signals.

const uint8_t pinA0 = A0;

void setup() 
{
    pinMode( pinA0, INPUT_PULLUP );
    Serial.begin( 115200 );

}//setup

void loop() 
{
    Serial.println( digitalRead( pinA0 ) );
        
}//loop

Hi,
If you are inputting from other Arduinos, rather than;

pinMode( pinA0, INPUT_PULLUP );

this will do;

pinMode( pinA0, INPUT );

Tom... :grinning: :+1: :coffee: :australia:

Hi,
Can you please tell us your project application.
What are you outputing to?

Can you please post a basic block diagram of your project?

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

Tom,

I am only just now figuring out the sad realization that the Elegoo Mega cannot receive more than 1 input at the same time. Am I correct in that realization?

The outputs from the Elegoo go to 100 different relays. This system is acting as a simple controller.

Thanks so much, JohnWasser. I really appreciate your help here.

I doubt it.

Can you please give a detailed description of your project; this should include examples of the data that you send from the PC to the Uno and what the expected output on the Mega would be.

The Uno code would be useful as well to help us understand.

I suspect that the GUI e.g. sends 5, the Uno translates this to e.g. 5 on the output pins, the Mega reads this and sets a specific output HIGH.

Hi,
You can't use pin1 of the UNOs, that and pin 0 are the programming I/O.

Have you got 100 drivers for the relays?

Why not use I/O expanders for your two Megas.
At the moment you have 3 lots of code.
If you use one controller, UNO or Nano, and a series of port expanders you only need ONE code.

Tom.... :grinning: :+1: :coffee: :australia:
PS. This sounds like a piece of performance, kinetic art.

Computer is connected to Arduino via a hardwire.

A very simple GUI, coded in Python, tells the Arduino which digital pins to set to HIGH. I am only using digital pins 2-13 on the Arduino (12 in total). THIS MEANS THAT THE ARDUINO IS SENDING A CONSTANT 5 VOLTS VIA THE DIGITAL OUTPUTS, FOR ABOUT 1 SECOND EACH.

Digital outputs 2-7 are sent to the first Elegoo Board. Digital outputs 8-13 are sent to the second Elegoo Board.

The original problem I faced was the Elegoo not reading the 5 volt input from the arduino. I have already fixed that problem.

Now, my question is, can the Elegoo boards receive more than one input from the arduino at the same time?