arduino , i2c e pulsanti

Salve :slight_smile: dunque io ho un Arduino uno e per controllare un motore da 24 volt uso un md03 .
Ora scaricando qualche esempio , configuarndo la comunicazione i2c, tutto funziona alla perfezione. Ora, se pero' aggiungo delle if clause ( perché dovo comandare il motore con dei pulsanti ) il motore non si muove più. Come faccio a fare interagire dei pulsanti con la comunicazione i2c ?
Grazie mille a tutti :slight_smile:

  1. togli tutti i delay()
  2. ci fai vedere il Tuo sketch che non funziona.
    Ciao Uwe

oky grazie mille :slight_smile: ecco qua i 2 programmi che ho tentato con insuccesso
primo tentativo :

*******************************************************
*             MD03 example for Arduino                 *
*   MD03 is in I2C mode LCD03 controlled by serial     *
*                                                      *
*             By James Henderson 2012                  *
*******************************************************/

#include <Wire.h>
#include <SoftwareSerial.h>

#define ADDRESS             0x58                    // Address of MD03
#define SOFTREG             0x07                    // Byte to read software
#define CMDBYTE             0x00                    // Command byte
#define SPEEDBYTE           0x02                    // Byte to write to speed register
#define TEMPREG             0x04                    // Byte to read temprature
#define CURRENTREG          0x05                    // Byte to read motor current

#define LCD_RX              0x02                    // Pin for rx
#define LCD_TX              0x03                    // Pin for tx
#define LCD03_HIDE_CUR      0x04
#define LCD03_CLEAR         0x0C
#define LCD03_SET_CUR       0x02

SoftwareSerial lcd_03 = SoftwareSerial(LCD_RX, LCD_TX);  // Sets up serial for LCD03

byte direct = 1; 
boolean inte = 1;
boolean inte3 = 1;
boolean inte2 = 1;
boolean inte4 = 1;
int lopp = 0;
boolean flx = 0;
boolean flag = 0;
// Stores what direction the motor should run in

void setup(){
 lcd_03.begin(9600);                                    // Begin serial for LCD03
 
 Wire.begin();
 delay(100);
 
 lcd_03.write(LCD03_HIDE_CUR);                          // Hides LCD03 cursor
 lcd_03.write(LCD03_CLEAR);                             // Clears LCD03 screen
 
 int software = getData(SOFTREG);                       // Gets software version and prints it to LCD03
 lcd_03.print("MD03 Example  V:");
 lcd_03.print(software);
 pinMode (7, INPUT_PULLUP);
 pinMode (8, INPUT_PULLUP);
 pinMode (5, INPUT_PULLUP);
 pinMode (10, INPUT_PULLUP);
}


