Car needle sweep project

Arduino121111's picture

Offline

Joined: Dec 10, 2019

Replies: 1
Car needle sweep project
December 10, 2019 - 6:34am

I am trying to add needle sweep to my car speedo and rev counter. I have used an arduino uno and managed to use the following code to get the needle to move. My problem is when i edit the code for 2 ouputs to control speedo and rev needles it does not work. Im not very experienced in writing code so im asking if anyone would be able to help me edit the code so i can add 2 needle sweeps and to only sweep back and forth once then stop till power is disconnected

/*
Stepper Motor Control - one revolution

This program drives a bipolar stepper motor.
The motor is attached to digital pins 3 - 6 of the Arduino.

The motor should revolve one revolution in one direction, then
one revolution in the other direction.

*/

#include

const int stepsPerRevolution = 420; // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins3 through 6:
Stepper myStepper(stepsPerRevolution, 6, 5, 4,3);

void setup() {
// set the speed at 65 rpm:
myStepper.setSpeed(65);
// initialize the serial port:
Serial.begin(9600);
}

void loop() {
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(500);

// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
delay(500);
}

Aswinth Raj's picture

Offline

Joined: Aug 16, 2016

Replies: 953
Control two stepper motor simultaneously with Arduino
December 10, 2019 - 1:59pm

I can see many mistakes in your program including missing a header file. If you really want to understand how things work and how to write your program you can read the following two links 

https://circuitdigest.com/tutorial/what-is-stepper-motor-and-how-it-works

https://circuitdigest.com/microcontroller-projects/arduino-stepper-motor-control-tutorial

 

Instead, if you just want things to work and get away with it you can try the following program 

// Arduino stepper motor control code

#include <Stepper.h> // Include the header file

// change this to the number of steps on your motor
#define STEPS 32

// create an instance of the stepper class using the steps and pins
Stepper stepper1(STEPS, 8, 10, 9, 11);

Stepper stepper2(STEPS, 2, 4, 3, 5);

int val = 0;

void setup() {
  Serial.begin(9600);
  stepper1.setSpeed(200); stepper2.setSpeed(200);
}

void loop() {

  if (Serial.available()>0)
  {
    val = Serial.parseInt();
    stepper1.step(val); stepper2.step(val);
    Serial.println(val); //for debugging
  }

}

Make sure you have connected the first motor to pins 8,9,10 and 11 ... The 2nd motor to pins 2,3,4and5. I cannot guarantee this program to work without knowing what motor you are using and the circuit circuit diagram of your project 

Hope this helps you in some way 

Arduino121111's picture

Offline

Joined: Dec 10, 2019

Replies: 1
Ok so iv done some code and
December 15, 2019 - 11:06pm

Ok so iv done some code and it all works except motor one starts first and seems to move faster than motor 2 when i need them both to move at same time.

#include
int start = 0;
const int stepsPerRevolution = 420; // change this to fit the number of steps per revolution motor 1
const int stepsPerRevolution2 = 420;// change this to fit the number of steps per revolution motor 2

// initialize the stepper library on pins 1.2.3.4//5.6.7.8
Stepper myStepper(stepsPerRevolution, 4, 3, 2,1); //motor 1
Stepper myStepper2(stepsPerRevolution, 8, 7, 6,5); //motor 2
void setup() {

// set the speed at 65 rpm:
myStepper.setSpeed(65);
myStepper2.setSpeed(65);
pinMode(13, OUTPUT);
}

void swip()
{
//farward direction motor 1 & 2
myStepper.step(stepsPerRevolution);
delay(100);
myStepper2.step(stepsPerRevolution2);
delay(100);
//reverse direction motor 1 & 2
myStepper.step(-stepsPerRevolution);
delay(100) ;
myStepper2.step(-stepsPerRevolution2);
delay(100) ;
start++;
}
void loop() {

if(start==0)
swip();
digitalWrite(13, HIGH);

}