I am having a hard time writing/understanding code, anyone willing to help?

Lost is where I am. I have watched a bunch of videos and tutorials, done a number of exercises and I just don't understand. Im at the point I just want someone to write the code for this project I am working on as it seems like I am NEVER going to get this figured out. I am so frustrated that I am at the point of saying screw trying to learn Arduino code.

Anyone else been there? What did you do? How did you learn? What is the best way to learn how to write code? I did all the free tutorials on Programing Electronics Academy which was a HUGE help. I took my time and did the exercises but even though the videos were done very well and I did understand what I was learning, I just don't know where to even start with writing the code for my project. I know what I need it to do, just don't know how to take that info and turn it into Arduino code.

If anyone is willing to give me some much needed help and at least guide me on this one project so I can finish it, learn from it, and move on, that would be greatly appreciated!!! please message me, or you can reach me at diebog1 attgmaledotcom

Thanks,

Jeremy

So what does your project need to do? Can you write that out as a series of instructions so that any random person on the internet can do the task?

Now translate the English instructions into C code.

You should remember that your random person is much much faster than you are. A millisecond is a long time for that person. You can have them do stupidly monotonous tasks like looking at the clock on the wall thousands of times per second and only performing an action once a week.

Ok Ill try

I am using an infrared beam break QT50CM sensor that I need to monitor and then send ground signals to 2 buttons. Button "A" is ON and button "B" is Open The device I am making this for has a function and to turn it on button A needs to be held down while you press button B, after this both buttons are released. This turns on the function and to turn it off you push either button, pretty simple.

I have 3 functions I need to the Arduino to do,

First:

When the beam on QT50CM is broke for longer than say 20 seconds or so, I need a ground signal sent to button A and held until another ground signal is sent to button B. After this has happened both signals need to be turned off. As long as the beam stays broken nothing else happens as the function is "ON".

Second:

If the beam is reconnected, a ground pulse would be sent to either button which turns off the function.

3rd:

Not a must but it would be nice if I could light up a status led when this function is turned on. Again not a must, and I can always add it in later on.

Try building the hardware so you can code with it through trial and error, you'll figure it out. It's theorical at this point, cart before the horse situation.

diebog:
and then send ground signals to 2 buttons.

That sounds completely back to front.

Normally nobody sends anything to a button switch. A button switch is used to send information to the Arduino - to tell the Arduino that the user has pressed the button.

Start by thinking of your project as a series of separate parts. Get a button switch and write a short Arduino program that can display a message on the Serial Monitor when the user pushes a button.

Then write a short program that can display a message when the beam is broken.

Only when you can do the parts separately should you think about combining them. And if you break the project into small parts like that it will be much easier to get help here if you don't understand something.

...R
Planning and Implementing a Program

MorganS:
So what does your project need to do? Can you write that out as a series of instructions so that any random person on the internet can do the task?

Now translate the English instructions into C code.

You should remember that your random person is much much faster than you are. A millisecond is a long time for that person. You can have them do stupidly monotonous tasks like looking at the clock on the wall thousands of times per second and only performing an action once a week.

Morgan is right.

In "the old days" we were taught to create a logical Flow Chart in which you write in plain English what is supposed to happen in the program. (The Flow Chart was vital when writing in Assembly Language).

You then take each section and write the code that does the job.

It also helps to keep if - else coding tidy & readily understandable.

Having said that, there is nothing like getting a copy of Stephen Davis's "Programming with C++ for Dummies" or similar and working through it. I'm still picking it up after 3 months slogging through his worked examples.

And there is "SAMS Teach Yourself C in 21 Days" . . . . provided you realise it is three 40-hour weeks worth!

Hi,
I think the OP is trying to interface the sensor with a device that has two buttons.

The Arduino will probably via two relays, having their outputs in associated with the buttons, provide the correct combination of button presses needed when the sensor is activated.

Tom... :slight_smile:

Hi,
OPs sensor, i think the LED orientation is a typo mistake.

Tom... :slight_smile:

Hi,
Can you post a diagram, hand drawn if you like, of the A and B button wiring to the unit you are controlling?

What is the application, the device the buttons are connected to?

Thanks.. Tom.. :slight_smile:

TomGeorge:
Hi,
I think the OP is trying to interface the sensor with a device that has two buttons.

The Arduino will probably via two relays, having their outputs in associated with the buttons, provide the correct combination of button presses needed when the sensor is activated.

Tom... :slight_smile:

Tom is correct. I am using the sensor to control a devices 2 buttons. There is a feature in this device that is activated by holding the ON button and then pressing the Open button. I need to replicate this action.

I did do this example on Adafruit but that is as far as I have gone

And ya the led on that schematic I am not using. I have no idea what its there for

The buttons I am controlling have relays already setup. The buttons work by applying a ground signal to the IC on the device. So all I need to do is write a low output on 2 of the digital pins of the Arduino setup to imitate physically holding on and pressing open.

But first you need to know the voltage you are connecting to the Arduino's I/O pins, if it's a 5V Arduino, you can't put 12V on it, 5V max, and you can't allow current more than about 25 mA to flow into it for very long.

diebog:
If the beam is reconnected, a ground pulse would be sent to either button which turns off the function.

Which button? The Arduino needs you to specify which one. You can say "both" if you like.

MorganS:
Which button? The Arduino needs you to specify which one. You can say "both" if you like.

It has to be 2 separate signals. The first would act like holding the on button, and then once that is done the second signal would act as pressing the open button. If both signals were sent at the same time the function will not turn on. The on button needs to be held 1 second or so and then the open button would be pressed. Does that make any more sense?

