Arduino codes.

The Smart cube project requires a complicated code which connects the MPU-6050 gyroscope with bluetooth and 5 objects: 2 home lights made from LED and RGB strip lights , 2 servo motors-door lock and curtain blinders and ventilaton- 12 Volts USB fan.

The code logic is: the gyroscope has different angles in each face of Smart Cube,  when turning on Z axis the other axis not changing , but Z axis, which means different axis in each point, but for user it is same logic- turn left or right on Z axis and change it’s settings- speed, direction, power. The logic of this is to change  change the supply voltage while rotating the cube.

  • The first idea for the code:

int led= ; //led at pin int ledValue; //value of led int ledFadeAmount = 5; //value when led is fading

void setup() {

pinMode(led, OUTPUT); //led specified as output Serial.begin(2400); //starting serial at 2400 baud BSens.begin(); //initialize the ADXL345 sensors_event_t event; //collect data from ADXL345 BSens.getEvent(&event); //read data from ADXL345 float pos = event.acceleration.x; //test purpose for determining position of x. Future use.

}

void ledFadeIn(){ //function of fade from 16 to 255

sensors_event_t event; BSens.getEvent(&event);

if (event.acceleration.z > -8.50 | event.acceleration.z < -10.00){ //if z position is greater than -8.50 or x is smaller than -10.00 ledValue = ledValue + ledFadeAmount; //set led value to the sum of led value and fade amount if(ledValue > 255) //if led value is greather than 255 ledValue = 255; //set led value to 255. Stops looping between led value 16 and 255. analogWrite(led, ledValue); //analog write of led value to led (pin 9) delay(30); //delay of 30 ms then start the ledfadein again }

}

void ledFadeOut(){ //function of fade from 255 to 16

sensors_event_t event; //collect data from ADXL345 BSens.getEvent(&event); //read data from ADXL345

if (event.acceleration.z < -8.50 | event.acceleration.z > -10.00){ //if z position is smaller than -8.50 or x is larger than -10.00 ledValue = ledValue – ledFadeAmount; //set led value to the sum of led value minus the fade amount if(ledValue < 16) //if led value is smaller than 255 ledValue = 16; //set led value to 16. Stops looping between led value 255 an analogWrite(led, ledValue); //analog write of led value to led (pin 9) delay(30); //delay of 30 ms then start the ledfadeout again }

}

void loop() {

sensors_event_t event; //collect data from ADXL345 BSens.getEvent(&event); //read data from ADXL345 float pos = event.acceleration.x; //test purpose for determining position of x. Future use. Serial.println(ledValue); //print value of led in the Serial Monitor

if (event.acceleration.z > -8.50 | event.acceleration.z < -10.00) //if z position is greater than -8.50 or x is smaller than -10.00 ledFadeIn(); //goto the ledFadeIn function else ledFadeOut(); //goto the ledFadeIn function

}

  • The final code has two variations:
  • Code made for DF Robot Bluno Beetle, which is very miniature arduino with easy to connect and fast Bluetooth 4.0
  • Code made to connect Arduino with HC-05 Bluetooth modules.

Here the codes shown:

  • Bluno send code with objects and gyroscope settings:

#include <Wire.h>

#include <MPU6050.h>

MPU6050 sensor(0x68) ;

int16_t motion_x, motion_y, motion_z , rotation_x, rotation_y, rotation_z ;

int16_t gx, gy, gz ;

int s=0;

byte flag_x1, flag_x2, flag_y1, flag_y2, flag_z1, flag_z2;

void setup ( )

{ Wire.begin ( );

Serial.begin (19200);

//Serial.println ( “Initializing the sensor” );

sensor.initialize ( );

//Serial.println (sensor.testConnection ( ) ? “Successfully Connected” : “Connection failed”);

//delay (1000);

//Serial.println ( “Taking Values from the sensor” );

//delay (1000); s=0; flag_x1=0; flag_x2=0; flag_y1=0; flag_y2=0; flag_z1=0; flag_z2=0; }

void loop ( )

{ sensor.getRotation (&rotation_x, &rotation_y, &rotation_z); sensor.getMotion6 (&motion_x, &motion_y, &motion_z, &gx, &gy, &gz); rotation_x = map (rotation_x, -16000, 16000, 0, 255) ; rotation_y = map (rotation_y, -16000, 16000, 0, 255) ; rotation_z = map (rotation_z, -16000, 16000, 0, 255) ;

motion_x = map (motion_x, -16000, 16000, 0, 255) ; motion_y = map (motion_y, -16000, 16000, 0, 255) ; motion_z = map (motion_z, -16000, 16000, 0, 255) ;

if ((motion_z>200)&&(motion_x>=120)&&(motion_y>=120)) // включается управление объектом №1 {

if (flag_z1 ==0) { Serial.println (1000); // Число объекта №1 flag_z1 =1; flag_z2=0; flag_x1=0; flag_x2=0; flag_y1=0; flag_y2=0; } if (rotation_z<110) { if (s<255) { s+=5; } else { s=255; }

} else if (rotation_z>150) { if (s>0) { s-=5; } else { s=0; }

} Serial.println (s); }

else if ((motion_z<50)&&(motion_x>=120)&&(motion_y>=120)) // // включается управление объектом №2 { if (flag_z2==0) { Serial.println (1001); // Число объекта №2 flag_z2=1; flag_z1=0; flag_x1=0; flag_x2=0; flag_y1=0; flag_y2=0; } if (rotation_z>150) { if (s<180) // разворот сервомотора { s+=5; } else { s=180; }

} else if (rotation_z<110) { if (s>0) { s-=5; } else { s=0; }

} Serial.println (s); }

if ((motion_y>200)&&(motion_x>=120)&&(motion_z>=120)) // включается управление объектом №3 { if (flag_y1 ==0) { Serial.println (1010); // Число объекта №3 flag_y1 =1; flag_y2=0; flag_x1=0; flag_x2=0; flag_z1=0; flag_z2=0; } if (rotation_y<110) { if (s<255) { s+=5; } else { s=255; }

} else if (rotation_y>150) { if (s>0) { s-=5; } else { s=0; }

} Serial.println (s); } else if ((motion_y<50)&&(motion_x>=120)&&(motion_z>=120)) // // включается управление объектом №4 { if (flag_y2==0) { Serial.println (1011); // Число объекта №4 flag_y2=1; flag_y1=0; flag_x1=0; flag_x2=0; flag_z1=0; flag_z2=0; } if (rotation_y>150) { if (s<255) //увеличение яркости { s+=5; } else { s=255; }

} else if (rotation_y<110) { if (s>0) { s-=5; } else { s=0; }

} Serial.println (s); } if ((motion_x>200)&&(motion_y>=120)&&(motion_z>=120)) // включается управление объектом №5 { // Свободная грань } else if ((motion_x<50)&&(motion_y>=120)&&(motion_z>=120)) // // включается управление объектом №6 { if (flag_x2 ==0) { Serial.println (1100); // Число объекта №1 flag_x2 =1; flag_x1=0; flag_y1=0; flag_y2=0; flag_z1=0; flag_z2=0; } if (rotation_x<110) { if (s<255) { s+=5; } else { s=255; }

} else if (rotation_x>150) { if (s>0) { s-=5; } else { s=0; }

} Serial.println (s); }

