#include #define SLAVE_ADDR_8574_A 0x3e #define SLAVE_ADDR_8574_B 0x3f #include "Arduino.h" ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Code issu de la librairie HX711 pour Arduino de https://github.com/bogde/HX711.git class HX711 { private: byte PD_SCK; // Power Down and Serial Clock Input Pin byte DOUT; // Serial Data Output Pin byte GAIN; // amplification factor 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){ begin(dout, pd_sck, gain); } HX711() { }; virtual ~HX711() {} // Allows to set the pins and gain later than in the constructor ////////////////////////////// void begin(byte dout, byte pd_sck, byte gain = 128){ PD_SCK = pd_sck; DOUT = dout; pinMode(PD_SCK, OUTPUT); pinMode(DOUT, INPUT); set_gain(gain); } ////////////////////////////// // 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() { return digitalRead(DOUT) == LOW; } // 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){ switch (gain) { case 128: GAIN = 1; break; // channel A, gain factor 128 case 64: GAIN = 3; break; // channel A, gain factor 64 case 32: GAIN = 2; break; // channel B, gain factor 32 } digitalWrite(PD_SCK, LOW); read(); } ////////////////////////////// // waits for the chip to be ready and returns a reading long read() { // wait for the chip to become ready while (!is_ready()) { // Will do nothing on Arduino but prevent resets of ESP8266 (Watchdog Issue) yield(); } long value = 0; uint8_t data[4] = { 0,0,0,0 }; // pulse the clock pin 24 times to read the data // https://www.arduino.cc/reference/en/language/functions/advanced-io/shiftin/ 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); } // Replicate the most significant bit to pad out a 32-bit signed integer if ( (data[2] & 0x80) !=0) { data[3] = 0xFF; } else { data[3] = 0x00; } // Construct a 32-bit signed integer value = ( static_cast(data[3]) << 24 | static_cast(data[2]) << 16 | static_cast(data[1]) << 8 | static_cast(data[0]) ); return value; } }; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// HX711 scale(A1, A0); // DOUT, SCK #include #include ////////////////////////////////////////// bool detecteFrontMontantPortB(unsigned char numerobit) { static char ancien[8]={-1,-1,-1,-1,-1,-1,-1,-1}; bool ret=false; unsigned char pb=LirePortB(); if (ancien[numerobit]==-1){ for (int i=0;i<8;i++) ancien[i]=(pb>>i)&1; }else{ char actuel=(pb>>numerobit)&1; if ( (actuel==1) && (ancien[numerobit]==0)) ret=true; else ret=false; ancien[numerobit]=actuel; } return ret; } ////////////////////////////////////////// unsigned char LirePortB() { Wire.requestFrom((byte)SLAVE_ADDR_8574_B, (byte)1);// demande la lecture d'1 octet depuis l'adresse du pérpiphérique if (Wire.available()==1) //si l'octet est disponible return Wire.read(); // lire l'octet else return 0; } ////////////////////////////////////////// void EcrirePortA(unsigned char valeur) //cette fonction pilote les 8 leds avec la valeur 8bits fournie dans le paramètre valeur { Wire.beginTransmission((byte)SLAVE_ADDR_8574_A);//démarre la transmission avec l'adresse du pérpiphérique Wire.write(~(byte)valeur); //envoie la donnée complémentée car les LEDs s'allument à l'état 0 Wire.endTransmission(); } ////////////////////////////////////////// void setup() { Serial.begin(9600); Serial.println("reset"); //scale.set_scale(250.f); //// this value is obtained by calibrating the scale with known weights //scale.tare(); Wire.begin(); // joindre le bus i2c en tant que maître delay(100); Wire.beginTransmission((byte)SLAVE_ADDR_8574_B); Wire.write((byte)0xff); Wire.endTransmission();//configure le composant B en entrée pinMode(3,OUTPUT); } ////////////////////////////////////////// void loop() { }