# Help from someone more advanced than myself

Hello! I am new to your Arduino forums, but I am hoping to stay fir a while. I am a new engineering student, and was recently given a lab to do with very little instruction from the professor. I am hoping someone might be able to help me with the error I am getting from my code. The object of the lab is to take 2 resistor totals and put them into an equation and get out a new resistor total. I poked around the board and saw someone else who had a similar thing going and it was suggested to do while loops. I built my code and used while so that when prompted on the serial monitor it would say " enter R1 amount, then again asks for R2 total". After entering in the second resistor total, it is supposed to give me a new total, or REQ as we named it. Porblem I am having is, I keep getting a -1 no matter what I do. I read in my text book that 2 = signs are needed in order to avoid that, and have checked several times to make sure I indeed had them. The code has no issue being uploaded, but simply will not give me a correct output. Any advice would be great! here is my code as it sits right now:

``````// Processor directives
#define REQ ( R1 * R2 ) / ( R1 +R2 )                     // The equation that calculates REQ
#define R1 INPUT                                         // Resistor 1 (unknown value)
#define R2 INPUT                                         // Resistor 2 (unknown value)

void setup()  {
Serial.begin(9600);                                    //Initialize communication port at 9600 baud rate
int (R1);                                              // Set R1 to intiger.
int (R2);                                              // Set R2 to intiger.
float REQ;                                             // Set REQ to include decimals.
Serial.println("Enter the value of the resistors");
}
void loop()  {
Serial.println("What is the value of R1 in ohms?");
while ( Serial.available() == 0)
{
delay(1500);
}
Serial.println("");
Serial.println("What is the value of R2 in ohms?");
while (Serial.available() == 0)
{
delay(1500);}
Serial.println(REQ);
}
``````

All you need to do is use Serial.parseInt() for both values. nothing more.

In serial monitor type 1239 4567 there is a space between them

``````int R1, R2;
void setup()
{
Serial.begin(115200);
}

void loop() {
if(Serial.available() > 0)
{
R1 = Serial.parseInt();
R2 = Serial.parseInt();
Serial.print(R1);
Serial.print('\t');
Serial.println(R2);
}
}
``````

You can add in the other things once you understand what the code is doing.

For multiple values with prompts, I would use a counter to keep track of the values entered and an array to store the values in.

``````#define R1 INPUT                                         // Resistor 1 (unknown value)
#define R2 INPUT                                         // Resistor 2 (unknown value)
``````

This replaces every instance of the text R1 and R2 with the text (INPUT). I doubt that this is what you are looking to happen. REQ is valid is it replaces the text REQ with the equation for the resistors in parallel.

The other post gives you other ideas for the integer values

``````    R1 == Serial.read();
while ( Serial.available() == 0)
``````

First, you are reading before seeing if anything is available.

Thank you to all that replied (and very fast!) I am pretty new to the programming portion, but I think I got it. I will work on it and report back if I have more issues. I do appreciate the help, and as long as you dont mind, Id love to stick around and learn a thing or two!

== is for comparison: if(val==something). = ( one equal sign) is for assignment: val = something.

Ok, so im still a little lost. Do I need to add into my code what hazardsmind wrote out? I guess im still unclear where it adds into my code. I apologize ahead of time if these questions seem silly, the lab was to be done in class and lecture went over so there wasnt enough time for the lab, and it was made into homework.

You need to break the problem into pieces:

1. Read the value for R1
2. Read the value for R2
3. Calculate the result
4. Write out the result
5. Start at step 1 again

Then write the code to suit that flow.

You have enough hints and your own code to do that now.

This in pretty ugly, but you might experiment with it to see how it functions:

``````int input1;
int input2;

void setup() {
Serial.begin(115200);
}

void loop() {
char buff;
int digitsEntered;

if (Serial.available() > 0) {
buff[digitsEntered] = '\0';
if (input1 == 0) {
input1 = atoi(buff);
Serial.print("input1 = ");
Serial.println(input1);
} else if (input2 == 0) {
input2 = atoi(buff);
Serial.print("input2 = ");
Serial.println(input2);
}
if (input1 != 0 && input2 != 0) {
Serial.print("input1 / input2 = ");
Serial.println(input1 / input2);
}
}
}
``````

#define REQ ( R1 * R2 ) / ( R1 +R2 ) // The equation that calculates REQ

This method is fine for two resistors in parallel but it gets rather tedious when you have multiple resistors. I prefer to use this method: 1 / ( (1/R1) + (1/R2) +...+ (1/Rn) )

The resistor values can be inside an array and a FOR loop can be used to add them up.
Edit: Not type long, must be float

``````float Total =0, Final;
for(byte i=0; i < Num_of_Resistors; i++)
Total += (1 / float(Res[ i ]));

Final = (1 / Total);
``````

Example 1:

``````int array = {500,500,500};
void setup()
{
Serial.begin(115200);
Serial.println( Req(array, 3) );
}

void loop() {

}

int Req(int * Res, byte Num_of_Resistors)
{
float Total = 0;
for(byte i=0; i < Num_of_Resistors; i++)
Total += (1 / float(Res[ i ]));

return (1 / Total);
}
``````

Example 2: (Such Fanciness, much wow)

`````` //Note the location of this function, it is at the top of the code and not the bottom.
template<class T, size_t N>
T Req(T (&Res)[N])
{
float Total = 0;
for(byte i=0; i < N; i++)
Total += (1 / float(Res[ i ]));

return (1 / Total);
}

int array = {500,500,500};
void setup()
{
Serial.begin(115200);
Serial.println( Req(array) );
}

void loop() {

}
``````

Ok, so im still a little lost. Do I need to add into my code what hazardsmind wrote out? I guess im still unclear where it adds into my code.

Both HazardsMind and Econojack have given you two ways (Serial.parseInt and atoi) that you can use to address the problem that the serial input is a string of ascii characters, but you want to handle them as an integer. Serial input of 123 will not be read by the arduino as the integer, but rather the ascii characters values 49,50,and 51.