Millis() not working problem

:
#include<Wire.h>                   //Include kutuphane ı2 data Wire
#define MCP4725 0X60
unsigned long  adc;
unsigned long  buffer[3];  
unsigned int rdtx=0;
unsigned int rdtmax=20;
unsigned long karsilastir=0;
unsigned int karsmax=260;

unsigned long  zaman1=0,zaman2=0,zaman3=0,zaman4=0,zaman5=0;

byte trp=2; //  tr out:
byte trn=3; //   tr cıkış :
byte kazancled=4; //  led  output:
byte npoutled=5; //   led cıkısı:
byte pwrled=6; //   led  cıkış :
byte incompy = 7; // buton opamp 1-0 gırış:
byte outcompv=8; // ao-a1 karsılastırma cıkış :
byte incompx = 9; // 8 out koprusu 1-0 gırış:
byte setswic=10; //   led cıkısı:

void setup()
         {
pinMode  ( trp, OUTPUT );  // s :
pinMode  ( trn , OUTPUT ); //  s :
pinMode  ( kazancled, OUTPUT );  // s :
pinMode  (npoutled,OUTPUT);  //   sinyal cıkış NPN ve PNP out  :
pinMode  (pwrled,OUTPUT);  //  power ve rdt flaşör   :
pinMode(incompy, INPUT);   //  modlar :
pinMode  ( outcompv , OUTPUT ); //  s :
pinMode(incompx, INPUT);   //  modlar :
pinMode  ( setswic, OUTPUT );  // s :
Wire.begin(0x60);
Wire.setClock (400000);
}
void loop()                    
           
               {   if ( millis () - zaman1 >= 100)  
          {
   karsilastir  = analogRead(A6);
   if (karsilastir  <= karsmax )
    {digitalWrite(setswic, !digitalRead(setswic)); }
         zaman1 = millis ();                                
             
          {
  { if (millis () -zaman2 >=100)
   adc = analogRead(A7);
   adc = map(adc, 0 ,1023 ,0,4095 )  ;
  buffer[0] = 0b01000000;          
  buffer[1] = adc >> 4;            
  buffer[2] = adc << 4;            
  Wire.beginTransmission (0x60);      
  Wire.write(buffer[0]);          
  Wire.write(buffer[1]);          
  Wire.write(buffer[2]);          
  Wire.endTransmission();
    zaman2=millis();
         
       

  {
     rdtx = analogRead(A2);
    if (rdtx <= rdtmax )
          {  if (millis()-zaman3 >= 100)  
    {digitalWrite(pwrled, !digitalRead( pwrled)); }
     else
     {digitalWrite(pwrled, !digitalRead( pwrled)); }
     zaman3=millis();
               
                    {

     unsigned long compr1 ,compr2 ;
   compr1 = analogRead ( A0) ;
   compr2 = analogRead ( A1) ;
   if (compr1 <= compr2 )
      {  if (millis()-zaman4 >= 100)
            {digitalWrite(outcompv, !digitalRead(outcompv )); }
        zaman4=millis();
       
         
 
{
bool incompx = digitalRead(7);
  bool incompy = digitalRead(9);
  if (incompx == 1 &&  incompy == 1)

        {  if (millis()-zaman5 >= 100)
    {digitalWrite(trp, !digitalRead(trp )); }
    {digitalWrite(trn, !digitalRead(trn )); }
    {digitalWrite(npoutled, !digitalRead( npoutled )); }
     zaman5=millis();
         
         
        }}}}}}}}
     }
  }
type or paste code here

millis() not working where did i make the error
is compiling

          It doesn't work when I install it on the system

Have you printed the value returned to see if it changes? I can assure you it is working.
Paul

I have a mistake, can you review the codes?
do you mean values
here

sample kode here
unsigned long zaman1=0,zaman2=0,zaman3=0,zaman4=0,zaman5=0;

if ( millis () - zaman1 >= 100)
//
//
//

Explain what you expected to happen, and what happened instead.