// az = map (az, -16000, 16000, 0, 255) ; /*if (s<255) { sensor.getRotation (&ax, &ay, &az); az = map (az, -16000, 16000, 0, 255) ; if (az>150) { s+=5; } } if (s>0) { sensor.getRotation (&ax, &ay, &az); az = map (az, -16000, 16000, 0, 255) ; if (az<80) { s-=5; } } Serial.println (s); */ /*Serial.print (ax); Serial.print (‘;’); Serial.print (ay); Serial.print (‘;’); Serial.print (az); Serial.println (‘;’);*/

//sensor.getMotion6 (&ax, &ay, &az, &gx, &gy, &gz); //ax = map (ax, -16000, 16000, 0, 255) ; //ay = map (ay, -16000, 16000, 0, 255) ; //az = map (az, -16000, 16000, 0, 255) ; //Serial.print (ax); //Serial.print (‘;’); //Serial.print (ay); //Serial.print (‘;’); //Serial.print (az); //Serial.println (‘;’);

//sg90.write (ax);

delay (1); }

  • Bluno receive code with commans to smart home objects:

//Bluno_Receiver_AT+SETTING=DEFCENTRAL or AT+SETTING=DEFPERIPHERAL #include <VarSpeedServo.h>

VarSpeedServo myservo1, myservo2; byte object;

