I'm currently doing a project that requires me to use 2 servos, a stepper and a color sensor. I currently have 2 Arduino Uno Controller boards but would like to only use one. I have codes for the stepper and servo but they only run on their own and I need them to run simultaneously. The stepper is connected to a power supply module.
Here's my sketch for the stepper:
int pin1 = 10;
int pin2 = 11;
int pin3 = 12;
int pin4 = 13;
boolean dir = false;
void setup()
{
// put your setup code here, to run once:
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT);
}
void loop()
{
switch(_step){
case 0:
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, HIGH);
break;
case 1:
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, HIGH);
break;
case 2:
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, LOW);
break;
case 3:
digitalWrite(pin1, LOW);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, LOW);
break;
case 4:
digitalWrite(pin1, LOW);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
break;
case 5:
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
break;
case 6:
digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
break;
case 7:
digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, HIGH);
break;
default:
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
break;
}
if (dir){
_step++;
}else{
_step--;
}
if(_step>7){
_step=0;
}
if(_step<0){
_step=7;
}
delay(1);
}
Here's the sketch for the servo:
#include <Servo.h>
Servo servo; // create servo object to control a servo
int angle = 10;// twelve servo objects can be created on most boards
// variable to store the servo position
void setup () {
servo.attach(8); // attaches the servo on pin 9 to the servo object
servo.write(angle);
}
void loop() {
for (angle = 10; angle < 180; angle++)
{
servo.write(angle); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
for(angle = 180; angle > 10;angle--)
{
servo.write(angle);
delay(5);
}
}
I'm new at coding and this is my first time ever using Arduino. Any help is appreciated.