Menu

Arduino doesn't recognize long integer datatype

2019-02-27
2019-02-28
  • Foad S. Farimani

    I have a simple model with some LEDs and potentiometers (in the attachment) and the code below:

    int potPin1 = A0;  
    int potPin2 = A1;
    
    int ledPin1 = 2;   
    int ledPin2 = 3;   
    int ledPin3 = 4;   
    
    long val1 = 0;   
    long val2 = 0; 
    int Tmax = 1000;   
    
    void setup() {
      pinMode(ledPin1, OUTPUT);  
      pinMode(ledPin2, OUTPUT);
      pinMode(ledPin3, OUTPUT);
      Serial.begin(9600);
    }
    
    void loop() {
      val1 = analogRead(potPin1) * Tmax / 1024;   
      val2 = analogRead(potPin2) * val1 / 1024; 
      Serial.print(val1);
      Serial.print(", ");
      Serial.println(val2);
      digitalWrite(ledPin1, HIGH);  
      delay(val2);                  
      digitalWrite(ledPin1, LOW);   
      delay(val1 - val2);                  
    }
    

    but I get negative values :

    It seems like a bug to me.

     
    • Foad S. Farimani

      For the moment I used a float variable as a workaround:

      int potPin1 = A0;  
      int potPin2 = A1;
      
      int ledPin1 = 2;   
      int ledPin2 = 3;   
      int ledPin3 = 4;   
      
      int val1 = 0; 
      int val1_ = 0;  
      int val2 = 0; 
      int val2_ = 0;
      float tmpf = 0.0;
      float Tmax = 1000;
      unsigned long T = millis();
      int dt = 0;   
      
      void setup() {
        pinMode(ledPin1, OUTPUT);  
        pinMode(ledPin2, OUTPUT);
        pinMode(ledPin3, OUTPUT);
        Serial.begin(9600);
      }
      
      void loop() {
        tmpf = analogRead(potPin1) * (Tmax / 1024.0);   
        val1 = (int) tmpf;
        tmpf = analogRead(potPin2) * (tmpf / 1024.0); 
        val2 = (int) tmpf;
      
        if (val1 != val1_ || val2 != val2_) {
          val1_ = val1;
          val2_ = val2;
          Serial.print(val1);
          Serial.print(", ");
          Serial.println(val2);
        }
      
        dt = millis() - T;
        if ((dt == 0 || val1 <= dt)) {
          T = millis();
          digitalWrite(ledPin1, HIGH);
        }    
        if (val2 <= dt) {
          digitalWrite(ledPin1, LOW);
        }                
      }
      
       
  • Santiago

    Santiago - 2019-02-28

    Hi.
    Think this is nothing about simulide, this is about using variable types in C/C++;

    In the first case you are using integer numbers calculations then store into a long, integers overflow, so you get negative numbers. Cast to long and it will work as espected.

     
    👍
    1
    • Foad S. Farimani

      aha! so basically the output of the analogRead is int anyways and the temporary memory used for arithmetics is the same as the operator pairs. if I do ((long) analogRead(...)) it should be solved. Will try and come back.

       
      • Foad S. Farimani

        this worked great. Thanks a lot.
        As a side feature request, the rotating wheel for potentiometer is a bit difficult to control. it would be great if one could change it to a slider and/or put the values manually (e.g. in the properties section).

         
        • Santiago

          Santiago - 2019-02-28

          You can put the value in potentiometer properties (the last property: "Value Ohm").

          And manually there is a trick to controll it with accuracy:
          Click on the pot wheel and hold it, then move mouse pointer outside of the pot as far as you want, the moving mouse pointer in a circle around the pot. The further you go from pot the more accurate you can change the value.

           
          👍
          1
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.