Context of the code:
I am trying to control 2 DC motors through an H-gate, this means there are 4 inputs in total: For DC motor 1: Input 1 (VCC) and Input 2 (GND). For DC motor 2: Input 3(VCC) and Input 2 (GND).
Summary of the code:
The global variables are motinpN which are the pins connected to the different inputs.
An array, which is where the different PWM for the motor speeds are stored.
Char x, which is the variable used for the boolean logic.
Then comes the actual loop which consists of 4 similar functions, the "input selector" function and then the statements that turn the integers stored in the inpSpeedS[] arrays into motor speeds.
The issue:
The code won't enter the first loop, i.e it won't advance past
Serial.println("X equals:"); Serial.print(x);"
I would like to know then, what I am doing wrong in order for this not to work?
int Speed = 0;
const int motinp1 = 2;
const int motinp2 = 3;
const int motinp3 = 4;
const int motinp4 = 5;
char x = '0';
int inpSpeedS[4]; // An array to determine the speed of each individual input
void setup()
{
Serial.begin(9600);
pinMode(motinp1, OUTPUT);
}
void loop() {
Serial.println("X equals:");
Serial.print(x);
if (Serial.available () && (x == '0')) {
Serial.print("Which input?");
x = Serial.read();
Serial.println(x);
}
if (Serial.available () && (x == 'a'))
{
Serial.println("Input 1 selected");
inpSpeedS[0] = Serial.parseInt();
Serial.print("Input 1: ");
Serial.println(inpSpeedS[0]);
x = '0'; //reset x, goes back to input selection
} if (Serial.available () && (x == 'b'))
{
Serial.println("Input 2 selected");
inpSpeedS[1] = Serial.parseInt();
Serial.print("Input 2: ");
Serial.println(inpSpeedS[1]);
x = '0';
}
if (Serial.available () && (x == 'c'))
{
Serial.println("Input 3 selected");
inpSpeedS[2] = Serial.parseInt();
Serial.println("Input 3: ");
Serial.print(inpSpeedS[2]);
x = '0';
}
if (Serial.available () && (x == 'd'))
{
Serial.println("Input 4 selected");
inpSpeedS[3] = Serial.parseInt();
Serial.print("Input 4: ");
Serial.println(inpSpeedS[3]);
x = '0';
}
analogWrite(motinp1, inpSpeedS[0]);
analogWrite(motinp2, inpSpeedS[1]);
analogWrite(motinp3, inpSpeedS[2]);
analogWrite(motinp4, inpSpeedS[3]);
}
EDIT - I have turned the char 0 from a string literal null to an ASCII code for 0, and same for the other chars. It is still stuck outside of the function loops.
char x = "0";
doesn't yeld on you that you are assigning pointer to C string into char variable?'0'
and"0"
. First one is ASCII value of character zero, second one is pointer to memory, where the string containing {'0', '\0'} is stored