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 (mod edit link removed).
I'm going to use this forum somewhat as a blog, however I put it under this section because this is the first time I've used Arduino and I already need help.
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).
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.
As you can probably tell, this code does not quite work properly.
#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 library
Stepper stepper(STEPS, IN4, IN2, IN3, IN1);
// joystick output is connected to Arduino A1
#define joystick A1
void 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 potentiometer
int val = analogRead(joystick);
// if the joystic is in the middle ===> stop the motor
if ((val > 500) && (val < 523)) {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
else {
// move the motor in the first direction
if (val >= 523) {
// map the speed between 5 and 500 rpm
int speed_ = map(val, 523, 1023, 5, 1000);
// set motor speed
stepper.setSpeed(speed_);
// move the motor (1 step)
stepper.step(1);
val = analogRead(joystick);
}
// move the motor in the other direction
if (val <= 500) {
// map the speed between 5 and 500 rpm
int speed_ = map(val, 500, 0, 5, 1000);
// set motor speed
stepper.setSpeed(speed_);
// move the motor (1 step)
stepper.step(-1);
val = analogRead(joystick);
}
}
}
and here is the DC motor test code:
#define E1 5 // Enable Pin for motor
#define I1 3 // Control pin 1 for motor
#define I2 4 // Control pin 2 for motor
void 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);
}
When you see that a moderator (me) has removed something, in this case a link that looks like it might be spam, from your post then putting it back 5 minutes later isn't the wisest thing you can do. Please do not repost the link, doing so will result in permanent suspension.
This feels like finding a needle in a haystack...
Please explain what exact problem you have with this code.
Most here will not take the effort to first find your problem and then solve it..