void setup() { Serial.begin(19200); object=0; pinMode(3, OUTPUT); pinMode(5, OUTPUT); // pinMode(5, OUTPUT) myservo1.attach(2); myservo2.attach(4); myservo1.write(0,50); myservo2.write(0,50); }

void loop() { while (Serial.available() > 0) { int inCommand = Serial.parseInt(); switch (inCommand) { case 1000: object=1; break; case 1001: object=2; break; case 1010: object=3; break; case 1011: object=4; break; case 1100: object=5; break; case 1101: object=6; break; }

if (object==1) { // Serial.println(inCommand); //analogWrite(2,inCommand); myservo1.write(inCommand, 50);

} if (object==2) { // Serial.println(inCommand); analogWrite(3,inCommand); } if (object==3) { // Serial.println(inCommand); //analogWrite(4,inCommand); myservo2.write(inCommand, 50); } if (object==4) { // Serial.println(inCommand); analogWrite(5,inCommand); } if (object==5) { // Serial.println(inCommand); analogWrite(A0,inCommand); }

} }

  • The Bluno receive module has been broken and the problem was to make another connection with bluetooth or other tool of connection: The radio module has been tried to use , but it doesn’t work on streaming mode and HC-05 bluetooth modules has been connected with Arduino Nano and UNO.
  • Here the final code of the Smart Cube device:

#include <Wire.h> #include <SoftwareSerial.h> #include <MPU6050.h>

MPU6050 sensor(0x68) ;

int16_t motion_x, motion_y, motion_z , rotation_x, rotation_y, rotation_z ;

int16_t gx, gy, gz ;

int s=0;

byte flag_x1, flag_x2, flag_y1, flag_y2, flag_z1, flag_z2;

SoftwareSerial mySerial(2, 3); // RX, TX void setup ( )

{ Wire.begin ( );

Serial.begin (115200); mySerial.begin(38400); //Serial.println ( “Initializing the sensor” );

sensor.initialize ( );

//Serial.println (sensor.testConnection ( ) ? “Successfully Connected” : “Connection failed”);

//delay (1000);

//Serial.println ( “Taking Values from the sensor” );

//delay (1000); s=0; flag_x1=0; flag_x2=0; flag_y1=0; flag_y2=0; flag_z1=0; flag_z2=0;

}

void loop ( )

{ sensor.getRotation (&rotation_x, &rotation_y, &rotation_z); sensor.getMotion6 (&motion_x, &motion_y, &motion_z, &gx, &gy, &gz); rotation_x = map (rotation_x, -16000, 16000, 0, 255) ; rotation_y = map (rotation_y, -16000, 16000, 0, 255) ; rotation_z = map (rotation_z, -16000, 16000, 0, 255) ;

motion_x = map (motion_x, -16000, 16000, 0, 255) ; motion_y = map (motion_y, -16000, 16000, 0, 255) ; motion_z = map (motion_z, -16000, 16000, 0, 255) ;

if ((motion_z>200)&&(motion_x>=120)&&(motion_y>=120)) // включается управление объектом №1 {

if (flag_z1 ==0) { mySerial.println (1000); // Число объекта №1 Serial.println (1000); flag_z1 =1; flag_z2=0; flag_x1=0; flag_x2=0; flag_y1=0; flag_y2=0; } if (rotation_z<110) { if (s<255) { s+=5; } else { s=255; }

} else if (rotation_z>150) { if (s>0) { s-=5; } else { s=0; }

} mySerial.println (s); }

else if ((motion_z<50)&&(motion_x>=120)&&(motion_y>=120)) // // включается управление объектом №2 { if (flag_z2==0) { mySerial.println (1001); // Число объекта №2 flag_z2=1; flag_z1=0; flag_x1=0; flag_x2=0; flag_y1=0; flag_y2=0; } if (rotation_z>150) { if (s<180) // разворот сервомотора { s+=5; } else { s=180; }

} else if (rotation_z<110) { if (s>0) { s-=5; } else { s=0; }

} mySerial.println (s); }

