Some time ago I made an app to control (speed, direction, inertia) of locomotives on the pike of a friend, from an android smartphone.
Some years ago the app was working fine, but now is not !
What's wrong ?
And this is he Arduino sketch
QUOTE
/*
Operating Voltage 5V to 12V
Motor controller L298P, Drives 2 DC motors or 1 stepper motor
Max current 2A per channel
or 4A max (with external power supply)
Current sensing 1.65V/A
Free running stop and brake function
Size: 68 x 53 x 23mm (approx)
!
Arduino Motor Shield R3 29250
eBay link - Motor Shield 29250
Avaliable for ~ US D6.00
Fonction Moteur A Moteur B
Direction Digital 12 Digital 13
Vitesse Digital 3 Digital 11
Frein Digital 9 Digital 8
Note : ce shield utilise aussi les entres A0 et A1 da qualche altra parte dice che questi danno la corrente di uscita
La vitesse se r gle en modifiant la valeur attribu e aux broches D3 et D11
(100 dans les exemples ci-dessous) ; elle est comprise entre 0 et 255.
La direction (sens) se r gle sur D12 et : HAUT pour AVANT, BAS pour ARRIERE.
On freine en attribuant la valeur BAS D9 et D8 (HAUT ne freine pas).
NOTA BENE:
Se usato con connessione bluetooth i dati in ingresso arrivano da Serial3
e in <<void recvWithStartEndMarkers()>> mettere
while (Serial3.available() > 0 && newData == false) {
rc = Serial3.read();
Param = 1 > setvel
Param = 2 > Freno
Param = 9 > blocco
Param = 12 > direzione
Param = 15 > ritup ritardo in accelerazione
Param = 16 > ritdwn ritardo in decelerazione
*/
int sensorPin = A0;
int setvel = 0;
int actvel = 0;
int Freno = 0;
int var = 0;
int ritup = 100;
int ritdwn = 250;
int Vmin = 55;
int Vmax = 245;
int mA = 0;
int skip = 0;
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
String dataout; // passa i dati ad arduino slave <nnn,n,n
int Param = 0;
int Valore = 0;
boolean newData = false;
// =========================================================================
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial3.available() > 0 && newData == false) {
rc = Serial3.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
} else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//======================== PARSE DATA ==============================
void parseData() { // split the data into its parts
char* strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars, ","); // get the first part -
Param = atoi(strtokIndx); //
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
Valore = atoi(strtokIndx); // convert this part to an integer
/* strtokIndx = strtok(NULL, ",");
Status2 = atoi(strtokIndx); // convert this part to a float
strtokIndx = strtok(NULL, ",");
Status3 = atoi(strtokIndx); // convert this part to a float
*/
}
void showParsedData() {
Serial.print("Param = ");
Serial.println(Param);
Serial.print("Valore = ");
Serial.println(Valore);
Serial.println("====================================");
}
// °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°à
// °°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°
void setup() {
//Setup Channel A
Serial.begin(9600);
pinMode(12, OUTPUT); //Initiates Motor Channel A pin direzione
pinMode(9, OUTPUT); //Initiates Brake Channel A pin blocco
pinMode(3, OUTPUT);
//pinMode (A0, INPUT);
digitalWrite(12, HIGH);
digitalWrite(9, LOW);
analogWrite(3, 0);
}
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
// Serial3.println (receivedChars) ;
parseData();
showParsedData();
newData = false;
}
// =============================================================================
if (Param == 5000) {
if (skip == 1) {
goto skippa1;
}
Serial.println("Arduino è pronto");
Serial.print("Param = ");
Serial.println(Param);
Serial.println("Freno = 0");
Serial.println("Inerzia in accelerazione = 100 ");
Serial.println("Inerzia in decelerazione = 250 ");
Serial.println("Digita <1000> per continuare");
skippa1:
skip = 1;
goto saltatutto;
}
if (Param == 12 && Valore == 1) {
digitalWrite(12, HIGH);
Serial.println("AVANTI");
delay(1000);
Param = 1000;
}
if (Param == 12 && Valore == 0) {
digitalWrite(12, LOW);
Serial.println("indietro");
delay(1000);
Param = 1000;
}
if (Param == 9 && Valore == 1) {
digitalWrite(9, HIGH);
Param = 1000;
}
if (Param == 9 && Valore == 0) {
digitalWrite(9, LOW);
Param = 1000;
}
if (Param == 1) {
setvel = Valore;
if (setvel < Vmin) {
setvel = 0;
}
if (setvel > Vmax) {
setvel = Vmax;
}
Param = 1000;
}
if (Param == 2) {
Freno = Valore;
ritdwn = 255 - Freno;
Param = 1000;
}
if (Param == 15) {
ritup = Valore;
}
if (Param == 16) {
ritdwn = 255 - Freno;
}
if (actvel < setvel) {
actvel = actvel + 1;
analogWrite(3, actvel);
delay(ritup);
}
if (actvel > setvel) {
actvel = actvel - 1;
analogWrite(3, actvel);
delay(ritdwn);
}
Serial.print("SET/ACT Speed = ");
Serial.print(setvel);
Serial.print("/");
Serial.println(actvel);
mA = analogRead(sensorPin);
Serial.print("mA = ");
Serial.println(mA);
// ================================ CHIUDE LOOP ==============================0
saltatutto:
// Serial.print("Param = ");
// Serial.println(Param);
Valore = 0;
UNQUOTE