void loop(){
 inte = digitalRead(7);
 inte2 = digitalRead(8);
 inte3 = digitalRead(5);
 if (inte == 0 && flag == 0 && inte2 == 0 && inte3 == 0)  
 {
 for(int i = 0; i < 250; i=i++){
   sendData(SPEEDBYTE, i);             // Sets speed to i         // Sets motor to direct, a value of 1 runs the motor forward and 2 runs backward
   int temp = getData(TEMPREG);
   inte = digitalRead(7);
   inte2 = digitalRead(8);
   inte3 = digitalRead(5);
   if (inte == 1) {flag =1;flx =1;break;}
   if (inte2 == 1) {flag =1;flx =1;break;}
   if (inte3 == 1) {flag =1;flx =1;break;}    // Gets temperature
              // Wait to make sure all data sent  
 }
  if (flx == 0)
 {
   Serial.println("led tutto acceso");
   sendData (SPEEDBYTE, 250);
   flag = 1;
   flx = 1;
   
 }
 }
  if (inte == 1 && flag == 1 && inte2 == 0 && inte3 == 0 )
 {
   Serial.println("entro nel If di spegnimento");
   for(int i = 0; i > 0; i=i--){
   sendData(SPEEDBYTE, i);             // Sets speed to i         // Sets motor to direct, a value of 1 runs the motor forward and 2 runs backward
   int temp = getData(TEMPREG);
   inte = digitalRead(7);
   if (inte == 0) {flag =0;flx=1;break;}
  }
  if (flx==1)
   {
   Serial.println("led tutto spento");
     sendData (SPEEDBYTE, 0);
     flag = 0;
     flx = 0;

 }
  } 
     if (inte == 0 && flag == 1 && inte2 == 1 && inte3 == 0 )
 {
   Serial.println("entro nel If di spegnimento");
   for(int i = 0; i > 0; i=i--){
   sendData(SPEEDBYTE, i);             // Sets speed to i         // Sets motor to direct, a value of 1 runs the motor forward and 2 runs backward
   int temp = getData(TEMPREG);
   inte = digitalRead(7);
   if (inte2 == 0) {flag =0;flx=1;break;}
  }
   if (flx==1)
   {
   Serial.println("led tutto spento");
     sendData (SPEEDBYTE, 0);   
     flag = 0;
     flx = 0;

 }
  } 
   if (inte == 0 && flag == 1 && inte2 == 0 && inte3 == 1 )
 {
   Serial.println("entro nel If di spegnimento");
   for(int i = 0; i > 0; i=i--){
   sendData(SPEEDBYTE, i);             // Sets speed to i         // Sets motor to direct, a value of 1 runs the motor forward and 2 runs backward
   int temp = getData(TEMPREG);
   inte = digitalRead(7);
   if (inte3 == 0) {flag =0;flx=1;break;}
  }
   if (flx==1)
   {
   Serial.println("led tutto spento");
     sendData (SPEEDBYTE, 0);
     flag = 0;
     flx = 0;

 }
  }
   inte4 = digitalRead(10);
 
 if(inte4 == 1){                       // If loop that swaps value of direct between 1 and 2 each time through loop
   direct = 2;
 }    
  inte4 = digitalRead(10);
 
 if(inte4 == 0){                       // If loop that swaps value of direct between 1 and 2 each time through loop
   direct = 1;
 }                       
}

byte getData(byte reg){                   // function for getting data from MD03
 Wire.beginTransmission(ADDRESS);
   Wire.write(reg);
 Wire.endTransmission();
 
 Wire.requestFrom(ADDRESS, 1);         // Requests byte from MD03
 while(Wire.available() < 1);          // Waits for byte to become availble
 byte data = Wire.read();

 return(data);
}

void sendData(byte reg, byte val){         // Function for sending data to MD03
 Wire.beginTransmission(ADDRESS);         // Send data to MD03
   Wire.write(reg);
   Wire.write(val);
 Wire.endTransmission();
}

ecco qui il secondo :

**
* This is a simple test of the MD03 motor controller.  It makes the motor move 
* forward for five seconds, stop for five seconds, then repeat.
*/
#include <Wire.h>

/**
* Define some useful constants.
*/
#define MD03_ADDRESS 0x58 // The address of the MD03 motor controller
#define MD03_COMMAND_REG 0x00
#define MD03_STATUS_REG 0x01
#define MD03_SPEED_REG 0x02
#define MD03_ACCELERATION_REG 0x03
#define MD03_TEMPERATURE_REG 0x04
#define MD03_MOTOR_CURRENT_REG 0x05
#define MD03_SOFTWARE_REV_REG 0x07
#define MD03_MOVE_FORWARD 0x01
#define MD03_STOP 0x00
#define MD03_MOVE_BACKWARDS 0x02

byte counter;
boolean inte = 1;
boolean inte3 = 1;
boolean inte2 = 1;
boolean inte4 = 1;
boolean flx = 0;
boolean flag = 0; 

void setup() {
 Wire.begin(); // Initialize the I2C bus
 Serial.begin(115200); // Initialize the serial bus
 counter = 0;
 
 pinMode (7, INPUT_PULLUP);
 pinMode (8, INPUT_PULLUP);
 pinMode (5, INPUT_PULLUP);
 pinMode (10, INPUT_PULLUP);

}