Did I mistype my millis codes?

{ } are the pretty parentheses wrong?

or

Are the positions of the codes written wrong?

I uploaded after compiling the codes
not working

See reply #5.

You do seems to have a few { } pairs that do not really serve any purpose (not connected to any type of conditional statement).
There are also a few places where you check if millis() - zamanX >= 100 then set the value of zamanX regardless of the outcome of the if statement.

Your timers are nested very deeply. Did you intend for each timer to only check when all the previous timers were done?

void loop()
{
  if ( millis () - zaman1 >= 100)
  {
    karsilastir  = analogRead(A6);
    if (karsilastir  <= karsmax )
    {
      digitalWrite(setswic, !digitalRead(setswic));
    }
    zaman1 = millis ();

    {
      {
        if (millis () - zaman2 >= 100)
          adc = analogRead(A7);
        adc = map(adc, 0 , 1023 , 0, 4095 )  ;
        buffer[0] = 0b01000000;
        buffer[1] = adc >> 4;
        buffer[2] = adc << 4;
        Wire.beginTransmission (0x60);
        Wire.write(buffer[0]);
        Wire.write(buffer[1]);
        Wire.write(buffer[2]);
        Wire.endTransmission();
        zaman2 = millis();
        {
          rdtx = analogRead(A2);
          if (rdtx <= rdtmax )
          {
            if (millis() - zaman3 >= 100)
            {
              digitalWrite(pwrled, !digitalRead( pwrled));
            }
            else
            {
              digitalWrite(pwrled, !digitalRead( pwrled));
            }
            zaman3 = millis();

            {

              unsigned long compr1 , compr2 ;
              compr1 = analogRead ( A0) ;
              compr2 = analogRead ( A1) ;
              if (compr1 <= compr2 )
              {
                if (millis() - zaman4 >= 100)
                {
                  digitalWrite(outcompv, !digitalRead(outcompv ));
                }
                zaman4 = millis();



                {
                  bool incompx = digitalRead(7);
                  bool incompy = digitalRead(9);
                  if (incompx == 1 &&  incompy == 1)

                  {
                    if (millis() - zaman5 >= 100)
                    {
                      digitalWrite(trp, !digitalRead(trp ));
                    }
                    {
                      digitalWrite(trn, !digitalRead(trn ));
                    }
                    {
                      digitalWrite(npoutled, !digitalRead( npoutled ));
                    }
                    zaman5 = millis();


                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

It would be more common to have each timer separate:

void loop()
{
  if ( millis () - zaman1 >= 100)
  {
    karsilastir  = analogRead(A6);
    if (karsilastir  <= karsmax )
    {
      digitalWrite(setswic, !digitalRead(setswic));
    }
    zaman1 = millis ();
  }

  if (millis () - zaman2 >= 100)
  {
    adc = analogRead(A7);
    adc = map(adc, 0 , 1023 , 0, 4095 )  ;
    buffer[0] = 0b01000000;
    buffer[1] = adc >> 4;
    buffer[2] = adc << 4;
    Wire.beginTransmission (0x60);
    Wire.write(buffer[0]);
    Wire.write(buffer[1]);
    Wire.write(buffer[2]);
    Wire.endTransmission();
    zaman2 = millis();
  }

  rdtx = analogRead(A2);
  if (rdtx <= rdtmax )
  {
    if (millis() - zaman3 >= 100)
    {
      zaman3 = millis();
      digitalWrite(pwrled, !digitalRead( pwrled));
    }
    else
    {
      digitalWrite(pwrled, !digitalRead( pwrled));
    }
  }

  unsigned long compr1 , compr2 ;
  compr1 = analogRead ( A0) ;
  compr2 = analogRead ( A1) ;
  if (compr1 <= compr2 )
  {
    if (millis() - zaman4 >= 100)
    {
      digitalWrite(outcompv, !digitalRead(outcompv ));
      zaman4 = millis();
    }
  }

  bool incompx = digitalRead(7);
  bool incompy = digitalRead(9);
  if (incompx == 1 &&  incompy == 1)
  {
    if (millis() - zaman5 >= 100)
    {
      digitalWrite(trp, !digitalRead(trp ));
      digitalWrite(trn, !digitalRead(trn ));
      digitalWrite(npoutled, !digitalRead( npoutled ));
      zaman5 = millis();
    }
  }
}
1 Like

I compiled the codes and expected it to work normally.
I didn't want to use delay in all sections just to control with militia
I didn't have a purpose
{ } curly braces
I didn't think it would go this deep

thank you so much john wasser

how can i adapt this code

the code is here

how can i adapt this code

the code is here

MCP.writeDAC(819*5)

code here
  adc = analogRead(A7);
    adc = map(adc, 0 , 1023 , 0, 4095 )  ;
    buffer[0] = 0b01000000;
    buffer[1] = adc >> 4;
    buffer[2] = adc << 4;
    Wire.beginTransmission (0x60);
    Wire.write(buffer[0]);
    Wire.write(buffer[1]);
    Wire.write(buffer[2]);
    Wire.endTransmission();



55 / 5000

Çeviri sonuçları

original codes are delayed and working here`

#include<Wire.h>                   //Include kutuphane ı2 data Wire
#define MCP4725 0X60
unsigned long  adc;
unsigned long  buffer[3];  
unsigned int rdtx=0;
unsigned int rdtmax=20;
unsigned long karsilastir=0;
unsigned int karsmax=260;
byte trp=2; //  tr out:
byte trn=3; //   tr cıkış :
byte kazancled=4; //  led  output:
byte npoutled=5; //   led cıkısı:
byte pwrled=6; //   led  cıkış :
byte incompy = 7; // buton opamp 1-0 gırış:
byte outcompv=8; // ao-a1 karsılastırma cıkış :
byte incompx = 9; // 8 out koprusu 1-0 gırış:
byte setswic=10; //   led cıkısı:

void setup()
         {
pinMode  ( trp, OUTPUT );  // s :
pinMode  ( trn , OUTPUT ); //  s :
pinMode  ( kazancled, OUTPUT );  // s :
pinMode  (npoutled,OUTPUT);  //   sinyal cıkış NPN ve PNP out  :
pinMode  (pwrled,OUTPUT);  //  power ve rdt flaşör   :
pinMode(incompy, INPUT);   //  modlar :
pinMode  ( outcompv , OUTPUT ); //  s :
pinMode(incompx, INPUT);   //  modlar :
pinMode  ( setswic, OUTPUT );  // s :
Wire.begin(0x60);
Wire.setClock (400000);
}
void loop()                    
              {
  karsilastir  = analogRead(A6);   if (karsilastir  <= karsmax )
   {digitalWrite(setswic, 1);   delay (20);}
   else
   {digitalWrite(setswic, 0); delay (22); }                        
             {                      
   adc = analogRead(A7);
   adc = map(adc, 0 ,1023 ,0,4095 )  ;
  buffer[0] = 0b01000000;          
  buffer[1] = adc >> 4;            
  buffer[2] = adc << 4;            
  Wire.beginTransmission (0x60);      
  Wire.write(buffer[0]);          
  Wire.write(buffer[1]);          
  Wire.write(buffer[2]);          
  Wire.endTransmission();
    {delay (20);}
            {
     rdtx = analogRead(A2);
    if (rdtx <= rdtmax )
     {digitalWrite(pwrled, 1);  delay (20);}
     else
    {digitalWrite(pwrled, 0); delay (50);}
     {digitalWrite(pwrled, 1); delay (60);}
                {
   unsigned long compr1 ,compr2 ;
   compr1 = analogRead ( A0) ;
   compr2 = analogRead ( A1) ;
   if (compr1 <= compr2 )
      {digitalWrite (outcompv ,1);  delay(10);}
      else
      {digitalWrite (outcompv ,0);  delay (10);}
       }
    {
bool incompx = digitalRead(7);
  bool incompy = digitalRead(9);
  if (incompx == 1 &&  incompy == 1)
  {digitalWrite(trp, 1);
  digitalWrite(trn, 1);
  digitalWrite(npoutled, 1);   delay (150); }
  else
     {digitalWrite(trp, 0);
     digitalWrite(trn, 0);
     digitalWrite(npoutled, 0);     delay (40); }      
           }
        }
     }
  }

I want to do all the delays with millis

Code compiled with millis works unstable here
There are flashing LEDs can you help me johnwasser

#include<Wire.h>                   //Include kutuphane ı2 data Wire
#define MCP4725 0X60
unsigned long  adc;
unsigned long  buffer[3];  
unsigned int rdtx=0;
unsigned int rdtmax=20;
unsigned long karsilastir=0;
unsigned int karsmax=260;
byte trp=2; //  tr out:
byte trn=3; //   tr cıkış :
byte kazancled=4; //  led  output:
byte npoutled=5; //   led cıkısı:
byte pwrled=6; //   led  cıkış :
byte incompy = 7; // buton opamp 1-0 gırış:
byte outcompv=8; // ao-a1 karsılastırma cıkış :
byte incompx = 9; // 8 out koprusu 1-0 gırış:
byte setswic=10; //   led cıkısı:

void setup()
         {
pinMode  ( trp, OUTPUT );  // s :
pinMode  ( trn , OUTPUT ); //  s :
pinMode  ( kazancled, OUTPUT );  // s :
pinMode  (npoutled,OUTPUT);  //   sinyal cıkış NPN ve PNP out  :
pinMode  (pwrled,OUTPUT);  //  power ve rdt flaşör   :
pinMode(incompy, INPUT);   //  modlar :
pinMode  ( outcompv , OUTPUT ); //  s :
pinMode(incompx, INPUT);   //  modlar :
pinMode  ( setswic, OUTPUT );  // s :
Wire.begin(0x60);
Wire.setClock (400000);
}
void loop()
{
  if ( millis () - zaman1 >= 100)
  {
    karsilastir  = analogRead(A6);
    if (karsilastir  <= karsmax )
    {
      digitalWrite(setswic, !digitalRead(setswic));
    }
    zaman1 = millis ();
  }

  if (millis () - zaman2 >= 100)
  {
    adc = analogRead(A7);
    adc = map(adc, 0 , 1023 , 0, 4095 )  ;
    buffer[0] = 0b01000000;
    buffer[1] = adc >> 4;
    buffer[2] = adc << 4;
    Wire.beginTransmission (0x60);
    Wire.write(buffer[0]);
    Wire.write(buffer[1]);
    Wire.write(buffer[2]);
    Wire.endTransmission();
    zaman2 = millis();
  }

  rdtx = analogRead(A2);
  if (rdtx <= rdtmax )
  {
    if (millis() - zaman3 >= 100)
    {
      zaman3 = millis();
      digitalWrite(pwrled, !digitalRead( pwrled));
    }
    else
    {
      digitalWrite(pwrled, !digitalRead( pwrled));
    }
  }

  unsigned long compr1 , compr2 ;
  compr1 = analogRead ( A0) ;
  compr2 = analogRead ( A1) ;
  if (compr1 <= compr2 )
  {
    if (millis() - zaman4 >= 100)
    {
      digitalWrite(outcompv, !digitalRead(outcompv ));
      zaman4 = millis();
    }
  }

  bool incompx = digitalRead(7);
  bool incompy = digitalRead(9);
  if (incompx == 1 &&  incompy == 1)
  {
    if (millis() - zaman5 >= 100)
    {
      digitalWrite(trp, !digitalRead(trp ));
      digitalWrite(trn, !digitalRead(trn ));
      digitalWrite(npoutled, !digitalRead( npoutled ));
      zaman5 = millis();
    }
  }
}



:evergreen_tree: :evergreen_tree:
:face_with_monocle: