#include <Servo.h>
#define MAX_SIGNAL 2000
#define MIN_SIGNAL 700
#define M1 3
#define M2 5
#define M3 6
#define M4 10
Servo motor1;
Servo motor2;
Servo motor3;
Servo motor4;
int speed1, speed2, speed3, speed4;
void setup() {
Serial.begin(9600);
motor1.attach(M1);
motor2.attach(M2);
motor3.attach(M3);
motor4.attach(M4);
motor1.writeMicroseconds(MIN_SIGNAL);
motor2.writeMicroseconds(MIN_SIGNAL);
motor3.writeMicroseconds(MIN_SIGNAL);
motor4.writeMicroseconds(MIN_SIGNAL);
Serial.println("Sending minimum signal. Power on motor and wait for beeps.");
delay(4000);
Serial.println("After beeps, type a signal length terminated by m and the motor number to motor. For example,");
Serial.print(MIN_SIGNAL);
Serial.println("m1 to stop the motor 1. ma is for all motors.");
}
void stop() {
Serial.println("stoping...");
motor1.writeMicroseconds(MIN_SIGNAL);
motor2.writeMicroseconds(MIN_SIGNAL);
motor3.writeMicroseconds(MIN_SIGNAL);
motor4.writeMicroseconds(MIN_SIGNAL);
speed1 = MIN_SIGNAL;
speed2 = MIN_SIGNAL;
speed3 = MIN_SIGNAL;
speed4 = MIN_SIGNAL;
}
void loop() {
int input, number;
// Wait for input
if (!Serial.available()) return;
input = Serial.read();
// emergency stop command is ', right next to enter key
if (input == '\'') {
stop();
return;
}
// Convert string to integer. String terminated with m (for microseconds).
number = input - '0';
while ((input = Serial.read()) != 'm') {
if (input == -1) continue;
number *= 10;
number += input - '0';
}
while ((input = Serial.read()) == -1);
switch (input) {
case 'a':
speed1 = number;
speed2 = number;
speed3 = number;
speed4 = number;
break;
case '1':
speed1 = number;
break;
case '2':
speed2 = number;
break;
case '3':
speed3 = number;
break;
case '4':
speed4 = number;
break;
default:
break;
}
Serial.print("Now writing: ");
Serial.print(speed1);
Serial.print(" ");
Serial.print(speed2);
Serial.print(" ");
Serial.print(speed3);
Serial.print(" ");
Serial.println(speed4);
motor1.writeMicroseconds(speed1);
motor2.writeMicroseconds(speed2);
motor3.writeMicroseconds(speed3);
motor4.writeMicroseconds(speed4);
}
as you can see if i would like to get all my motors to run i would need the app inventor to send a value of for example 1500ma to the arduino , however i was able to only send a value of 0-255 , is there anyway i can do this?