void loop() {
 /*
  * Make the motor move forward for 5 seconds.
  */
 Serial.println("Making the motor move forward...");
  inte = digitalRead(7);
   inte2 = digitalRead(8);
   inte3 = digitalRead(5);
   inte4 = digitalRead(10);
   
  if (inte == 0 && inte2 == 0 && inte3 == 0)  
 {

 Wire.beginTransmission(MD03_ADDRESS); // Start communicating with the MD03
 Wire.write(MD03_SPEED_REG);            // Send the address of speed register
 Wire.write(250);                       // Set the speed
 Wire.endTransmission();

 Wire.beginTransmission(MD03_ADDRESS); // Start communicating with the MD03
 Wire.write(MD03_ACCELERATION_REG);     // Send the address of the acceleration register
 //Wire.send(27);                        // Set the acceleration (27 = reach new speed in 1s)
 Wire.write(250);                        // Set the acceleration (60 = reach new speed in 2s)
 Wire.endTransmission();


 }
if (inte == 1 && inte2 == 0 && inte3 == 0 )
 {  // Wait 10 seconds

 Serial.println("Stopping the motor...");

 /*
  * The following decelerates the motor based on the value in the acceleration register.
  */
 Wire.beginTransmission(MD03_ADDRESS);   // Start communicating with the MD03
 Wire.write(MD03_SPEED_REG);              // Send the address of speed register
 Wire.write(MD03_STOP);                   // Set the speed to be zero
 Wire.endTransmission();

 Wire.endTransmission();
  Wire.beginTransmission(MD03_ADDRESS); // Start communicating with the MD03
 Wire.write(MD03_ACCELERATION_REG);     // Send the address of the acceleration register
 //Wire.send(27);                        // Set the acceleration (27 = reach new speed in 1s)
 Wire.write(100);                        // Set the acceleration (60 = reach new speed in 2s)
 Wire.endTransmission();
 
  
 }

 if (inte == 0 && inte2 == 1 && inte3 == 0 )
 {  // Wait 10 seconds

 Serial.println("Stopping the motor...");

 /*
  * The following decelerates the motor based on the value in the acceleration register.
  */
 Wire.beginTransmission(MD03_ADDRESS);   // Start communicating with the MD03
 Wire.write(MD03_SPEED_REG);              // Send the address of speed register
 Wire.write(MD03_STOP);                   // Set the speed to be zero
 Wire.endTransmission();

 Wire.endTransmission();
  Wire.beginTransmission(MD03_ADDRESS); // Start communicating with the MD03
 Wire.write(MD03_ACCELERATION_REG);     // Send the address of the acceleration register
 //Wire.send(27);                        // Set the acceleration (27 = reach new speed in 1s)
 Wire.write(250);                        // Set the acceleration (60 = reach new speed in 2s)
 Wire.endTransmission();
 
    
 }
 
  if (inte == 0 && inte2 == 0 && inte3 == 1)
 {  // Wait 10 seconds

 Serial.println("Stopping the motor...");

 /*
  * The following decelerates the motor based on the value in the acceleration register.
  */
 Wire.beginTransmission(MD03_ADDRESS);   // Start communicating with the MD03
 Wire.write(MD03_SPEED_REG);              // Send the address of speed register
 Wire.write(MD03_STOP);                   // Set the speed to be zero
 Wire.endTransmission();

 Wire.endTransmission();
  Wire.beginTransmission(MD03_ADDRESS); // Start communicating with the MD03
 Wire.write(MD03_ACCELERATION_REG);     // Send the address of the acceleration register
 //Wire.send(27);                        // Set the acceleration (27 = reach new speed in 1s)
 Wire.write(195);                        // Set the acceleration (60 = reach new speed in 2s)
 Wire.endTransmission();
 


 }

  Wire.beginTransmission(MD03_ADDRESS); // Start communicating with the MD03
 Wire.write(MD03_COMMAND_REG);          // Send the address of the command register
 Wire.write(MD03_MOVE_FORWARD);         // Send the forward command
 Wire.endTransmission();
}

