I'm having difficulty understanding the AnologRead() function.
According to the definition presented below, the AnalogRead function will measure the output voltage of a pin as a 10 bit integer with 0 = 0V and 1023 = to the board voltage. That to me means that if I am using a 3.3V Arduino and measure 1V at the pin, then the AnalogRead should return 1023*1/3.3 = 310.
I'm not getting that. When I do a Serial.print() of the variable I get a value equal to 1023-310 = 713
Everything works, but I have to program based on the AnalogRead subtracted from 1023.
Totally irrelevant what it was set to in a previous sketch, unless the current circuit was hooked up while running the previous sketch and something was damaged.
We need to actually see the OP's sketch, and how everything is connected, or everything is just guessing.
The current board is a Yourdrino RoboRed. which is just like an Uno but can be used with 21V input. The board I will be using is a Nano 33 BLE.
Attached is the section of code where I read the pin and print it. Below that is the output of Serial Monitor.
The issue occurs with both channels Radpin and CondPin. With the output presented the pins for RadPin and CondPin measure at 1.59V and 1.03V respectively. and are reporting 515 and 700,
#include <PWM.h>
// Note for low frequency < 31Hz,the high resolution pwmWriteHR() must be used with 16 bit PWM Value
//For the RoboRed only pins 9 and 10 can be used with 16 bit resolution
int RadPin = A0;
int CondPin = A1;
int AuxPin = A2;
int RadVals[6] = {0, 0, 0, 0, 0, 0};
int CondVals[6] = {0, 0, 0, 0, 0, 0};
int AuxVals[6] = {0, 0, 0, 0, 0, 0};
// 10 bit temperature equivolents for radiator sensor
int Rad20 = 444; //85C
int Rad30 = 484; //90C
int Rad40 = 524; //95C
int Rad50 = 562; //100C
int Rad60 = 599; //105C
int Rad70 = 635; //110C
int Rad80 = 668; //115C
int Rad90 = 699; //120C
// 10 bit temperature equivolents for condenser sensor
int Cnd20 = 480; //40C
int Cnd30 = 530; //45C
int Cnd40 = 579; //50C
int Cnd50 = 626; //55C
int Cnd60 = 669; //60C
int Cnd70 = 709; //65C
int Cnd80 = 745; //70C
int Cnd90 = 778; //75C
int Aux20 = 0;
int Aux30 = 0;
int Aux40 = 0;
int Aux50 = 0;
int Aux60 = 0;
int Aux70 = 0;
int Aux80 = 0;
int Aux90 = 0;
int RadSpeed = 10;
int CondSpeed = 10;
int AuxSpeed = 10;
int FanSpeed = 10; // Set fan to 10% PWM = off
int FanPin = 9; // Set Fan pin
int16_t PWMVal;
int32_t frequency = 10;
int t1;
void setup() {
// put your setup code here, to run once:
pinMode(RadPin, INPUT);
Serial.begin(9600);
InitTimersSafe()
//sets the frequency for the specified pin
; bool success = SetPinFrequencySafe(FanPin, frequency);
//if the pin frequency was set successfully, pin 13 turn on. Pin 13 can be used to light an LED etc.
if (success) {
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
}
}
void loop() {
// put your main code here, to run repeatedly:
//this code prints sensor value to the console
Serial.print("Rad pin input = "); Serial.println(RadVals[0]);
Serial.print("Cond pin input = "); Serial.println(CondVals[0]);
Serial.print("Aux pin input = "); Serial.println(AuxVals[0]);
Serial.println("Yo1");
Serial.println();
// delay(1000);
// t1=millis();
// do {
// } while (millis()-t1 <1000);
// initialize values
RadVals[5] = 0;
CondVals[5] = 0;
AuxVals[5] = 0;
//read sensor value and set upper limit cap
//for (int i = 1; i <= 5; i++) {
int i = 0;
do {
// Find 5 data points
RadVals[i] = analogRead(RadPin);
CondVals[i] = analogRead(CondPin);
AuxVals[i] = analogRead(AuxPin);
Serial.print("Rad pin input stored = "); Serial.println(RadVals[i]);
Serial.print("Cond pin input stored = "); Serial.println(CondVals[i]);
Serial.print("Aux pin input stored = "); Serial.println(AuxVals[i]);
Serial.println(i);
Serial.println("Yo2");
Serial.println();
// delay(1000);
//Serial.print("Rad pin input before sum = "); Serial.println(RadVals[5]);
//Serial.println("Yo25");
//Serial.println();
// sum all 5 data points
RadVals[5] = RadVals[5] + RadVals[i];
CondVals[5] = CondVals[5] + CondVals[i];
AuxVals[5] = AuxVals[5] + AuxVals[i];
Serial.print("Rad pin input sum = "); Serial.println(RadVals[5]);
Serial.print("Cond pin input sum = "); Serial.println(CondVals[5]);
Serial.print("Aux pin input sum = "); Serial.println(AuxVals[5]);
Serial.println(i);
Serial.println("Yo3");
Serial.println();
// delay(1000);
i = i + 1;
} while (i < 5);
// Find Average of 5 data points
RadVals[5] = RadVals[5] / 5;
CondVals[5] = CondVals[5] / 5;
AuxVals[5] = AuxVals[5] / 5;
Serial.print("Rad pin input Average = "); Serial.println(RadVals[5]);
Serial.print("Cond pin input Average = "); Serial.println(CondVals[5]);
Serial.print("Aux pin input Average = "); Serial.println(AuxVals[5]);
Serial.println(i);
Serial.println("Yo4");
Serial.println();
// delay(1000);
if (RadVals[5] < Rad20) RadSpeed = 10;
if (RadVals[5] < Rad30 and RadVals[5] >= Rad20) RadSpeed = 20;
if (RadVals[5] < Rad40 and RadVals[5] >= Rad30) RadSpeed = 30;
if (RadVals[5] < Rad50 and RadVals[5] >= Rad40) RadSpeed = 40;
if (RadVals[5] < Rad60 and RadVals[5] >= Rad50) RadSpeed = 50;
if (RadVals[5] < Rad70 and RadVals[5] >= Rad60) RadSpeed = 60;
if (RadVals[5] < Rad80 and RadVals[5] >= Rad70) RadSpeed = 70;
if (RadVals[5] < Rad90 and RadVals[5] >= Rad80) RadSpeed = 80;
if (RadVals[5] >= Rad90) RadSpeed = 90;
if (CondVals[5] < Cnd20) CondSpeed = 10;
if (CondVals[5] < Cnd30 and CondVals[5] >= Cnd20) CondSpeed = 20;
if (CondVals[5] < Cnd40 and CondVals[5] >= Cnd30) CondSpeed = 30;
if (CondVals[5] < Cnd50 and CondVals[5] >= Cnd40) CondSpeed = 40;
if (CondVals[5] < Cnd60 and CondVals[5] >= Cnd50) CondSpeed = 50;
if (CondVals[5] < Cnd70 and CondVals[5] >= Cnd60) CondSpeed = 60;
if (CondVals[5] < Cnd80 and CondVals[5] >= Cnd70) CondSpeed = 70;
if (CondVals[5] < Cnd90 and CondVals[5] >= Cnd80) CondSpeed = 80;
if (CondVals[5] >= Cnd90) CondSpeed = 90;
if (AuxVals[5] < Aux20) AuxSpeed = 10;
if (AuxVals[5] < Aux30 and AuxVals[5] >= Aux20) AuxSpeed = 20;
if (AuxVals[5] < Aux40 and AuxVals[5] >= Aux30) AuxSpeed = 30;
if (AuxVals[5] < Aux50 and AuxVals[5] >= Aux40) AuxSpeed = 40;
if (AuxVals[5] < Aux60 and AuxVals[5] >= Aux50) AuxSpeed = 50;
if (AuxVals[5] < Aux70 and AuxVals[5] >= Aux60) AuxSpeed = 60;
if (AuxVals[5] < Aux80 and AuxVals[5] >= Aux70) AuxSpeed = 70;
if (AuxVals[5] < Aux90 and AuxVals[5] >= Aux80) AuxSpeed = 80;
if (AuxVals[5] >= Aux90) AuxSpeed = 90;
FanSpeed = max(RadSpeed, CondSpeed);
//map and assign pwm values to the fan output 0 to 65535 corresponds to 0 to 100%
PWMVal = map(FanSpeed, 0, 100, 0, 65535);
Serial.print("Rad pin input Average = "); Serial.println(RadVals[5]);
Serial.print("Cond pin input Average = "); Serial.println(CondVals[5]);
Serial.print("Aux pin input Average = "); Serial.println(AuxVals[5]);
Serial.print("Fan Speed = "); Serial.println(FanSpeed);
Serial.print("PMWval = "); Serial.println(PWMVal);
Serial.println("Yo4");
Serial.println();
// delay(1000);
//write the PWM value to the pwm output pin
pwmWriteHR(FanPin, PWMVal);
}
I am reading all five samples first. The code is summing each sample and after completion of the loop it divides by 5. I can probably loose the array but prefer to keep it in place to preserve each value for analysis. As far as the for next loop, I have already tried it and may go back to it. You can see it commented out above the while statement. I was having trouble with both For next and While Do until I commented out the all the Delay statements. For some reason delay was really messing every thing up.
Those prints are just there to see whats going on on the serial monitor. Do you think they could be messing things up? I can comment all of them out except for the last one at this point. If the average at the end changes significantly that should prove that the other print statements are effecting it.
Are you trying to PWM the A0 pin and then get the Arduino to read it back?
Do you know the PWM gives a signal of 5V followed by 0V, and the ratio of high to low averages out to be the number you set the PWM to be. Your meter could be averaging this for you.