diebog:
It has to be 2 separate signals. The first would act like holding the on button, and then once that is done the second signal would act as pressing the open button. If both signals were sent at the same time the function will not turn on. The on button needs to be held 1 second or so and then the open button would be pressed. Does that make any more sense?

No. A button is an input device - you don't send signals to it to "control" it. I believe that your problem with programming is that you do not have the logic completely figured out, and that is necessary before writing a program.

Reduce the problem to a single switch. This switch will represent the beam break sensor. When the beam is unbroken, the sensor will register HIGH (it could also register LOW - this is not a problem. Assume HIGH for now.) So wire a switch: connect one side of the switch to pin 5 (for example), and the other side to GND.

Assuming you are using an Arduino Uno, a sketch to test this switch is like this:

void setup() {
  pinMode(5, INPUT_PULLUP);
  pinMode(13, OUTPUT);
  }

void loop() {
  digitalWrite(13, digitalRead(5));
  }

Then wire up two more switches just like that one, use push button switches, and put them on two other digital pins (such as pins 6 and 7.) Practice writing code that reads all three switches, but only lights the LED if a particular combination of switches are pressed. This will give you practice in designing logic patterns in the states of the switches. For example write a sketch that turns the LED on if Switch 1 and 3 are closed, but switch 2 is open. Or one that doesn't care about the state of switch 2, or ... test a lot of different combinations until you get the hang of using logical combinations.

Learning to write code is a process, You need to actually do all the steps. Don't just acknowledge them and assume you will be able to use them from reading it through. Write lots of practice code.

diebog:
The buttons I am controlling have relays already setup.

Do you (by any chance) mean that you already have some device (that you have not told us about) which is controlled by pressing one or two buttons and you want to use the Arduino to activate that device rather than using human fingers to press the buttons?

...R

:fearful: :fearful:

I keep forgetting about that "didn't mention a major part of the project" thing. That's probably the second most popular info gap, right after the XY problem. If that's the case, then "controlling a button" makes sense.'

@diebog - is that the case? If so, then what electro-mechanical device are you using, a solenoid, or some other type of linear motor?

I am using an infrared beam break QT50CM sensor that I need to monitor and then send ground signals to 2 buttons. Button "A" is ON and button "B" is Open The device I am making this for has a function and to turn it on button A needs to be held down while you press button B, after this both buttons are released. This turns on the function and to turn it off you push either button, pretty simple.

ok, so your two outputs go to buttons on some device. To turn the button off you set the output pin to pinMode(pin, INPUT), which makes the pin float, and to turn the button on you set the output pin to pinMode(pin, OUTPUT), digitalWrite(pin, LOW).

So your problem is in getting the timing logic going.

We rely on the fact that loop() gets spammed many thousands of times a second.

At any given moment, your sketch can be doing one of three things: it can be waiting for the beam to break, it can have pressed button A and be waiting until it's time to press button B, and it can have pressed both B and A and be waiting to release them.

Most of the time, loop() does nothing. But occasionally an event happens in its environment (the beam, the time) to which it must respond.

enum State {
  IDLE, ON, OPEN
} state = IDLE;

uint32_t start_time; // the time at which we entered the current state
          // we don't bother with start_time in IDLE state, but ON and OPEN are timed

const uint32_t ON_TIME = 2000; // wait two seconds for power up
const uint32_t OPEN_TIME = 500; // press the open switch for half a second

void setup() {
  float button A
  float button B
  state = IDLE;
}

void loop() {
  switch(state) {
  case IDLE:
    if(beam is broken) {
      start_time = millis();
      ground button A
      state = ON;
    }
    break;

  case ON: 
    if(millis()-start_time >= ON_TME) {
      start_time = millis();
      // don't need to ground button A - it's already grounded.
      ground button B
      state = OPEN;
    }
    break;

  case OPEN:
    if(millis()-start_time >= OPEN_TME) {
      float button A
      float button B
      state = IDLE;
    }
    break;

  }
}

like that.

There might be some more logic to do with if the beam is still broken when the OPEN phase times out. Maybe OPEN state should be:

  case OPEN:
    if(millis()-start_time >= OPEN_TME and button B is not floating) {
      float button B;
    }

    if(millis()-start_time >= OPEN_TME and the beam is not broken) {
      float button A;
      state = IDLE;
    }
   break;

Alternatively, you might want to introduce another state for "I have opened the door, but the beam is still broken", and another one for "I have closed the door, and I am not going to open it again for another 5 seconds irrespective of the state of the beam".

Oh incidentally, my code above has a bug. What happens if the open time clicks over after the first if() statement but before the second one? How would you fix it?

Robin2:
Do you (by any chance) mean that you already have some device (that you have not told us about) which is controlled by pressing one or two buttons and you want to use the Arduino to activate that device rather than using human fingers to press the buttons?

...R

Yes that is correct. I should of linked THIS before. I posted it but realized I was asking the wrong questions. But it probably explains more

ChrisTenone:
:fearful: :fearful:

I keep forgetting about that "didn't mention a major part of the project" thing. That's probably the second most popular info gap, right after the XY problem. If that's the case, then "controlling a button" makes sense.'

@diebog - is that the case? If so, then what electro-mechanical device are you using, a solenoid, or some other type of linear motor?

Sorry for the confusion, I forget everyone doesnt know what I am thinking and what I say might not make sense. I really appreciate all the help everyone is giving!