Potresti rieditare il tuo messaggio e inserire il codice coi i tag CODE.
Segui le istruzioni al punto 7 --> [REGOLAMENTO] Come usare questa sezione del forum - Italiano - Arduino Forum
Quegli strani pulsanti sopra le faccine non sono messi li a caso. :grin:

**
 * This is a simple test of the MD03 motor controller.  It makes the motor move 
 * forward for five seconds, stop for five seconds, then repeat.
 */
#include <Wire.h>
 
/**
 * Define some useful constants.
 */
#define MD03_ADDRESS 0x58 // The address of the MD03 motor controller
#define MD03_COMMAND_REG 0x00
#define MD03_STATUS_REG 0x01
#define MD03_SPEED_REG 0x02
#define MD03_ACCELERATION_REG 0x03
#define MD03_TEMPERATURE_REG 0x04
#define MD03_MOTOR_CURRENT_REG 0x05
#define MD03_SOFTWARE_REV_REG 0x07
#define MD03_MOVE_FORWARD 0x01
#define MD03_STOP 0x00
#define MD03_MOVE_BACKWARDS 0x02
 
byte counter;
boolean inte = 1;
boolean inte3 = 1;
boolean inte2 = 1;
boolean inte4 = 1;
boolean flx = 0;
boolean flag = 0; 
 
void setup() {
  Wire.begin(); // Initialize the I2C bus
  Serial.begin(115200); // Initialize the serial bus
  counter = 0;
  
  pinMode (7, INPUT_PULLUP);
  pinMode (8, INPUT_PULLUP);
  pinMode (5, INPUT_PULLUP);
  pinMode (10, INPUT_PULLUP);

}
 
void loop() {
  /*
   * Make the motor move forward for 5 seconds.
   */
  Serial.println("Making the motor move forward...");
   inte = digitalRead(7);
    inte2 = digitalRead(8);
    inte3 = digitalRead(5);
    inte4 = digitalRead(10);
    
   if (inte == 0 && inte2 == 0 && inte3 == 0)  
  {
 
  Wire.beginTransmission(MD03_ADDRESS); // Start communicating with the MD03
  Wire.write(MD03_SPEED_REG);            // Send the address of speed register
  Wire.write(250);                       // Set the speed
  Wire.endTransmission();
 
  Wire.beginTransmission(MD03_ADDRESS); // Start communicating with the MD03
  Wire.write(MD03_ACCELERATION_REG);     // Send the address of the acceleration register
  //Wire.send(27);                        // Set the acceleration (27 = reach new speed in 1s)
  Wire.write(250);                        // Set the acceleration (60 = reach new speed in 2s)
  Wire.endTransmission();
 
 
  }
 if (inte == 1 && inte2 == 0 && inte3 == 0 )
  {  // Wait 10 seconds
 
  Serial.println("Stopping the motor...");
 
  /*
   * The following decelerates the motor based on the value in the acceleration register.
   */
  Wire.beginTransmission(MD03_ADDRESS);   // Start communicating with the MD03
  Wire.write(MD03_SPEED_REG);              // Send the address of speed register
  Wire.write(MD03_STOP);                   // Set the speed to be zero
  Wire.endTransmission();
 
  Wire.endTransmission();
   Wire.beginTransmission(MD03_ADDRESS); // Start communicating with the MD03
  Wire.write(MD03_ACCELERATION_REG);     // Send the address of the acceleration register
  //Wire.send(27);                        // Set the acceleration (27 = reach new speed in 1s)
  Wire.write(100);                        // Set the acceleration (60 = reach new speed in 2s)
  Wire.endTransmission();
  
   
  }
 
  if (inte == 0 && inte2 == 1 && inte3 == 0 )
  {  // Wait 10 seconds
 
  Serial.println("Stopping the motor...");
 
  /*
   * The following decelerates the motor based on the value in the acceleration register.
   */
  Wire.beginTransmission(MD03_ADDRESS);   // Start communicating with the MD03
  Wire.write(MD03_SPEED_REG);              // Send the address of speed register
  Wire.write(MD03_STOP);                   // Set the speed to be zero
  Wire.endTransmission();
 
  Wire.endTransmission();
   Wire.beginTransmission(MD03_ADDRESS); // Start communicating with the MD03
  Wire.write(MD03_ACCELERATION_REG);     // Send the address of the acceleration register
  //Wire.send(27);                        // Set the acceleration (27 = reach new speed in 1s)
  Wire.write(250);                        // Set the acceleration (60 = reach new speed in 2s)
  Wire.endTransmission();
  
     
  }
  
   if (inte == 0 && inte2 == 0 && inte3 == 1)
  {  // Wait 10 seconds
 
  Serial.println("Stopping the motor...");
 
  /*
   * The following decelerates the motor based on the value in the acceleration register.
   */
  Wire.beginTransmission(MD03_ADDRESS);   // Start communicating with the MD03
  Wire.write(MD03_SPEED_REG);              // Send the address of speed register
  Wire.write(MD03_STOP);                   // Set the speed to be zero
  Wire.endTransmission();
 
  Wire.endTransmission();
   Wire.beginTransmission(MD03_ADDRESS); // Start communicating with the MD03
  Wire.write(MD03_ACCELERATION_REG);     // Send the address of the acceleration register
  //Wire.send(27);                        // Set the acceleration (27 = reach new speed in 1s)
  Wire.write(195);                        // Set the acceleration (60 = reach new speed in 2s)
  Wire.endTransmission();
  

 
  }
 
   Wire.beginTransmission(MD03_ADDRESS); // Start communicating with the MD03
  Wire.write(MD03_COMMAND_REG);          // Send the address of the command register
  Wire.write(MD03_MOVE_FORWARD);         // Send the forward command
  Wire.endTransmission();
 }
