if clause with i2c

hello to everyone. I have a MD03 and I' m trying to use it for control a 24V motor. and I'm trying to use some push button to stop and start it ( if they are all LOW I would like that the motor start. if you press one of the three buttons the motor will stop and the differences between pressing the three different buttons is how long does it take to stop. ). I' m tryng to use i2c comunication for te MD03
I wrote this code but it doesn't work. ( well without the buttons it works)

#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 
#define  ACCELBYTE           0x03   

#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

#define BUTTONAR 5 // ar = arresto rapido
#define BUTTONAN 7 // normale 
#define BUTTONAE 8 // elettrico
#define BUTTONSM 10  // SM= senso di marcaia

int BUTTONARSTATE = 0; // ar = arresto rapido
int BUTTONANSTATE = 0;
int BUTTONAESTATE = 0;
int BUTTONSMSTATE = 0;

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

byte direct = 1; 

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(){
    BUTTONARSTATE = digitalRead(BUTTONAR);
    BUTTONANSTATE = digitalRead(BUTTONAN);
    BUTTONAESTATE = digitalRead(BUTTONAE);
    BUTTONSMSTATE = digitalRead(BUTTONSM);
    

  if (BUTTONARSTATE == HIGH && BUTTONANSTATE == HIGH && BUTTONAESTATE == HIGH)  
  {
    sendData(SPEEDBYTE, 250);  
    sendData(ACCELBYTE, 250);    // Sets speed to i         // Sets motor to direct, a value of 1 runs the motor forward and 2 runs backward
    

  }
  
   else if (BUTTONARSTATE == LOW  && BUTTONANSTATE == HIGH && BUTTONAESTATE == HIGH )
  {
  sendData(SPEEDBYTE, 0); 
  sendData(ACCELBYTE, 100);  // Sets speed to i         // Sets motor to direct, a value of 1 runs the motor forward and 2 runs backward
   } 
      
   else if (BUTTONARSTATE == HIGH && BUTTONANSTATE == LOW && BUTTONAESTATE == HIGH )
  {
    sendData(SPEEDBYTE, 0); 
    sendData(ACCELBYTE, 196);    // Sets speed to i         // Sets motor to direct, a value of 1 runs the motor forward and 2 runs backward
   } 
    
  else if ( BUTTONARSTATE == HIGH && BUTTONANSTATE == HIGH && BUTTONAESTATE == LOW)
  {
    sendData(SPEEDBYTE, 0); 
    sendData(ACCELBYTE, 250);    // Sets speed to i         // Sets motor to direct, a value of 1 runs the motor forward and 2 runs backward
   
   }
  
  if(BUTTONSMSTATE == HIGH){                       // If loop that swaps value of direct between 1 and 2 each time through loop
    direct = 2;
  }    
  
  else if(BUTTONSMSTATE == LOW) {                       // If loop that swaps value of direct between 1 and 2 each time through loop
    direct = 1;
  } 
    sendData (CMDBYTE, direct);  
}

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();
}

Wire.endTransmission(); returns an int, can you check its value?

do you use pull ups for the I2C lines? (4k7) 4700 ohm would be good.

    BUTTONARSTATE = digitalRead(BUTTONAR);
    BUTTONANSTATE = digitalRead(BUTTONAN);
    BUTTONAESTATE = digitalRead(BUTTONAE);
    BUTTONSMSTATE = digitalRead(BUTTONSM);

By convention, all capital letter names are reserved for constants. Constants NEVER appear on the left side of an equal sign (except, of course, when being assigned an initial value).

How are the switches actually wired? Why are you not using the internal pullup resistors and connecting one leg to ground and one leg to the digital pin (and expecting LOW to mean pressed)?

ok :slight_smile: what do you mean with internal pull up ? I wrote PULL_UP thinking that in this way it cuold mean pressed with LOW . I would like to add that the code without all the part for the buttons works. so I think the problem could be in the buttons' code or it cuold be the comunication with i2c with if clause
what do you think ?
( because I need it really urgently , I would be so happy if someone could write for me a working code
Really really thank you to all :slight_smile: )

what do you mean with internal pull up ?

The Arduino has internal pullup resistors. Enable them by setting the pin mode to INPUT_PULLUP.

oky :slight_smile: I' ve done it but nothing change :frowning:

oky :slight_smile: I' ve done it but nothing change :frowning:

You need to post a wiring diagram, your current code, and your serial output. If you don't have any, why the hell not?

this is my current code

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

#define ADDRESS     0x58
#define CMDBYTE     0x00
#define SPEEDBYTE   0x02
#define ACCELBYTE   0x03
    
int inPin1 = 5;
int inPin2 = 7;
int inPin3 = 8;
int inPin4 = 10;

boolean valar = 0;
boolean valan = 0;
boolean valae = 0;
boolean valsm = 0;

byte direct = 1;

boolean flx = 0;
boolean flag = 0;

void setup(){
  Wire.begin();
  delay(100);
  
  pinMode (inPin1, INPUT_PULLUP);
  pinMode (inPin2, INPUT_PULLUP);
  pinMode (inPin3, INPUT_PULLUP);
  pinMode (inPin4, INPUT_PULLUP);
}

void loop(){
  int i = 0;
  int a = 0;
  
  valar = digitalRead(inPin1);
  valan = digitalRead(inPin2);
  valae = digitalRead(inPin3);
  valsm = digitalRead(inPin4);
  
  if(valar == 0 && flag == 0 && valan == 0 && valae == 0){
    i = 250;
    a = 250;
    if (valar == 1) {flag ==1; flx ==1;}
    if (valan == 1) {flag ==1; flx ==1;}
    if (valae == 1) {flag ==1; flx ==1;}
 if (flx == 0) {
  flag = 1;
  flx = 1;
 } 
  }
  
  else if(valar == 1 && flag == 1 && valan == 0 && valae == 0){
    i = 0;
    a = 100;
    if (valar == 0) {flag =0; flx =1;}
    if (flx == 1){
      flag = 0;
      flx = 0;
    }
  }
  else if(valar == 0 && flag == 1 && valan == 1 && valae == 0){
    i = 0;
    a = 196;
    if (valan == 1){flag =0; flx =1;}
    if ( flx == 1){
      flag = 0;
      flx = 0;
    }
  }
  else if(valar == 0 && flag == 1 && valan == 0 && valae == 0){
    i = 0;
    a = 250;
    if (valae == 1){flag =0; flx =1;}
    if ( flx == 1){
      flag = 0;
      flx = 0;
    }
  }
  
  sendData(SPEEDBYTE, i);
  sendData(ACCELBYTE, a);
  sendData(CMDBYTE, direct);
  delay(10);
  
  if(valsm == HIGH){
    direct = 1;
  }
  else if(valsm == LOW){
    direct = 2;
  }
}

 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();
 }

do you use pull ups for the I2C lines? (4k7) 4700 ohm would be good.

I don't think you understood that.

You connect a resistor from analog input 5 to +5V, you do the same for input 4. These are called pull up resistors. They go on the I2C lines.

You need to post a wiring diagram, your current code, and your serial output.

One out of three is not good enough.