void setup() {
Serial.begin(115200);
}
void loop() {
int val;
val = analogRead(A0);
Serial.println(val);
delay(100);
}
is it possible to change this code where you see the lowest and highest values?
thanks in advandce
void setup() {
Serial.begin(115200);
}
void loop() {
int val;
val = analogRead(A0);
Serial.println(val);
delay(100);
}
is it possible to change this code where you see the lowest and highest values?
thanks in advandce
Yes
Add two variables, e.g. maxVal and minVal; they either need to be global (declared before setup()) or static in loop().
After reading the analog input as you do now, compare val with those two variables and update them when needed.
im new with this so , im still learning ![]()
can you show the code
int minVal = 1023;
int maxVal = 0;
void setup()
{
...
...
}
void loop()
{
int val;
val = analogRead(A0);
// update minVal if val is lower than current minVal
if(val < minVal)
{
minVal = val;
}
}
You can puzzle on maxVal.
Hello ronnys
Welcome to the world's best Arduino forum ever.
Consider:
enum analogValues {Current, Min, Max};
uint16_t analogValue[] {0, 1023, 0};
constexpr uint8_t AnalogPin {A0};
void setup()
{
Serial.begin(115200);
Serial.println("get ready");
}
void loop()
{
analogValue[Current] = analogRead(AnalogPin);
if (analogValue[Current] < analogValue[Min]) analogValue[Min] = analogValue[Current], Serial.print("value min = "), Serial.println(analogValue[Min]);
if (analogValue[Current] > analogValue[Max]) analogValue[Max] = analogValue[Current], Serial.print("value max = "), Serial.println(analogValue[Max]);
}
Have a nice day and enjoy coding in C++.
Code seems to work very well. Any chance you can elaborate for those of us who are new? What's the purpose of enum, constexpr, and the square brackets in the loop?
Hello nuk3man
These are instructions from the C++ toolbox.
Take a look at them to acquire the knowledge:
const byte moistureSensorPowerPin = 19;
enum analogValues {Current, Min, Max};
uint16_t analogValue[] {0, 1023, 0};
constexpr uint8_t AnalogPin {A5};
void setup()
{
Serial.begin(115200);
Serial.println("get ready");
pinMode(moistureSensorPowerPin, OUTPUT);
digitalWrite(moistureSensorPowerPin, HIGH);
}
void loop()
{
analogValue[Current] = analogRead(AnalogPin);
if (analogValue[Current] < analogValue[Min]) analogValue[Min] = analogValue[Current], Serial.print("value min = "), Serial.println(analogValue[Min]);
if (analogValue[Current] > analogValue[Max]) analogValue[Max] = analogValue[Current], Serial.print("value max = "), Serial.println(analogValue[Max]);
}
thanks for te Code , this works for me
void setup() {
Serial.begin(9600); // start serial comms
}
void loop() {
findhighlow();
}
void findhighlow() {
// find highest and lowest and when they occurred in the count
// only Serial.print()s when LOWEST or HIGHEST changes
unsigned long reading = 0; // new reading
unsigned long highest = 0; // highest value (set to lowest possible value)
unsigned long lowest = 1023;// lowest value (set highest possible value)
unsigned long count = 0; // running count for when min/max occurrs
unsigned long readCountHighest = 0; // the reading count when the highest value occurred
unsigned long readCountLowest = 0; // the reading count when the lowest value occurred
boolean newCurrentHighLow = 0; // signals a new high or low
// randomSeed(analogRead(A0));
while (1) {
reading = analogRead(A0); // random reading
count++; // store loop count
if (reading > highest)
{
highest = reading;
newCurrentHighLow = true;
readCountHighest = count;
}
if (reading < lowest)
{
lowest = reading;
newCurrentHighLow = true;
readCountLowest = count;
}
if (newCurrentHighLow) // only print when a new low or high ocurrs
{
newCurrentHighLow = false;
Serial.print("(NEW value ");
Serial.print(reading);
Serial.print(" at count ");
Serial.print(count);
Serial.print(")");
Serial.print(" (LOWEST value ");
Serial.print(lowest);
Serial.print(" at count ");
Serial.print(readCountLowest);
Serial.print(")");
Serial.print(" (HIGHEST value ");
Serial.print(highest);
Serial.print(" at count ");
Serial.print(readCountHighest);
Serial.println(")");
}
}
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.