Hello everybody!
First things first, new to arudino, new to programming, new to electronics! This is going to be difficult.
To start with, this is a project to electronically control the bump and rebound settings of the shock absorbers in our formula student race car this year via Arduino and bi-direction DC motors.
As i mentioned, I am very new to this whole area of engineering, being an automotive engineer. The first step taken was to compile a wireless programme (via Xbees) that would allow me to input a charactor in one COM port which would transmit to another, consequently controlling the motor as required, shown below:
#include <NewSoftSerial.h>
#include <AFMotor.h>
AF_DCMotor motor(1);
NewSoftSerial mySerial = NewSoftSerial(2, 13);
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
Serial.println("Hi P!");
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
mySerial.println("Hi D!");
motor.setSpeed(200);
}
void loop() // run over and over again
{
char newchar;
if (mySerial.available() > 0) {
newchar = mySerial.read();
Serial.print(newchar);
if (newchar == '1'){
//drive the motor
motor.run(FORWARD);
delay(1000);
motor.run(RELEASE);
}
if (newchar == '2'){
//drive the motor
motor.run(BACKWARD);
delay(1000);
motor.run(RELEASE);
}
}
}
So pressing 1 turned the motor forwards for 1 second, and 2 turned it backwards for a second.
The next step is to count the amount of pulses from the hall sensor during this process. The sensor has 4 pulses per rotation and is geared at a ratio of 298:1 (encoder shaft : output shaft).
This is where i have got to:
#include <NewSoftSerial.h>
#include <AFMotor.h>
AF_DCMotor motor(4);
const int EncoderSpeed = 19; // pin that encoder speed attached to
NewSoftSerial mySerial = NewSoftSerial(2, 13);
int pulseCounter = 0; // counter for number of pulses
void setup() {
pinMode(EncoderSpeed, INPUT);
pinMode(13, OUTPUT);
Serial.begin(9600);
Serial.println("Hi P!");
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
mySerial.println("Hi D!");
motor.setSpeed(200);
}
void loop() // run over and over again
{
char newchar;
if (mySerial.available() > 0) {
newchar = mySerial.read();
Serial.print(newchar);
if (newchar == '1'){
//drive the motor
motor.run(FORWARD);
delay(1000);
motor.run(RELEASE);
digitalRead(EncoderSpeed); // read the encoder input pin
mySerial.println("number of encoder pulses: ");
mySerial.println(pulseCounter, DEC);
}
}
}
I am receiving feedback wirelessly with the number of pulses, which is incorrect, simply 0 every time i input 1 to turn the motor for 1 second. I appreciate that interupts is the way to go, however, have little understanding of these currently.
The encoder is connected to the motor shield via the speed signal, not the direction as this is not needed for the project. I assume this is a very simple issue, but some guidance on how to actually get the arduino to count the pulses and transmit this back to me would be greatly appreciated.
Finally, the end requirement is to input a character which signifies a number of pulses for the motor to turn for, again any input here would be appreciated. Although I am trying to learn bit by bit!
Thanks