I wrote a code for an RC car, and I want to control it with a joystick in the APP INVENTOR. How do I set the block?
#include <SoftwareSerial.h>
#include <Servo.h>
Servo myServo;
int motor_pwm = 8;
int motor_pwm1 = 2;
int motor_pwm2 = 3;
int r_motor_n = 10; //PWM control Right Motor +
int r_motor_p = 11; //PWM control Right Motor -
int l_motor_p = 9; //PWM control Left Motor -
int l_motor_n = 6; //PWM control Left Motor +
int s_motor_p = 4; //PWM control Left Motor -
int s_motor_n = 5; //PWM control Left Motor +
int serv = 13;
int speedy = 255;
char incomingByte = 0; // for incoming serial data
SoftwareSerial ble ( 0, 1 );
void setup()
{
Serial.begin ( 9600 );
ble.begin ( 9600 );
myServo.attach(13);
myServo.write(0);
pinMode(motor_pwm, 0);
pinMode(motor_pwm1, 0);
pinMode(motor_pwm2, 0);
pinMode(r_motor_n, OUTPUT); //Set control pins to be outputs
pinMode(r_motor_p, OUTPUT);
pinMode(l_motor_p, OUTPUT);
pinMode(l_motor_n, OUTPUT);
pinMode(s_motor_p, OUTPUT);
pinMode(s_motor_n, OUTPUT);
digitalWrite(r_motor_n, LOW); //set both motors off for start-up
digitalWrite(r_motor_p, LOW);
digitalWrite(l_motor_p, LOW);
digitalWrite(l_motor_n, LOW);
digitalWrite(s_motor_p, LOW);
digitalWrite(s_motor_n, LOW);
Serial.begin(9600);
}
void loop()
{
analogWrite(motor_pwm, 255);
analogWrite(motor_pwm1, 255);
analogWrite(motor_pwm2, 255);
if (ble.available() > 0)
{
incomingByte = ble.read();
Serial.println(incomingByte);
switch (incomingByte)
{
case 'S': // control to stop the robot
digitalWrite(r_motor_n, LOW);
digitalWrite(r_motor_p, LOW);
digitalWrite(l_motor_p, LOW);
digitalWrite(l_motor_n, LOW);
Serial.println("Stop");
incomingByte = '*';
break;
case 'R': //control for right
analogWrite(r_motor_n, speedy);
digitalWrite(r_motor_p, LOW);
analogWrite(l_motor_p, speedy);
digitalWrite(l_motor_n, LOW);
Serial.println("right");
incomingByte = '*';
break;
case 'L': //control for left
digitalWrite(r_motor_n, LOW);
analogWrite(r_motor_p, speedy);
digitalWrite(l_motor_p, LOW);
analogWrite(l_motor_n, speedy);
Serial.println("left");
incomingByte = '*';
break;
case 'F': //control for forward
analogWrite(r_motor_n, speedy);
digitalWrite(r_motor_p, LOW);
digitalWrite(l_motor_p, LOW);
analogWrite(l_motor_n, speedy);
Serial.println("forward");
incomingByte = '*';
break;
case 'B': //control for backward
digitalWrite(r_motor_n, LOW);
analogWrite(r_motor_p, speedy);
analogWrite(l_motor_p, speedy);
digitalWrite(l_motor_n, LOW);
Serial.println("back");
incomingByte = '*';
break;
case 'M': //control Mopper
digitalWrite(s_motor_n, speedy);
analogWrite(s_motor_p, LOW);
Serial.println("mopper on");
incomingByte = '*';
break;
case 'm': //Stop Mopper
digitalWrite(s_motor_n, LOW);
analogWrite(s_motor_p, LOW);
Serial.println("mopper off");
incomingByte = '*';
break;
case 'U': // roller up
myServo.write(0);
Serial.println("roller up");
incomingByte = '*';
break;
case 'u': // roller down
myServo.write(135);
Serial.println("roller down");
incomingByte = '*';
break;
case 'a':
speedy = 100;
Serial.println("speed= 10");
incomingByte = '*';
break;
case 'b':
speedy = 150;
Serial.println("speed= 25");
incomingByte = '*';
break;
case 'c':
speedy = 200;
Serial.println("speed= 75");
incomingByte = '*';
break;
case 'd':
speedy = 255;
Serial.println("speed= 100");
incomingByte = '*';
break;
}
}
}