#include byte x = 0x00; byte data[8]; //To use the I2C protocol: //connect SDA pin (data line) on pin 27 of the ATmega328P //connect SCL pin (clock line) on pin 28 of the ATmega328P #define EN A0 /* Enable pin for all stepper outputs - 8 on uno*/ #define R_DIR 5 /* Direction-Pin for right motor */ #define R_STEP 2 /* Step-Pin for right motor */ #define L_DIR 6 /* Direction-Pin for left motor*/ #define L_STEP 3 /* Step-Pin for left motor */ int command; float h,t; /* Rotational direction of stepper motors*/ boolean Direction_clockwise = LOW ; boolean Direction_counterclockwise = HIGH ; /* Low = Clockwise, High = Counterclockwise */ /* to lift the booth, right motor turns counterclockwise, left motor turns clockwise */ /* with following connections on driver: 1A blue, 1B red, 2A green, 2B black */ // // step // void step() { digitalWrite(R_STEP, HIGH); digitalWrite(L_STEP, HIGH); delay (10); digitalWrite(R_STEP, LOW); digitalWrite (L_STEP, LOW); delay (10); } // // waiting for commands to control the booth // void booth_control(int command) { switch (command){ case 1: // lift structure.*/ digitalWrite(EN, LOW); //Low to enable digitalWrite(R_DIR, Direction_counterclockwise); digitalWrite(L_DIR, Direction_clockwise); break; case 2: // lower structure. digitalWrite(EN, LOW); //Low to enable digitalWrite(R_DIR, Direction_clockwise); digitalWrite(L_DIR, Direction_counterclockwise); break; case 0: // stop the motors digitalWrite(EN, HIGH); //HIGH to disable digitalWrite(R_STEP, LOW); digitalWrite (L_STEP, LOW); break; } } // // reading temperature // void read_temp() { /* I2C Protocol adapted for CM2322 sensor CM2322 sensor I2C address is 0x5C , with R/W bit set to 0 (write): 0xB8 = 1011 1000 with R/W bit set to 1 (read): 0xB9 = 1011 1001 */ byte i, error; byte address =0x5C;// 0xB8 = 1011 1000 byte READ_Data = 0x03; byte READ_Start = 0x00; byte READ_Length = 0x04; /* Wakes the sensor up*/ Wire.beginTransmission(address); // LSB = 0 : write error = Wire.endTransmission(); /*Request data to sensor */ Wire.beginTransmission(address); // LSB = 0 : write Wire.write(READ_Data); // sends function code to read sensor data Wire.write(READ_Start); // sends starting register address Wire.write(READ_Length); // sends register length Wire.endTransmission(); // stop /* Read data from sensor */ i=0; Wire.requestFrom(0x5C, 8); // request 8 bytes from sensor while (Wire.available()) { // slave may send less than requested data[i] = Wire.read(); // receive a byte as part of a table i++; } /* compute humidity */ h = ((word)data[2]) << 8 | data[3]; h *= 0.1; /* compute temperature */ t = ((word)(data[4] & 0x7F)) << 8 | data[5]; t *= 0.1; if (data[4] & 0x80) t *= -1; //Serial.println(F("data")); //Serial.println(millis()); } void setup() { Wire.begin(); Serial.begin(115200); Serial.println("\nBooth control"); Serial.println("Enter 0 to stop the motors"); Serial.println("Enter 1 to lift the structure"); Serial.println("Enter 2 to lower the structure"); Serial.println("Enter 4 to read temperature"); /* Configure the stepper drive pins as outputs */ pinMode(EN, OUTPUT); pinMode(R_DIR, OUTPUT); pinMode(R_STEP, OUTPUT); pinMode(L_DIR, OUTPUT); pinMode(L_STEP, OUTPUT); // pinMode(switchPin, INPUT); digitalWrite(EN, HIGH); //HIGH to disable } void loop() { if(Serial.available()) { String booth_ctrl=Serial.readStringUntil('\n'); command=booth_ctrl.toInt(); if (command == 4) { read_temp(); Serial.print(F("Humidity: ")); Serial.print(h); Serial.print(F("% Temperature: ")); Serial.print(t); Serial.println(F("°C ")); } else { booth_control(command); Serial.print(F("command: ")); Serial.println(command,DEC); } } step(); }