Controllo Arduino Car

Sto provando a realizzare una Arduino Car con BT, per farlo voglio realizzare una applicazione con MIT APP Inventor per realizzare il telecomando...qui sorge il problema, non so come realizzare un tasto che quando viene premuto invia ad Arduino un valore "x" e quando viene lasciato non lo invia più.

//L293 Connection   
  const int motorA1  = 5;  // Pin  2 of L293
  const int motorA2  = 6;  // Pin  7 of L293
  const int motorB1  = 10; // Pin 10 of L293
  const int motorB2  = 11;  // Pin 14 of L293  
//Useful Variables
  int i=0;
  int j=0;
  char state;
  int vSpeed=200;     // Default speed, from 0 to 255

void setup() {
    // Set pins as outputs:
    pinMode(motorA1, OUTPUT);
    pinMode(motorA2, OUTPUT);
    pinMode(motorB1, OUTPUT);
    pinMode(motorB2, OUTPUT); 
    // Initialize serial communication at 9600 bits per second:
    Serial.begin(9600);
}
 
void loop() {

  //Save income data to variable 'state'
    if(Serial.available() > 0){     
      state = Serial.read(); 
      Serial.println(state);
      Stop(); 
    }
  
     
  /***********************Forward****************************/
  //If state is equal with letter 'A', car will go forward!
    if (state == 'A') {
      analogWrite(motorA1, vSpeed); 
      analogWrite(motorA2, vSpeed);
      analogWrite(motorB1, vSpeed);      
      analogWrite(motorB2, vSpeed); 
     
    }
  /***********************Backward****************************/
  //If state is equal with letter 'I', car will go backward
    else if (state == 'I') {
      analogWrite(motorA1, -vSpeed);   
      analogWrite(motorA2, -vSpeed); 
      analogWrite(motorB1, -vSpeed);  
      analogWrite(motorB2, -vSpeed); 
    }
  /***************************Left*****************************/
  //If state is equal with letter 'S', wheels will turn left
    else if (state == 'S') {
      analogWrite(motorA1, vSpeed);   
      analogWrite(motorA2, 0); 
      analogWrite(motorB1, 0); 
      analogWrite(motorB2, vSpeed); 
    }
  /***************************Right*****************************/
  //If state is equal with letter 'D', wheels will turn right
    else if (state == 'D') {
      analogWrite(motorA1, 0);   
      analogWrite(motorA2, vSpeed); 
      analogWrite(motorB1, vSpeed);   
      analogWrite(motorB2, 0);     
    }
  /************************Bonus*****************************/

  /************************Bonus*****************************/

  /************************Bonus*****************************/

  /**********************Bonus***************************/

  /************************Bonus*****************************/
  //If state is equal with letter 'S', stop the car
    else if (state == 'P'){
        analogWrite(motorA1, 0);  
        analogWrite(motorA2, 0); 
        analogWrite(motorB1, 0);  
        analogWrite(motorB2, 0);
    }
   
}

void Stop() {
  analogWrite(motorA1, 0);  
  analogWrite(motorA2, 0); 
  analogWrite(motorB1, 0);  
  analogWrite(motorB2, 0);
}

Scrivo questo perchè in futuro a qualcuno potrebbe servire:
Ho modificato il codice di Arduino e anche quello del Mit app inventor

//L293 Connection   
  const int motorA1  = 5;  // Pin  2 of L293
  const int motorA2  = 6;  // Pin  7 of L293
  const int motorB1  = 10; // Pin 10 of L293
  const int motorB2  = 11;  // Pin 14 of L293  
//Useful Variables
  int i=0;
  int j=0;
  char state;
  int vSpeed=200;     // Default speed, from 0 to 255

void setup() {
    // Set pins as outputs:
    pinMode(motorA1, OUTPUT);
    pinMode(motorA2, OUTPUT);
    pinMode(motorB1, OUTPUT);
    pinMode(motorB2, OUTPUT); 
    // Initialize serial communication at 9600 bits per second:
    Serial.begin(9600);
}
 
void loop() {

  //Save income data to variable 'state'
    if(Serial.available() > 0){     
      state = Serial.read(); 
      Serial.println(state);
    }
  
     
  /***********************Forward****************************/
  //If state is equal with letter 'A', car will go forward!
    if (state == 'A') {
      analogWrite(motorA1, vSpeed); 
      analogWrite(motorA2, vSpeed);
      analogWrite(motorB1, vSpeed);      
      analogWrite(motorB2, vSpeed); 
     
    }
  /***********************Backward****************************/
  //If state is equal with letter 'I', car will go backward
    else if (state == 'I') {
      analogWrite(motorA1, -vSpeed);   
      analogWrite(motorA2, -vSpeed); 
      analogWrite(motorB1, -vSpeed);  
      analogWrite(motorB2, -vSpeed); 
    }
  /***************************Left*****************************/
  //If state is equal with letter 'S', wheels will turn left
    else if (state == 'S') {
      analogWrite(motorA1, vSpeed);   
      analogWrite(motorA2, 0); 
      analogWrite(motorB1, 0); 
      analogWrite(motorB2, vSpeed); 
    }
  /***************************Right*****************************/
  //If state is equal with letter 'D', wheels will turn right
    else if (state == 'D') {
      analogWrite(motorA1, 0);   
      analogWrite(motorA2, vSpeed); 
      analogWrite(motorB1, vSpeed);   
      analogWrite(motorB2, 0);     
    }
  /************************Bonus*****************************/

  /************************Bonus*****************************/

  /************************Bonus*****************************/

  /**********************Bonus***************************/

  /************************Bonus*****************************/
  //If state is equal with letter 'S', stop the car
    else if (state == 'P'){
        analogWrite(motorA1, 0);  
        analogWrite(motorA2, 0); 
        analogWrite(motorB1, 0);  
        analogWrite(motorB2, 0);
    }
   
}

quando il bottone viene premuto invia l'input di muoversi, quando si rilascia invia un input che gli impone di fermare la macchina

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.