First Project-measuring voltage programming

ok, I read about for loops and I was wondering if you meant that I should be able to do this with my code, cycle through measuring and recording each analog input.

Now that I'm thinking about it, I don't really need the 1 second delay between each of my measurements, do I? Because if I take out the delays, my void loop looks like this:

void loop () 
{ 
int sensorValue; 
float voltage; 

//measure analog input A0, convert into volts and print to serial monitor
Serial.println ((float)analogRead (A0) * (5.0 / 1023.0));
//Same function as above with input A1
Serial.println ((float)analogRead (A1) * (5.0 / 1023.0));
//Repeating through A5
Serial.println ((float)analogRead (A2) * (5.0 / 1023.0));
Serial.println ((float)analogRead (A3) * (5.0 / 1023.0));
Serial.println ((float)analogRead (A4) * (5.0 / 1023.0)); 
Serial.println ((float)analogRead (A5) * (5.0 / 1023.0)); 
//repeat process again after 15 mins 
delay (900000); 
}

Regarding the for loop: This is completely an experiment, and probably a flat out failure:

// experiment with for loop//

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

void loop() {
  int sensorValue = analogRead;
  float voltage;
  for (analogRead = A0; sensorValue < A5; ) { 
    voltage = sensorValue * (5.0 / 1023.0);
    Serial.println (voltage);  
    delay(90000);
  }
}

errors:

for_loop_experiment.ino: In function 'void loop()':
for_loop_experiment:9: error: invalid conversion from 'int (*)(uint8_t)' to 'int'
for_loop_experiment:11: error: assignment of function 'int analogRead(uint8_t)'
for_loop_experiment:11: error: cannot convert 'const uint8_t' to 'int ()(uint8_t)' in assignment

You are usi g analogRead incorrectly. It is a function call and can only be used inside a function. You must call it with the number of the Analog Pi you want to use ........like this

 variable = analogRead(0);

I'm not sure if this is what you meant but this is what I got from that:

// experiment with for loop//

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


void loop() {
  int sensorValue;
  float voltage;
  for (sensorValue = A0; sensorValue < A5; ) { 
    voltage = sensorValue * (5.0 / 1023.0);
    Serial.println (voltage);  
    delay(90000);
  }
}

Another thing, will this:

Serial.println ((float)analogRead (A0-5) * (5.0 / 1023.0));

do the same thing as this:

Serial.println ((float)analogRead (A0) * (5.0 / 1023.0));
Serial.println ((float)analogRead (A1) * (5.0 / 1023.0));
Serial.println ((float)analogRead (A2) * (5.0 / 1023.0));
Serial.println ((float)analogRead (A3) * (5.0 / 1023.0));
Serial.println ((float)analogRead (A4) * (5.0 / 1023.0)); 
Serial.println ((float)analogRead (A5) * (5.0 / 1023.0));

I'm not sure if this is what you meant but this is what I got from that:

No read it again.
This is wrong

  int sensorValue = analogRead;
// should be
  int sensorValue = analogRead(0); // or what ever pin you use

Same goes for other places. That for statement is wrong, look up the syntax in the referance.

No it will not do the same thing.

Grumpy_Mike:

I'm not sure if this is what you meant but this is what I got from that:

No read it again.
This is wrong

  int sensorValue = analogRead;

// should be
 int sensorValue = analogRead(0); // or what ever pin you use

ok, it looks like this works, but I assume the A0-A5 doesn't work here as well. What should I put in place of it to measure all 6?

// experiment with for loop//

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


void loop() {
  int sensorValue = analogRead(A0-A5);
  float voltage;
  for (sensorValue = A0; sensorValue < A5; ) { 
    voltage = sensorValue * (5.0 / 1023.0);
    Serial.println (voltage);  
    delay(90000);
  }
}

This will get me the same place as what I had before, but with less writing?

Obviously change it to meet your exact needs but this is what you are looking for

  for (int sensorPin = A0; sensorPin <= A5; sensorPin++)
  {
    Serial.println((float)analogRead (sensorPin) * (5.0 / 1023.0));
  }

UKHeliBob:
Obviously change it to meet your exact needs but this is what you are looking for

  for (int sensorPin = A0; sensorPin <= A5; sensorPin++)

{
   Serial.println((float)analogRead (sensorPin) * (5.0 / 1023.0));
 }

Ok, great. That makes sense.

Do I need to say this still I assume

int sensorValue = analogRead;
Float voltage;

Do I need to say this still I assume

No, all you need to print all 6 analogue ports is in those few lines of code.

int sensorValue = analogRead;

nope int sensorValue = analogRead();

lmboyer:
Do I need to say this still I assume

int sensorValue = analogRead;

Float voltage;/code]

To expand on Peter's answer, you do not need anything else to print the 6 analogue values because the voltage is calculated and printed and never stored in a variable, so one is not needed, and the sensorPin variable is declared as part of the for loop and is local to it.

