What make this code have different reading

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

}

void loop () {

  int reading = analogRead (A0);
  float voltage = reading * (5.0 / 1023.0 );

  Serial.println (voltage);
  delay (1000);

}

hi.. what make the different reading from the above code with this below code
the voltage gap very big, in the code below

void setup () {
  pinMode (A0, OUTPUT);
  Serial.begin (9600);

}

void loop () {

  int reading = analogRead (A0);
  float voltage = reading * (5.0 / 1023.0 );

  Serial.println (voltage);
  delay (1000);

}

pinMode (A0, OUTPUT);

???

Please use code tags when posting code.

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan

1 Like

thanks stefan.. i got now.. edited

Hi,
Did you change;

 pinMode (A0, OUTPUT);

to;

 pinMode (A0, INPUT);

Although in most cases if you are using the analog pins as analog inputs, the analogRead() will do it for you.
So you do not necessarily have to pinMode pinA0 at all.

Can you please post your circuit diagram?

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

1 Like

Your second code has a line

 pinMode (A0, OUTPUT);

which does define your IO-pin as an output

analogRead() does work correctly only if you leave the IO-pin A0 as input.

see this thread

best regards Stefan

thanks stefan.. i got now

Hi amilah,

I have a request. Would you be so kind and read through the following code and answer very honest if this code is easy or hard to understand for you?

/* Blink without Delay
delay() is the most crappy command that makes every newcomer scratch
their head why does my program not work as expected?

This demo-code is structured into functions
to all newcomers: programming with functions needs 1 minute more time
to write the code but saves 2 to 10 HOURS to find bugs
= you will finish your project much faster because you write a small function
test function and then write the next function. 
This means if a bug occurs it is located in a much smaller area 
= much faster to find. ==> faster finished project

This version of Blink without delay is based on a function 
called "TimePeriodIsOver" 

This function gives back a result "true"  or "false" depending on what the name
of the function says "timePeriodIsOver"

You need ONE additional line of code to make it work.
You have to define a variable that acts as a timing-variable

example: unsigned long myTimer; 

  This example code is in the public domain.
*/

// constants won't change. Used here to set a pin number:
const int ledPin =  LED_BUILTIN;// the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long myTimer;            // timing-variable
const unsigned long    myInterval = 1000;  // interval at which to blink (milliseconds)


// TimePeriodIsOver ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
// function for timing just copy & paste without modifying it
boolean timePeriodIsOver (unsigned long &periodStartTime, unsigned long timePeriod) {
  unsigned long currentMillis  = millis();  
  if ( currentMillis - periodStartTime >= timePeriod )
  {
    periodStartTime = currentMillis; // set new expireTime
    return true;                     // more time than TimePeriod) has elapsed since last time if-condition was true
  } 
  else return false;                 // not expired
} //TimePeriodIsOver ----------------------------------------------------------------------------


// LED-switching  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// function for switching the LED
void switchLEDState() {
  // if the LED is off turn it on and vice-versa:
  if (ledState == LOW) {
    ledState = HIGH;
  } 
  else {
    ledState = LOW;
  }
  digitalWrite(ledPin, ledState); // set the LED with the ledState of the variable:
} //LED-switching --------------------------------------------------------------


void setup() {
  pinMode(ledPin, OUTPUT);   // set the digital pin as output:
}


void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check if TimePeriodIsOver if true then change the state of the LED
  if (  timePeriodIsOver(myTimer,myInterval) ) {    
    switchLEDState(); // a single line of code with a SELF-explaining name what the function does 
  }
  
}

thank you very much
if you want to ask questions abou this code ask as much as you want
best regards Stefan

1 Like

thanks stefan.. i understand and need more practice

My goal is to write code-examples that are really easy to undestand.

of course You are free to say "no that's not what I want to do next"
I accept this no and will still contine to answer your own questions.

Can you make a try to adapt your code with the analog read to make use of my code-example ?

Your code uses a

delay(1000); 

to send the value only once per second to the serial monitor.

So try to adapt your code that it does not use delay(1000)
but my timing-function

best regards Stefan

Sneaky

Hi LarryD,

sneaky is a hard word for that.
I want to emphasize again: amila if it is not your thing just post a short "no" or ignore it and everything will be fine.

best regards Stefan

hi stefan.. i am studying the codes u gave me

It wasn’t meant to be hard.

How about crafty or ingenious.

I'm trying to create a win-win-situation

I’m giving you kudos Stefan.

         
unsigned long previousMillis = 0;        
const long interval = 1000;         

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

void loop() {
  
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {  
    previousMillis = currentMillis; 
    
    Serial.println (float ( analogRead(A0)) *  (5.0 / 1023) );
  }
}