Receive for 30msc Low then send LOW for 30 msc and back to HIGH

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

start by reading this: How to get the best out of this forum - Using Arduino / Installation & Troubleshooting - Arduino Forum

Then, edit your post and put it inside code tags. You should also tidy it up with proper formatting (from the IDE, use Ctrl-T)

done Thanks

That is a rather odd (and slow) baud rate. You should really bump that up to something like 115200.

What do you mean by "it is not working"? What do you see on the Serial Monitor?

When I’m trying to put in this space


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?

what video? Analog pins can be used for digital input and output.

This video from Youtube

Are you sure t hat this code matches your requirements?

The IO-pin that is called "PinIn" is switched between OUTPUT and INPUT_PULLUP-mode

Which means the IO-pin changes between low resistance and high resistance (this is called tri-state)

You will have to explain in detail what your project is

Without any documentation that explains what rather special kind of inputs/outputs this is it will be very hard to help you.

I sent video how project looks like one message ago. Thanks

The people in this video speak I guess - russian?
Are you expecting that somebody here will do a translation to english?

The text below the video has no details at all.
No links. no nothing.

If you refuse to write down what your project is all you will get is general avice how to improve your postings.

The link user @blh64 posted has much more information than just "post your code as a code-section".

Maybe you have more luck if you report your thread to a moderator to move your thread to the russian-speaking subforum

I don’t have any more information

I don't believe you.

You can at least say if you want to use the whole thing in the exact same BMW-car for the exact same purpose

if it is a different purpose
You can describe at least what the final purpose of detecting and creating a 30 ms LOW-pulse is used for

You can measure the voltage that comes out of

You can post a picture of this

You can write the name of 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.


I ran your code and added a precision pulse generator to make the 30 ms input.

A 30 millisecond pulse:

launching pulse.
change
done1
terminating pulse.
done2
BL50 done4

and either a 60 or 15 millisecond pulse:
launching pulse.
change
done1
terminating pulse.
done3

What should the printing be? This appears to perform what it looks like it should.

I did see a few 7s where you meant PinIn, fix that but it shouldn't be the problem.

HTH

a7

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;

If it doesn't print

if (StatePinInPrev != StatePinIn) {
        Serial.println("change");

then StatePinInPrev is equal to StatePinIn.

This

makes me ask - how are your pressing the button? How are you managing to make a pulse that it between 20 and 40 milliseconds duration?

I used a pulse generator to make perfect 30 millisecond pulses on demand.

Please post a link to where you got the sketch you are struggling with.

a7

From this video it just shows how it work, so I copied code from video

Puls is made by buttons controller

I saw no code. What are you talking about?

What does this mean? Is the "buttons controller" producing accurate 30 ms pulses?

a7

1 Like

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

Please post a link to this alleged documentation.

And... to the code you are working with. I dod not see any code to copy from the video, what am I missing?

a7