Hi all, I'm using Arduino Mega R3. can some one help me with this problem:
So I have one button with controller and it has LED, in original documentation is write that controller is all time on HIGH when you press button unit is sending for 30 milliseconds LOW signal then I need to receive it and send back from Arduino for 30 milliseconds same LOW signal and put it back to HIGH.
I found this code on internet but isn't working on my Arduino. Can someone check and give me any feedback. Thanks
class Channel {
public:
Channel(int PinIn, int PinOut) {
this->PinIn = PinIn;
this->PinOut = PinOut;
}
int PinIn;
bool StatePinInPrev;
int PinOut;
int state = 0;
long startTime;
void init() {
int StatePinIn = StatePinInPrev = HIGH;
pinMode(7, INPUT_PULLUP);
pinMode(PinOut, OUTPUT);
}
int readIn() {
return digitalRead(7);
}
void busToLow() {
pinMode(PinIn, OUTPUT);
digitalWrite(PinIn, LOW);
}
void busToHigh() {
digitalWrite(PinIn, HIGH);
pinMode(PinIn, INPUT_PULLUP);
}
void outputOn() {
digitalWrite(PinOut, HIGH);
}
void outputOff() {
digitalWrite(PinOut, LOW);
}
void tick() {
int StatePinIn = readIn();
if (StatePinIn != HIGH) {
Serial.println("1");
}
if (state == 0) {
//Serial.println(StatePinIn);
if (StatePinInPrev != StatePinIn) {
Serial.println("change");
StatePinInPrev = StatePinIn;
if (StatePinIn == LOW) {
startTime = millis();
state = 1;
Serial.println("done1");
}
}
} else if (state == 1) {
if (StatePinInPrev != StatePinIn) {
StatePinInPrev = StatePinIn;
if (StatePinIn == HIGH) {
long currentTime = millis();
long delta = currentTime - startTime;
if (delta >= 20 && delta <= 40) {
state = 2;
outputOn();
Serial.println("done2");
} else {
state = 0;
outputOff();
Serial.println("done3");
}
}
}
} else if (state == 2) {
long currentTime = millis();
long lastTime = 30 + 20;
if (currentTime - startTime >= lastTime) {
Serial.print("BL" + String(lastTime) + " ");
busToLow();
state = 3;
Serial.println("done4");
}
} else if (state == 3) {
long currentTime = millis();
long lastTime = 30 + 20 + 30;
if (currentTime - startTime >= lastTime) {
busToHigh();
state = 0;
}
}
}
};
Channel leftButton(7, 8);
void setup() {
Serial.begin(9700);
Serial.println("hello");
leftButton.init();
while (leftButton.readIn() == LOW)
;
Serial.println("Started");
}
void loop() {
leftButton.tick();
delay(1);
}
if (StatePinInPrev != StatePinIn) {
Serial.println("change");
It doesn’t show me nothing after pressing buttons. So I think program doesn’t see when button is pressed. But on video they used analog pin and I’m using digital one, can be issued by this?
Ok, yes it same like on video( it is original massage button with indicator). Wire from this button has with engine on 12v, when you press in it’s going to ground for 30msc then original unit need to switch on massage ang give answer ground for 30msc. If unit doesn’t give feedback button think that is going something wrong and doesn’t switch on indicator.
I have vibro motors in the seats which is working with arduino, so I want when I press this button to start vibro motors and switch on indicator.
Can you explain me, when I’m reading digitalRead(7),and printing in port it show me that when I press buttons that it gone to Low,
But when I’m trying to print it on part code is nothing printing, why?
if (StatePinInPrev != StatePinIn) {
Serial.println("change");
StatePinInPrev = StatePinIn;
Yes. On documentation of bmw it says “ button unit after pressing button send Low for 30ms to massage unit. Thank massage unit switch on massage and sending back same low signal for 30ms and indicator on button going to on, I want to use this button without massage unit, i want to do massage unit from arduino