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