if ((motion_y>200)&&(motion_x>=120)&&(motion_z>=120)) // включается управление объектом №3 { if (flag_y1 ==0) { mySerial.println (1010); // Число объекта №3 flag_y1 =1; flag_y2=0; flag_x1=0; flag_x2=0; flag_z1=0; flag_z2=0; } if (rotation_y<110) { if (s<255) { s+=5; } else { s=255; }

} else if (rotation_y>150) { if (s>0) { s-=5; } else { s=0; }

} mySerial.println (s); } else if ((motion_y<50)&&(motion_x>=120)&&(motion_z>=120)) // // включается управление объектом №4 { if (flag_y2==0) { mySerial.println (1011); // Число объекта №4 flag_y2=1; flag_y1=0; flag_x1=0; flag_x2=0; flag_z1=0; flag_z2=0; } if (rotation_y>150) { if (s<255) //увеличение яркости { s+=5; } else { s=255; }

} else if (rotation_y<110) { if (s>0) { s-=5; } else { s=0; }

} mySerial.println (s); } if ((motion_x>200)&&(motion_y>=120)&&(motion_z>=120)) // включается управление объектом №5 { // Свободная грань } else if ((motion_x<50)&&(motion_y>=120)&&(motion_z>=120)) // // включается управление объектом №6 { if (flag_x2 ==0) { mySerial.println (1100); // Число объекта №1 flag_x2 =1; flag_x1=0; flag_y1=0; flag_y2=0; flag_z1=0; flag_z2=0; } if (rotation_x<110) { if (s<255) { s+=5; } else { s=255; }

} else if (rotation_x>150) { if (s>0) { s-=5; } else { s=0; }

} mySerial.println (s); } Serial.println (s); // az = map (az, -16000, 16000, 0, 255) ; /*if (s<255) { sensor.getRotation (&ax, &ay, &az); az = map (az, -16000, 16000, 0, 255) ; if (az>150) { s+=5; } } if (s>0) { sensor.getRotation (&ax, &ay, &az); az = map (az, -16000, 16000, 0, 255) ; if (az<80) { s-=5; } } Serial.println (s); */ /*Serial.print (ax); Serial.print (‘;’); Serial.print (ay); Serial.print (‘;’); Serial.print (az); Serial.println (‘;’);*/

//sensor.getMotion6 (&ax, &ay, &az, &gx, &gy, &gz); //ax = map (ax, -16000, 16000, 0, 255) ; //ay = map (ay, -16000, 16000, 0, 255) ; //az = map (az, -16000, 16000, 0, 255) ; //Serial.print (ax); //Serial.print (‘;’); //Serial.print (ay); //Serial.print (‘;’); //Serial.print (az); //Serial.println (‘;’);

//sg90.write (ax);

delay (1); }

  • The code for receive device:

#include <VarSpeedServo.h> #include <SoftwareSerial.h> VarSpeedServo myservo1, myservo2; SoftwareSerial mySerial(10, 11); // RX, TX byte object;

void setup() { Serial.begin(115200); mySerial.begin(38400); object=0; pinMode(3, OUTPUT); pinMode(5, OUTPUT); pinMode(9, OUTPUT); // pinMode(5, OUTPUT) myservo1.attach(2); myservo2.attach(4); myservo1.write(0,50); myservo2.write(0,50);

}

void loop() { while (mySerial.available()) { int inCommand = mySerial.parseInt(); Serial.println(inCommand); switch (inCommand) { case 1000: object=1; break; case 1001: object=2; break; case 1010: object=3; break; case 1011: object=4; break; case 1100: object=5; break; case 1101: object=6; break; }

if (object==1) { // Serial.println(inCommand); //analogWrite(2,inCommand); myservo1.write(inCommand, 50);

} if (object==2) { // Serial.println(inCommand); analogWrite(3,inCommand); } if (object==3) { // Serial.println(inCommand); //analogWrite(4,inCommand); myservo2.write(inCommand, 50); } if (object==4) { // Serial.println(inCommand); analogWrite(5,inCommand); } if (object==5) { // Serial.println(inCommand); analogWrite(9,inCommand); }

} }

Tags: ,

Trackback from your site.

address: 20 Myasnitskaya ulitsa
(metro stations ‘Lubyanka’ and ‘Kitay-Gorod’)
Moscow 101000 Russia

phone: +7(495)772-95-90 *15026

email: city@hse.ru