You could read the analogue values and store then in an array for use later in the program, perhaps to compare with one another or to trigger actions when one or more goes out of range. Would that be of interest ?

You read the first analog input, into an int variable ( in ADC count units ) , and then convert to a float ( in volts ), and then print it.

Then you do this five more times.

Your second code attempt was almost correct. You got worse and worse, from there onwards.

The error in your second code attempt, is that you keep re-creating the int variable, into which you put the analog reading. You should not do this. You declare the variable to be an int variable the first time in the loop, and then keep using the same one.

Instead of wasting our time and yours, get an elementary programming book, and read it. Randomly trying things may be a workable approach with modern phones, you won't get far that way in using computers.

another thing is, I don't think that you'll get a correct reading from the analogRead with those calculations...
What kind of hardware are you using to measure the voltage??

Look here:

and you'll have a great start on building the calculation in the code and the way to measure it :slight_smile:

michinyon:
Instead of wasting our time and yours, get an elementary programming book, and read it. Randomly trying things may be a workable approach with modern phones, you won't get far that way in using computers.

Yes, I know that I should read books, and I definitely will, but this project is time sensitive so I need to just figure out how to do it first. Afterwards though, I will definitely stay with Arduino and read a lot about it to learn how to use it and have more fun with it.

Are you saying my original 2nd code attempt, or my 2nd for loop code attempt was closest? I thought I had it right already and was just making the code shorter, and am now fixing the mistakes I made by making it shorter without knowledge on how to do so.

Anyway, I'm sorry for wasting our time, but I think (and could be totally wrong) that I almost have it figured out, then I can leave you in peace...

// experiment with for loop//

void setup() {
  Serial.begin(9600);
  for (int sensorPin = A0; sensorPin <A6; sensorPin++);
}


void loop() {
  int sensorPin;
  float voltage;
  for (sensorPin = A0; sensorPin < A6; sensorPin++) { 
    voltage = sensorPin * (5.0 / 1023.0);
    Serial.println ((float)analogRead (sensorPin) * (5.0/1023));  
    delay(900000);
  }
}

again, no error messages.

Pady:
another thing is, I don't think that you'll get a correct reading from the analogRead with those calculations...
What kind of hardware are you using to measure the voltage??

Why is that? I haven't heard anything about that yet, but I guess it could be true. Im measuring the voltage and converting it into voltage to be printed to the serial monitor. I had planned on just connecting each circuit to the analog input pins. I based it off of a modification to this project: http://arduino.cc/en/Tutorial/ReadAnalogVoltage

voltage = sensorPin * (5.0 / 1023.0);

No error messages, but no sensible answers either.

Would it help if I changed it from

voltage = sensorPin * (5.0 / 1023.0);

to

float voltage = sensorPin * (5.0 / 1023.0);

or completely get rid of that line:

void loop() {
  int sensorPin;
  float voltage;
  for (sensorPin = A0; sensorPin < A6; sensorPin++) { 
    Serial.println ((float)analogRead (sensorPin) * (5.0/1023));  
    delay(900000);
  }
}

No, it wouldn't help at all.
Read it again

OK why have you changed things from reply #25.
That code is just crap, are you not paying attention to what we are telling you!

Would it help if I changed it from

NO!

A sensor pin number is just that a number. To get the reading on that number you have to put it into an analogRead function then you have to assign what that returns to a variable or use it in a print statement.

Grumpy_Mike:
OK why have you changed things from reply #25.

assign what that returns to a variable or use it in a print statement.

I have attempted to do what you suggested and look back at the 25th reply. I managed to come up with this, however, I am not sure that I have successfully "assigned the data as a variable" or not. I know all that does is reinforce the fact that I know nothing and that I need to read some books. I am trying to listen to you guys and I am trying to work things out without really knowing how doing this. I am trying my best to interpret it the best I can, and I will go straight to learning the code first next time, but all I am trying to do at this point is finish this project. I'm sorry for all of the strife I have caused.

// experiment with for loop//

void setup() {
  Serial.begin(9600);
  for (int sensorPin = A0; sensorPin <= A5; sensorPin++);
}


void loop() {
  int sensorPin;
  float voltage;
  for (int sensorPin = A0; sensorPin <= A5; sensorPin++) 
  { 
    Serial.println ((float)analogRead (sensorPin) * (5.0/1023));  
    delay(900000);
  }
}
  for (int sensorPin = A0; sensorPin <= A5; sensorPin++);

Pointless in setup.

 float voltage;

You don't use it, so why bother?

AWOL:

  for (int sensorPin = A0; sensorPin <= A5; sensorPin++);

Pointless in setup.

 float voltage;

You don't use it, so why bother?

Ok, I guess that makes sense.
Here it is again without those:

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


void loop() {
  int sensorPin;
  for (int sensorPin = A0; sensorPin <= A5; sensorPin++) 
  { 
    Serial.println ((float)analogRead (sensorPin) * (5.0/1023));  
    delay(900000);
  }
}