So I am trying to get my car remotely controlled from one raspberry pi that send signals (wireless) to another that is onboard the car. The second raspberry pi then transmits those signals (wired) to the Arduino. This works. What doesn't work is the next step. This next step is for the Arduino to interpret these commands and then act on them.
This is my code below:
int enA = 9;
int in1 = 8;
int in2 = 7;
char inChar;
String input;
String Input;
int potenVal;
int pwm = 0;
void setup() {
pinMode (enA, OUTPUT);
pinMode (in1, OUTPUT);
pinMode (in2, OUTPUT);
digitalWrite (in1, LOW);
digitalWrite (in2, LOW);
Serial.begin (9600);
}
void loop() {
if (Serial.available()) {
inChar = Serial.read();
if (isDigit(inChar)) {
Input += inChar;
}
if (inChar == '\n') {
int potenVal = Input.toInt();
unsigned n = potenVal;
testStage();
}
}
}
void testStage() {
//find what was sent
unsigned n = potenVal;
unsigned a = (n / 10000U) % 10;
if (a = 0) {
specialStage();
}
if (a = 1) {
normalStage();
}
if (a = 2) {
backwardsStage();
}
}
void specialStage() {
unsigned n = potenVal;
unsigned s = (n / 1000U) % 10;
if (s = 0) {
pwm = 255;
analogWrite (enA, pwm);
digitalWrite (in1, HIGH);
digitalWrite (in2, LOW);
//uncomment when two motors
/*analogWrite (enB, pwm);
digitalWrite (in3, LOW);
digitalWrite (in4, HIGH);
delay (1000);*/
}
if (s = 1) {
pwm = 255;
analogWrite (enA, pwm);
digitalWrite (in1, HIGH);
digitalWrite (in2, LOW);
//uncomment when two motors
/*analogWrite (enB, pwm);
digitalWrite (in3, LOW);
digitalWrite (in4, HIGH);
delay (1000);*/
}
}
void normalStage() {
unsigned n = potenVal;
unsigned n1 = (n / 100U) % 10;
unsigned n2 = (n / 10U) % 10;
unsigned n3 = (n / 1U) % 10;
char no1 = n1;
input += no1;
char no2 = n2;
input += no2;
char no3 = n3;
input += no3;
pwm = input.toInt();
analogWrite (enA, pwm);
digitalWrite (in1, HIGH);
digitalWrite (in2, LOW);
}
void backwardsStage() {
unsigned n = potenVal;
unsigned n1 = (n / 100U) % 10;
unsigned n2 = (n / 10U) % 10;
unsigned n3 = (n / 1U) % 10;
char no1 = n1;
input += no1;
char no2 = n2;
input += no2;
char no3 = n3;
input += no3;
pwm = input.toInt();
analogWrite (enA, pwm);
digitalWrite (in1, LOW);
digitalWrite (in2, HIGH);
Serial.println (pwm);
}
What I believe the problem is is that the variable pwm's modifications are not saved when i move to the next function, however i could be wrong. The String that I am sending is '20123'. I am receiving '0'
I'd add a couple Serial.print()s of Input, potentVal to make sure you are receiving what you think you're receiving. If you did send '20123', then you should be able to print it and the 2 your code should be translating it into.
/////////
Oh-- the int and unsigned here make these local variables so you aren't writing into the globals. Take them out inside the functions if you want to refer to the global vars.
Okay, i think I found the problem. So after following your advice i found that the conversion of int n1 to char no1 kind of broke it. I don't know how or why but it did. Fortunately, when I remove it the code works mostly. The bit that doesn't work is the
It isn't returnng input as an int but instead 0. Any Ideas? Here's my updated code:
int enA = 9;
int in1 = 8;
int in2 = 7;
char inChar;
String input;
String Input;
int potenVal;
unsigned n;
int pwm;
void setup() {
pinMode (enA, OUTPUT);
pinMode (in1, OUTPUT);
pinMode (in2, OUTPUT);
digitalWrite (in1, LOW);
digitalWrite (in2, LOW);
Serial.begin (9600);
}
void loop() {
if (Serial.available()) {
inChar = Serial.read();
if (isDigit(inChar)) {
Input += inChar;
}
if (inChar == '\n') {
potenVal = Input.toInt();
n = potenVal;
testStage();
}
}
}
void testStage() {
//find what was sent
n = potenVal;
unsigned a = (n / 10000U) % 10;
if (a = 0) {
specialStage();
}
if (a = 1) {
normalStage();
}
if (a = 2) {
backwardsStage();
}
}
void specialStage() {
unsigned n = potenVal;
unsigned s = (n / 1000U) % 10;
if (s = 0) {
pwm = 255;
analogWrite (enA, pwm);
digitalWrite (in1, HIGH);
digitalWrite (in2, LOW);
//uncomment when two motors
/*analogWrite (enB, pwm);
digitalWrite (in3, LOW);
digitalWrite (in4, HIGH);
delay (1000);*/
}
if (s = 1) {
pwm = 255;
analogWrite (enA, pwm);
digitalWrite (in1, HIGH);
digitalWrite (in2, LOW);
//uncomment when two motors
/*analogWrite (enB, pwm);
digitalWrite (in3, LOW);
digitalWrite (in4, HIGH);
delay (1000);*/
}
}
void normalStage() {
unsigned n = potenVal;
unsigned n1 = (n / 100U) % 10;
unsigned n2 = (n / 10U) % 10;
unsigned n3 = (n / 1U) % 10;
char no1 = n1;
input += no1;
char no2 = n2;
input += no2;
char no3 = n3;
input += no3;
pwm = input.toInt();
analogWrite (enA, pwm);
digitalWrite (in1, HIGH);
digitalWrite (in2, LOW);
}
void backwardsStage() {
unsigned n1 = (n / 100U) % 10;
unsigned n2 = (n / 10U) % 10;
unsigned n3 = (n / 1U) % 10;
input += n1;
input += n2;
input += n3;
Serial.println (input);
pwm = input.toInt;
Serial.println (input);
Serial.println (pwm);
analogWrite (enA, pwm);
digitalWrite (in1, LOW);
digitalWrite (in2, HIGH);
Serial.println (pwm);
}
It sounds like you are expecting the number (char)0 == '0' but (int8_t)'0' == 48 because ASCII.
If I understand correctly, the intention here is to take the last three decimal digits of an unsigned integer like 54321 and turn it into the number 321?
Instead of the String munging , why not do it with arithmetic: pwm = n % 1000 ?
So I have made to suggested edits and the code works the first 5-10 times (I can get a more precise number if required) but after that the serial works and the motor fails to turn. After a reset of the board, however, the motor starts spinning again. Help would be greatly appreciated. This is the new code:
int enA = 9;
int in1 = 8;
int in2 = 7;
char inChar;
String input;
String Input;
int potenVal;
unsigned n;
int pwm;
void setup() {
pinMode (enA, OUTPUT);
pinMode (in1, OUTPUT);
pinMode (in2, OUTPUT);
digitalWrite (in1, LOW);
digitalWrite (in2, LOW);
Serial.begin (9600);
}
void loop() {
if (Serial.available() > 0) {
inChar = Serial.read();
if (isDigit(inChar)) {
Input += inChar;
}
if (inChar == '\n') {
potenVal = Input.toInt();
n = potenVal;
testStage();
}
}
}
void testStage() {
//find what was sent
n = potenVal;
unsigned a = (n / 10000U) % 10;
if (a = 0) {
specialStage();
}
if (a = 1) {
normalStage();
}
if (a = 2) {
backwardsStage();
}
}
void specialStage() {
unsigned n = potenVal;
unsigned s = (n / 1000U) % 10;
if (s = 0) {
pwm = 255;
analogWrite (enA, pwm);
digitalWrite (in1, HIGH);
digitalWrite (in2, LOW);
Input = "";
//uncomment when two motors
/*analogWrite (enB, pwm);
digitalWrite (in3, LOW);
digitalWrite (in4, HIGH);
delay (1000);*/
}
if (s = 1) {
pwm = 255;
analogWrite (enA, pwm);
digitalWrite (in1, HIGH);
digitalWrite (in2, LOW);
Input = "";
//uncomment when two motors
/*analogWrite (enB, pwm);
digitalWrite (in3, LOW);
digitalWrite (in4, HIGH);
delay (1000);*/
}
}
void normalStage() {
unsigned n = potenVal;
unsigned n1 = (n / 100U) % 10;
unsigned n2 = (n / 10U) % 10;
unsigned n3 = (n / 1U) % 10;
char no1 = n1;
input += no1;
char no2 = n2;
input += no2;
char no3 = n3;
input += no3;
pwm = input.toInt();
analogWrite (enA, pwm);
digitalWrite (in1, HIGH);
digitalWrite (in2, LOW);
Input = "";
}
void backwardsStage() {
pwm = n % 1000;
analogWrite (enA, pwm);
digitalWrite (in1, LOW);
digitalWrite (in2, HIGH);
Serial.println (pwm);
delay (1000);
Serial.println (pwm);
digitalWrite (in1, LOW);
digitalWrite (in2, LOW);
Input = "";
}