Does the while command work?

unsigned long oldTime = 0;
const long interval = 100;

unsigned long  sensorPin = A7 ;
unsigned long  sensor = A6 ;
unsigned long  sensorC =20;

int sensorMin = 1023;  
int sensorMax = 0;     
int sensorValue = 0;


byte calb=4;
byte out=5; 


void setup()
{

pinMode( sensor , INPUT);   //  modlar :
pinMode(sensorPin, INPUT);  
pinMode  (out,OUTPUT);    
pinMode  (calb,OUTPUT); 

reads ();

}

 
   void loop()
{
  unsigned long currentMillis = millis();      
  if((currentMillis - oldTime) > interval)      
  { 
    oldTime = currentMillis;  }                  
     
       sensorC = analogRead(sensor);
     while (MinCAL>=MaxCAL)  { reads(); }
       digitalWrite  (out,1);  

      sensorValue = analogRead(sensorPin);

  sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

  sensorValue = constrain(sensorValue, 0, 255);
    
  if ( sensorValue>=250 )
  {digitalWrite(calb, 1);}
  else 
  {digitalWrite(calb, 0);
 delay (10);}   
  



}    } 


void calibrate() {

  digitalWrite(  out, 0);
  sensorValue = analogRead(sensorPin);
   if (sensorValue > sensorMax) {
   sensorMax = sensorValue;  }
  if (sensorValue < sensorMin) {
    sensorMin = sensorValue;

  }
}
  

Hello there

I want to do

simultaneous reading of two analog inputs
A6 AND A7
I determined
The value of A6 analog voltage is 0-1023 this
If it goes above 20 when reading 0-20
I want the A7 to work and keep reading voltage.

   A7 won't work if analog goes below 0 -20

             delays with militia voltage
             instead of delay

you can suggest different codes
Can you help me

What gets you out of this loop?

I think you should use an unsigned long long instead (64 bits) just in case the PIN number is really high… :upside_down_face: :cold_face:

What’s that?

A few small errors to work out:

Arduino: 1.8.13 (Mac OS X), Board: "Arduino Uno"

In function 'void setup()':22:3: error: 'reads' was not declared in this scope
   reads();
   ^~~~~

 In function 'void loop()':35:10: error: 'MinCAL' was not declared in this scope
   while (MinCAL >= MaxCAL)
          ^~~~~~

35:20: error: 'MaxCAL' was not declared in this scope
   while (MinCAL >= MaxCAL)
                    ^~~~~~

37:5: error: 'reads' was not declared in this scope
     reads();
     ^~~~~

At global scope:57:1: error: expected declaration before '}' token
 }
 ^
exit status 1
'reads' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

JML===
void setup()
It was wrong

void reads()

johnwaseer

atmega328 programming with arduino uno
I think my code doesn't work while it doesn't exit the endless loop. I guess. Could you open the code you wrote?

Your code does not even compile…

void calibrate()
i make a lot of mistakes

it
void reads ();

Why don’t you post the real code, the one that you uploaded?

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

unsigned long  sensorPin = A7 ;
unsigned long  sensor = A6 ;
unsigned long  sensorC =20;

int sensorMin = 1023;  
int sensorMax = 0;     
int sensorValue = 0;
unsigned int MinCAL = 10; 
unsigned int MaxCAL = 1022;

byte calb=4;
byte out=5; 


void setup()
{

pinMode( sensor , INPUT);   //  modlar :
pinMode(sensorPin, INPUT);  
pinMode  (out,OUTPUT);    
pinMode  (calb,OUTPUT); 

reads ();

}

 
   void loop()
{
  unsigned long currentMillis = millis();      
  if((currentMillis - oldTime) > interval)      
  { 
    oldTime = currentMillis;  }                  
     
       sensorC = analogRead(sensor);
     while (MinCAL>=MaxCAL)  { reads(); }
       digitalWrite  (out,1);  

      sensorValue = analogRead(sensorPin);

  sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

  sensorValue = constrain(sensorValue, 0, 255);
    
 { if ( sensorValue>=250 )
  {digitalWrite(calb, 1);}
  else 
  {digitalWrite(calb, 0);
 delay (10);}   
  



}    } 


void reads() {

  digitalWrite(  out, 0);
  sensorValue = analogRead(sensorPin);
   if (sensorValue > sensorMax) {
   sensorMax = sensorValue;  }
  if (sensorValue < sensorMin) {
    sensorMin = sensorValue;

  }
}
  

okey

I compiled it but this example goes into an infinite loop as soon as it opens

So same question as in#2: what will make MinCal < MaxCAL ?

johnwas

Can you simplify the code you wrote for us?

while (MinCAL >= MaxCAL) post processing or jumping with goto
do while (MinCAL >= MaxCAL)
if (MinCAL >= MaxCAL) post processing
for (MinCAL >= MaxCAL) post processing
Why is it not possible to activate A7 with commands like
Which start is appropriate?

thanks

A "while" does the associated test BEFORE doing the loop. A "for" does the associated test AFTER the loop has been executed.
Paul

The point is whatever condition is in the test of a while needs to become false at some point if you don’t want to be in an infinite loop

Nope.

Must be remembering another programming language. They get conflated after a few years!
Paul

I think @Paul_KD7HB meant

do {
// code;
} while();

1 Like

teacher when available

I want you to share a recommendation code

you are too busy on the site
you help everyone
I follow your posts

thank you
whether you write or not
Of course, I must also thank the site founder owners.

erewerw

The for loop is just a while loop in a posh frock.