Rubik's cube solver robot using Lego - Page 1
Forum section: electronics / projects

author: Ecuber
author_rank: Newbie
timestamp: February 22, 2025, 08:23:24 pm
content: 1:I'm currently building a Rubik's cube solving robot out of Legos, with an Arduino uno to run the three motors and to interpret the notation I give it (I'm going to use a website online to calculate the solve).I'm using a servo, a DC motor and a step motor.Z_step-motor turns the base (or the whole cube) 90 deg at a time. I am only going to have Z turn one direction for simplicity in the code.R_DC-motor rotates the whole cube once, on the x axis.A_servo-motor lifts the arm to enable Z to rotate the entire cube on the Z axis, instead of just the lower slice (in the code 'Au' for arm up and 'Ad' for arm down).See cube_bot_rotations.png and Motor_placement_and_their_moving_parts.pngMy goal is to use only the Arduino uno kit I have for this robot.2:Rubik's cube notation is very common and the most used letters are U, D, R, L, F and B.To tun a side in reverse a ' is placed after the letter and if you want to rotate the side twice then you add '2' after the letter.So for this robot a simple U' motion requires the algorithm R R Z Z Z R RHere is a list of all the algorithms:U: R R Z R RU': R R Z Z Z R RD2: Z ZR: R R R Z RR': R Z Z Z R R RL2: R Z Z R R RF: Au Z Ad R R R Z R Au Z Z Z AdF': Au Z Ad R Z Z Z R R R Au Z Z Z AdB2: Au Z Ad R Z Z R R R Au Z Z Z AdL: R Z R R RL’: R Z Z Z R R RL2: R Z Z R R RF: Au Z Ad R R R Z R Au Z Z Z AdF’: Au Z Ad R R R Z Z Z R Au Z Z Z AdF2: Au Z Ad R R R Z Z R Au Z Z Z AdB: Au Z Ad R Z R R R Au Z Z Z AdB’: Au Z Ad R Z Z Z R R R Au Z Z Z AdB2: Au Z Ad R Z Z R R R Au Z Z Z Ad3:My first problem is the code and wiring for all three motors.This is a very rough draft code for test controlling the servo and step motor.Arduino uses the C++ coding language, which I am not familiar with.This is my code:Code:#include <Servo.h>#include <Stepper.h>Servo servo;int x_axis;int servo_val;// define number of steps per revolution#define STEPS 32// define stepper motor control pins#define IN1 11#define IN2 10#define IN3 9#define IN4 8// initialize stepper libraryStepper stepper(STEPS, IN4, IN2, IN3, IN1);// joystick output is connected to Arduino A1#define joystick A1void setup() {pinMode(A0, INPUT);servo.attach(13);}void loop() {x_axis = analogRead(A0);servo_val = map(x_axis, 0, 1023, 0, 180);servo.write(servo_val);// read analog value from the potentiometerint val = analogRead(joystick);// if the joystic is in the middle ===> stop the motorif ((val > 500) && (val < 523)) {digitalWrite(IN1, LOW);digitalWrite(IN2, LOW);digitalWrite(IN3, LOW);digitalWrite(IN4, LOW);}else {// move the motor in the first directionif (val >= 523) {// map the speed between 5 and 500 rpmint speed_ = map(val, 523, 1023, 5, 1000);// set motor speedstepper.setSpeed(speed_);// move the motor (1 step)stepper.step(1);val = analogRead(joystick);}// move the motor in the other directionif (val <= 500) {// map the speed between 5 and 500 rpmint speed_ = map(val, 500, 0, 5, 1000);// set motor speedstepper.setSpeed(speed_);// move the motor (1 step)stepper.step(-1);val = analogRead(joystick);}}}and here is the DC motor test code:Code:#define E1 5 // Enable Pin for motor#define I1 3 // Control pin 1 for motor#define I2 4 // Control pin 2 for motorvoid setup() {pinMode(E1, OUTPUT);pinMode(I1, OUTPUT);pinMode(I2, OUTPUT);}void loop() {analogWrite(E1, 255);digitalWrite(I1, HIGH);delay(3000);digitalWrite(E1, LOW);delay(2000);}Any input, Ideas, hints or help would be much appreciated, especially with the code.