1 Reply Latest reply on Jul 19, 2016 3:03 PM by Intel Corporation

    Edison For Arduino w HX711 : wiring_digital-error: digitalRead

    TheCircleH

      so I have a script that runs perectly on the Arduino UNO.  When I went to run it on Edison Arduino, It compiled fine but at run time it throws

       

      wiring_digital-error: digitalRead: pin 3 out of range

       

      Arduino Code is Here

      #include<stdio.h>
      #include "HX711.h"
      #define calibration_factor 4492.0 
      #define DOUT 3
      #define CLK  2
      int WEIGHTCOUNT  = 1  ;  
      float TENWEIGHT[10]; 
      float TENWEIGHTAVERAGE = 0.00;
      float TENWEIGHTTOTAL = 0.0;
      float LASTWEIGHT = 2;
      HX711 scale(DOUT, CLK);
      
      
      void setup() {
       scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
       scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
      
      
        Serial.println("Weight Readings:");
       }
      
      
      void loop() {
      float b = scale.get_units() + 1; 
      if (b < 2 && b != 1) scale.tare(); 
      int c = b/LASTWEIGHT;
      if (b < 2 && c < .1) {
        Serial.println("Scale Is Not Loaded"); 
        delay(2000);
      }
      else
      {
      Serial.print("CALCULATING WEIGHT"); 
      int var = 0;
      while(var < 10){
        TENWEIGHT[var] = scale.get_units();
        delay(100) ;
        var++;
      }
      
        for(int i = 0 ; i < 10 ; i++){
        TENWEIGHTTOTAL = TENWEIGHTTOTAL+TENWEIGHT[i] ;
        }
      TENWEIGHTAVERAGE = TENWEIGHTTOTAL / 10 ; 
        
       // Serial.print("The Average Weight Is: ");
       // Serial.print(scale.get_units(), 1); //scale.get_units() returns a float
       float check = TENWEIGHTAVERAGE + 2 - LASTWEIGHT;
       Serial.println(TENWEIGHTAVERAGE);
       if (abs(check) < .5) {
         Serial.print(TENWEIGHTAVERAGE);
        Serial.print(" lbs"); //You can change this to kg but you'll need to refactor the calibration_factor
        Serial.println();
        Serial.println("PLEASE STEP OFF SCALE"); 
        delay(30000); 
       }
        LASTWEIGHT = TENWEIGHTAVERAGE + 2;
        TENWEIGHTTOTAL = 0;
        delay(1000); 
      }
      }
      

       

       

      AS there is no DigitalRead Called for in this , here is the hx711.h file

       

      #ifndef HX711_h
      #define HX711_h
      
      
      //#if ARDUINO >= 100
      #include "Arduino.h"
      //#else
      //#include "WProgram.h"
      //#endif
      
      
      class HX711
      {
        private:
        byte PD_SCK; // Power Down and Serial Clock Input Pin
        byte DOUT; // Serial Data Output Pin
        byte GAIN; // amplification factor
        long OFFSET; // used for tare weight
        float SCALE; // used to return weight in grams, kg, ounces, whatever
      
      
        public:
        // define clock and data pin, channel, and gain factor
        // channel selection is made by passing the appropriate gain: 128 or 64 for channel A, 32 for channel B
        // gain: 128 or 64 for channel A; channel B works with 32 gain factor only
        HX711(byte dout, byte pd_sck, byte gain = 128);
      
      
        virtual ~HX711();
      
      
        // check if HX711 is ready
        // from the datasheet: When output data is not ready for retrieval, digital output pin DOUT is high. Serial clock
        // input PD_SCK should be low. When DOUT goes to low, it indicates data is ready for retrieval.
        bool is_ready();
      
      
        // set the gain factor; takes effect only after a call to read()
        // channel A can be set for a 128 or 64 gain; channel B has a fixed 32 gain
        // depending on the parameter, the channel is also set to either A or B
        void set_gain(byte gain = 128);
      
      
        // waits for the chip to be ready and returns a reading
        long read();
      
      
        // returns an average reading; times = how many times to read
        long read_average(byte times = 10);
      
      
        // returns (read_average() - OFFSET), that is the current value without the tare weight; times = how many readings to do
        double get_value(byte times = 1);
      
      
        // returns get_value() divided by SCALE, that is the raw value divided by a value obtained via calibration
        // times = how many readings to do
        float get_units(byte times = 1);
      
      
        // set the OFFSET value for tare weight; times = how many times to read the tare value
        void tare(byte times = 10);
      
      
        // set the SCALE value; this value is used to convert the raw data to "human readable" data (measure units)
        void set_scale(float scale = 1.f);
      
      
        // get the current SCALE
        float get_scale();
      
      
        // set OFFSET, the value that's subtracted from the actual reading (tare weight)
        void set_offset(long offset = 0);
      
      
        // get the current OFFSET
        long get_offset();
      
      
        // puts the chip into power down mode
        void power_down();
      
      
        // wakes up the chip after power down mode
        void power_up();
      };
      
      
      #endif /* HX711_h */
      

       

      and here is the .cpp file

       

      #include <Arduino.h>
      #include <HX711.h>
      
      
      uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) {
        uint8_t value = 0;
        uint8_t i;
      
      
        for (i = 0; i < 8; ++i) {
          digitalWrite(clockPin, HIGH);
          if (bitOrder == LSBFIRST)
            value |= digitalRead(dataPin) << i;
          else
            value |= digitalRead(dataPin) << (7 - i);
          digitalWrite(clockPin, LOW);
        }
        return value;
      }
      
      
      
      
      HX711::HX711(byte dout, byte pd_sck, byte gain) {
        PD_SCK = pd_sck;
        DOUT = dout;
      
      
        pinMode(PD_SCK, OUTPUT);
        pinMode(DOUT, INPUT);
      
      
        set_gain(gain);
      }
      
      
      HX711::~HX711() {
      
      
      }
      
      
      bool HX711::is_ready() {
        return digitalRead(DOUT) == LOW;
      }
      
      
      void HX711::set_gain(byte gain) {
        switch (gain) {
        case 128: // channel A, gain factor 128
        GAIN = 1;
        break;
        case 64: // channel A, gain factor 64
        GAIN = 3;
        break;
        case 32: // channel B, gain factor 32
        GAIN = 2;
        break;
        }
      
      
        digitalWrite(PD_SCK, LOW);
        read();
      }
      
      
      long HX711::read() {
        // wait for the chip to become ready
        while (!is_ready());
      
      
          unsigned long value = 0;
          byte data[3] = { 0 };
          byte filler = 0x00;
      
      
        // pulse the clock pin 24 times to read the data
          data[2] = shiftIn(DOUT, PD_SCK, MSBFIRST);
          data[1] = shiftIn(DOUT, PD_SCK, MSBFIRST);
          data[0] = shiftIn(DOUT, PD_SCK, MSBFIRST);
      
      
        // set the channel and the gain factor for the next reading using the clock pin
        for (unsigned int i = 0; i < GAIN; i++) {
        digitalWrite(PD_SCK, HIGH);
        digitalWrite(PD_SCK, LOW);
        }
      
      
          // Datasheet indicates the value is returned as a two's complement value
          // Flip all the bits
          data[2] = ~data[2];
          data[1] = ~data[1];
          data[0] = ~data[0];
      
      
          // Replicate the most significant bit to pad out a 32-bit signed integer
          if ( data[2] & 0x80 ) {
              filler = 0xFF;
          } else if ((0x7F == data[2]) && (0xFF == data[1]) && (0xFF == data[0])) {
              filler = 0xFF;
          } else {
              filler = 0x00;
          }
      
      
          // Construct a 32-bit signed integer
          value = ( static_cast<unsigned long>(filler) << 24
                  | static_cast<unsigned long>(data[2]) << 16
                  | static_cast<unsigned long>(data[1]) << 8
                  | static_cast<unsigned long>(data[0]) );
      
      
          // ... and add 1
          return static_cast<long>(++value);
      }
      
      
      long HX711::read_average(byte times) {
        long sum = 0;
        for (byte i = 0; i < times; i++) {
        sum += read();
        }
        return sum / times;
      }
      
      
      double HX711::get_value(byte times) {
        return read_average(times) - OFFSET;
      }
      
      
      float HX711::get_units(byte times) {
        return get_value(times) / SCALE;
      }
      
      
      void HX711::tare(byte times) {
        double sum = read_average(times);
        set_offset(sum);
      }
      
      
      void HX711::set_scale(float scale) {
        SCALE = scale;
      }
      
      
      float HX711::get_scale() {
        return SCALE;
      }
      
      
      void HX711::set_offset(long offset) {
        OFFSET = offset;
      }
      
      
      long HX711::get_offset() {
        return OFFSET;
      }
      
      
      void HX711::power_down() {
        digitalWrite(PD_SCK, LOW);
        digitalWrite(PD_SCK, HIGH);
      }
      
      
      void HX711::power_up() {
        digitalWrite(PD_SCK, LOW);
      }