/*******************************************************
*             MD03 example for Arduino                 *
*   MD03 is in I2C mode LCD03 controlled by serial     *
*                                                      *
*             By James Henderson 2012                  *
*******************************************************/

#include <Wire.h>
#include <SoftwareSerial.h>

#define ADDRESS             0x58                    // Address of MD03
#define SOFTREG             0x07                    // Byte to read software
#define CMDBYTE             0x00                    // Command byte
#define SPEEDBYTE           0x02                    // Byte to write to speed register
#define TEMPREG             0x04                    // Byte to read temprature
#define CURRENTREG          0x05                    // Byte to read motor current

#define LCD_RX              0x02                    // Pin for rx
#define LCD_TX              0x03                    // Pin for tx
#define LCD03_HIDE_CUR      0x04
#define LCD03_CLEAR         0x0C
#define LCD03_SET_CUR       0x02

SoftwareSerial lcd_03 = SoftwareSerial(LCD_RX, LCD_TX);  // Sets up serial for LCD03

byte direct = 1; 
boolean inte = 1;
boolean inte3 = 1;
boolean inte2 = 1;
boolean inte4 = 1;
int lopp = 0;
boolean flx = 0;
boolean flag = 0;
// Stores what direction the motor should run in

void setup(){
  lcd_03.begin(9600);                                    // Begin serial for LCD03
  
  Wire.begin();
  delay(100);
  
  lcd_03.write(LCD03_HIDE_CUR);                          // Hides LCD03 cursor
  lcd_03.write(LCD03_CLEAR);                             // Clears LCD03 screen
  
  int software = getData(SOFTREG);                       // Gets software version and prints it to LCD03
  lcd_03.print("MD03 Example  V:");
  lcd_03.print(software);
  pinMode (7, INPUT_PULLUP);
  pinMode (8, INPUT_PULLUP);
  pinMode (5, INPUT_PULLUP);
  pinMode (10, INPUT_PULLUP);
}
 

