Hi guys,
I started my first ever project on Thursday night.
This is for a university project.
I'm studying mechanical engineering so I'm out of my comfort zone at the moment but keen to learn.
I have no previous coding experience.
The basic ides of my project is :
To detect the 8 positions (N, NE, E, SE, S, SW, W, NW) of a DC motor.
I will be using 2 optical sensors and a disk encoder to detect the motor position.
The encoder is concentric with the output shaft of the motor, contains 4 holes about its perimeter and also has a single nipple (for a loss of other words) extruded from its outer edge, parallel with the disk face.
One optical sensor will detect the 4 holes and the other optical sensor will detect the nipple.
The motors output shaft is connected to a 2:1 gear box and then to a compass dial.
Therefore, a combination of the optical eye readings will give the desired position ( 1,1 = N, 1,2 = NE .... 2,3 = W, 2,4 = NW)
For the purpose of this exercise I will be using 2 push buttons instead of the optical sensors to simulate the position of the encoder and I will also be using LED's to signal the state of the motor (RED = ON, GREEN OFF) (Counter-intuitive, I know), and finally I will be using the serial port to command the desired position (ideally a wind direction sensor will provide the direction but will keep the serial port command control for demonstration purposes).
Problems encounter / desired outcome:
I am using a switch-case statement to differentiate between the different serial commands. Each serial command begins by turning on the motor, reading the the current position of the motor, reacting accordingly (if not at desired position stay on, if at desired position turn off) and then exits the switch-case statement.
My problem is that this set up doesn't allow time for the encoder to reach the desired position before reacting.
Ideally, I would like to enter the desired switch-case statement, loop through this switch-case statement until the correct position has been met, stop the motor at this point and return to main loop.
I have studied the examples and many tutorials which I thought were relevant and have spent hours trawling through the net to find a similar previously asked question to no avail.
I have been staring at the computer for 12 hours a day since Thursday and maybe it might just take a fresh pair of eyes to help me understand.
If anyone can reference me to some relevant material or shed light on this via their experience or otherwise I would greatly appreciate it.
Thank you.
const int buttonPin1 = PUSH2;
const int ledPin1 = GREEN_LED;
const int buttonPin2 = PUSH1;
const int ledPin2 = BLUE_LED;
const int motor = RED_LED;
int buttonPushCounter1 = 0;
int buttonState1 = 0;
int lastButtonState1 = 0;
int buttonPushCounter2 = 0;
int buttonState2 = 0;
int lastButtonState2 = 0;
int motorstate = 0;
int lastmotorstate = 0;
int ByteReceived;
void setup() {
Serial.begin(9600);
Serial.println(" Type Direction in Box above, . ");
pinMode(buttonPin1, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(ledPin2, OUTPUT);
pinMode(motor, OUTPUT);
}
void loop() {
buttonState1 = digitalRead(buttonPin1);
if (buttonState1 != lastButtonState1) {
if (buttonState1 == HIGH) {
buttonPushCounter1++;
Serial.println("on");
Serial.print("number of button2 pushes: ");
Serial.println(buttonPushCounter1);
}
else {
Serial.println("off");
}
if(buttonPushCounter1 >= 2) buttonPushCounter1 =0;
}
{buttonState2 = digitalRead(buttonPin2);
if (buttonState2 != lastButtonState2) {
if (buttonState2 == HIGH) {
buttonPushCounter2++;
Serial.println("on");
Serial.print("number of button1 pushes: ");
Serial.println(buttonPushCounter2);
}
else {
Serial.println("off");
}
if(buttonPushCounter2 >= 4) buttonPushCounter2 =0;
}
lastButtonState1 = buttonState1;
lastButtonState2 = buttonState2;
if (Serial.available() > 0)
{
int ByteReceived = Serial.read();
Serial.print(ByteReceived);
Serial.print(" ");
Serial.print(ByteReceived, HEX);
Serial.print(" ");
Serial.print(char(ByteReceived));
switch (ByteReceived) {
case 'o':
digitalWrite(motor, HIGH);
Serial.print(" motor ON ");
break;
case 'x':
digitalWrite(motor, LOW);
Serial.print(" motor OFF ");
break;
case '1':
digitalWrite(motor, HIGH);
Serial.print(" motor ON ");
{motorstate = digitalRead(motor);
if ((buttonPushCounter1 % 1 == 0)&&(buttonPushCounter2 % 1 == 0)) {
digitalWrite(ledPin1, HIGH);
digitalWrite(motor, LOW);
if (motorstate != lastmotorstate) {
Serial.print(" motor OFF ");
}
else {
digitalWrite(ledPin1, LOW);
}
lastmotorstate = motorstate;
break;
case '2':
digitalWrite(motor, HIGH);
Serial.print(" motor ON ");
{motorstate = digitalRead(motor);
if ((buttonPushCounter1 % 2 == 0)&&(buttonPushCounter2 % 1 == 0)) {
digitalWrite(ledPin1, HIGH);
digitalWrite(motor, LOW);
if (motorstate != lastmotorstate) {
Serial.print(" motor OFF ");
}
else {
digitalWrite(ledPin1, LOW);
}
lastmotorstate = motorstate;
break;
case '3':
digitalWrite(motor, HIGH);
Serial.print(" motor ON ");
{
motorstate = digitalRead(motor);
if ((buttonPushCounter1 % 3 == 0)&&(buttonPushCounter2 % 1 == 0)) {
digitalWrite(ledPin1, HIGH);
digitalWrite(motor, LOW);
if (motorstate != lastmotorstate) {
Serial.print(" motor OFF ");
}
else {
digitalWrite(ledPin1, LOW);
}
lastmotorstate = motorstate;
break;
//... Continues on until case 8
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}