void loop(){
  inte = digitalRead(7);
  inte2 = digitalRead(8);
  inte3 = digitalRead(5);
  if (inte == 0 && flag == 0 && inte2 == 0 && inte3 == 0)  
  {
  for(int i = 0; i < 250; i=i++){
    sendData(SPEEDBYTE, i);             // Sets speed to i         // Sets motor to direct, a value of 1 runs the motor forward and 2 runs backward
    int temp = getData(TEMPREG);
    inte = digitalRead(7);
    inte2 = digitalRead(8);
    inte3 = digitalRead(5);
    if (inte == 1) {flag =1;flx =1;break;}
    if (inte2 == 1) {flag =1;flx =1;break;}
    if (inte3 == 1) {flag =1;flx =1;break;}    // Gets temperature
               // Wait to make sure all data sent  
  }
   if (flx == 0)
  {
    Serial.println("led tutto acceso");
    sendData (SPEEDBYTE, 250);
    flag = 1;
    flx = 1;
    
  }
  }
   if (inte == 1 && flag == 1 && inte2 == 0 && inte3 == 0 )
  {
    Serial.println("entro nel If di spegnimento");
    for(int i = 0; i > 0; i=i--){
    sendData(SPEEDBYTE, i);             // Sets speed to i         // Sets motor to direct, a value of 1 runs the motor forward and 2 runs backward
    int temp = getData(TEMPREG);
    inte = digitalRead(7);
    if (inte == 0) {flag =0;flx=1;break;}
   }
   if (flx==1)
    {
    Serial.println("led tutto spento");
      sendData (SPEEDBYTE, 0);
      flag = 0;
      flx = 0;

  }
   } 
      if (inte == 0 && flag == 1 && inte2 == 1 && inte3 == 0 )
  {
    Serial.println("entro nel If di spegnimento");
    for(int i = 0; i > 0; i=i--){
    sendData(SPEEDBYTE, i);             // Sets speed to i         // Sets motor to direct, a value of 1 runs the motor forward and 2 runs backward
    int temp = getData(TEMPREG);
    inte = digitalRead(7);
    if (inte2 == 0) {flag =0;flx=1;break;}
   }
    if (flx==1)
    {
    Serial.println("led tutto spento");
      sendData (SPEEDBYTE, 0);   
      flag = 0;
      flx = 0;

  }
   } 
    if (inte == 0 && flag == 1 && inte2 == 0 && inte3 == 1 )
  {
    Serial.println("entro nel If di spegnimento");
    for(int i = 0; i > 0; i=i--){
    sendData(SPEEDBYTE, i);             // Sets speed to i         // Sets motor to direct, a value of 1 runs the motor forward and 2 runs backward
    int temp = getData(TEMPREG);
    inte = digitalRead(7);
    if (inte3 == 0) {flag =0;flx=1;break;}
   }
    if (flx==1)
    {
    Serial.println("led tutto spento");
      sendData (SPEEDBYTE, 0);
      flag = 0;
      flx = 0;

  }
   }
    inte4 = digitalRead(10);
  
  if(inte4 == 1){                       // If loop that swaps value of direct between 1 and 2 each time through loop
    direct = 2;
  }    
   inte4 = digitalRead(10);
  
  if(inte4 == 0){                       // If loop that swaps value of direct between 1 and 2 each time through loop
    direct = 1;
  }                       
}

byte getData(byte reg){                   // function for getting data from MD03
  Wire.beginTransmission(ADDRESS);
    Wire.write(reg);
  Wire.endTransmission();
  
  Wire.requestFrom(ADDRESS, 1);         // Requests byte from MD03
  while(Wire.available() < 1);          // Waits for byte to become availble
  byte data = Wire.read();

  return(data);
}

void sendData(byte reg, byte val){         // Function for sending data to MD03
  Wire.beginTransmission(ADDRESS);         // Send data to MD03
    Wire.write(reg);
    Wire.write(val);
  Wire.endTransmission();
}

l' altro :wink:

Qual'è l'errore :frowning: ?

Ti conviene creare una variabile di stato per il pulsante e controllare quest'ultima per il funzionamento del motore.
Oppure utilizzare una tecnica di debouce.
Esempio --> http://arduino.cc/en/Tutorial/ButtonStateChange

niente da fare continua a non funzionare :frowning:

ma si possono scrivere delle if clause che interagiscano con comandi i2c senza aggiungere componenti particolari ?

Suppongo di si.