A project with multiple components: switch, buzzer, thermocouple, lcd.. [CLOSED]

Hi all!

Expanding from my original problem with thermocouples, I have managed to program and get each individual component to work on Mega 2560 as an individual component only i.e.

  1. Switch (pushbutton) with dual LEDs
  2. Buzzer
  3. LCD Shield
  4. K-type thermocouple

The problem comes when I put everything together.

What I am trying to do:

  1. Starts off with a “main screen” with date and time and a static message
  2. When press and hold ‘SELECT’ button for LCD shield, it enters Menu mode to change settings, etc. and then when press and hold ‘SELECT’ again, it returns to main screen
  3. The switch is separate from the menu and when the button is pushed and held for 5 seconds, the red light comes on, after x secs, it changes green and then turns off (buzzer incorporated)
  4. Thermocouple keeps reading the temperature
  5. All this working in parallel (kind of) i.e. i don’t time or temp to stop reading while switch is in operation

The problems I am having:

  1. I can’t use the LCD buttons to navigate because I believe they might be clashing with other variables/definitions which I can’t figure out
  2. The switch goes red/green without pressing anything?!
  3. When I use the switch, everything else stops?! (i know it’s a single core board but there must be some way around it) I have already tried MILLIS tutorial but that doesn’t work

Advise and help is much needed and appreciated. Coding is not my strength, I have just started learning it.

mark18_20180312.ino (15.2 KB)

void loop() {
charaCreate();

How many times do you need to call charaCreate()? Do you understand the difference between setup() and loop()?

void thermocouple() {
  Serial.print("Deg C = ");
  Serial.print(ktc.readCelsius());
  Serial.print("\t Deg F = ");
  Serial.println(ktc.readFahrenheit());
  delay(1000);
}

This function should be called readThermoCoupleTwiceBecauseIDontKnowWhichUnitOfMeasureToUseANDThenStuffHeadInSand().

Or, make the function smarter.

      buttonTimer = millis();

The millis() function does NOT return a timer. It returns a TIME. The value is NOT a long. It is an unsigned long.

Your picture is about useless. Wires crossing over each other are hard to follow. Post a schematic, instead.

I suspect that your LED_BUTTON (dumb name for a pin that a switch is connected to) is floating, because you do not use the internal pullup resistor, and I can't tell if there is a resistor anywhere in the circuit.

farhanengr: 1. I can't use the LCD buttons to navigate because I believe they might be clashing with other variables/definitions which I can't figure out

When writing the separate programs to try things out make sure that no two programs use the same pin for different things or, for example, use the same HardwareTimer.

Then when you combine the programs you should not have any conflicts.

I see a huge number of delay()s in your program. I reckon you need to get rid of ALL of them if you want a responsive program. Have a look at how millis() is used to manage timing without blocking in Several things at a time.

You probably also need to get rid of WHILE as that also blocks other things from happening.

I see several examples like this

 while (activeButton == 0) {
    int button;
    readKey = analogRead(0);

if this was my project I would do all the reads in one place, not scattered throughout the program.

Have a look at Planning and Implementing a Program. Design your program so that it is easy to test.

It is not a good idea to wait until you have 15k of code to start debugging. When you want to combine programs start by combining 2 of them and get that working before you try to combine a 3rd program.

...R

First of all, thank you PaulS for your reply. You have been quite blunt in stating your opinions but I guess that's how one learns. As I mentioned in my first post, I don't have programming background and that's why many things may seem out of the world ;)

PaulS: How many times do you need to call charaCreate()? Do you understand the difference between setup() and loop()?

I do know the difference and my apologies for putting that into the loop section, I wasn't thinking right at the time of putting the code together for the post. :)

PaulS: This function should be called readThermoCoupleTwiceBecauseIDontKnowWhichUnitOfMeasureToUseANDThenStuffHeadInSand().

I think you are ''too quick to judge" over here. I DO NEED both units since the customer is in US and I am in Australia but thanks for the opinion :)

PaulS: Or, make the function smarter.

The millis() function does NOT return a timer. It returns a TIME. The value is NOT a long. It is an unsigned long.

I see what you are saying but if you go back and read the code it is not a timer, it's a time in ms, so millis is the correct function, however variable name may not be appropriate.

PaulS: Your picture is about useless. Wires crossing over each other are hard to follow. Post a schematic, instead.

Yes you are right, I don't know why I posted the picture maybe I was rushing yesterday and didn't have time to do a proper labelled pic or a 2D schematic.

PaulS: I suspect that your LED_BUTTON (dumb name for a pin that a switch is connected to) is floating, because you do not use the internal pullup resistor, and I can't tell if there is a resistor anywhere in the circuit.

Variable name huh? look mate I name the variable according to what I can understand them by not what the programming world thinks. Obviously you guys are the experts but I am learning still and thanks for your opinion again :)

Whether i use the pullup resistor or not, it works (in standalone mode) perfectly fine and there is no difference when I do use the pullup or I don't. Secondly, I don't have any resistors anywhere on my entire system.

So, apart from opinions about my coding practices ;) what's your input on the program/connections? Do you think i need resistors? What am i doing wrong?

Robin2 thank you for your reply.

Robin2: When writing the separate programs to try things out make sure that no two programs use the same pin for different things or, for example, use the same HardwareTimer.

Then when you combine the programs you should not have any conflicts.

Robin, I did make sure all pins are separate and I also did a combination of 2 programs then add another one and so on. However, I think HardwareTimer and RTC are the problems. As soon as I put the RTC together, everything stuffs up.

Robin2: I see a huge number of delay()s in your program. I reckon you need to get rid of ALL of them if you want a responsive program. Have a look at how millis() is used to manage timing without blocking in Several things at a time.

Yes you are right, I will work through the program to remove delays and also will look at the link you suggested.

Robin2: You probably also need to get rid of WHILE as that also blocks other things from happening.

I see several examples like this

 while (activeButton == 0) {
    int button;
    readKey = analogRead(0);

if this was my project I would do all the reads in one place, not scattered throughout the program.

Have a look at Planning and Implementing a Program. Design your program so that it is easy to test.

The reason I haven't touched that is because when I use other conditions, the switch doesn't work the way i want it to work. Maybe I am unable to interpret the condition in a different way ??!!

Robin2: It is not a good idea to wait until you have 15k of code to start debugging. When you want to combine programs start by combining 2 of them and get that working before you try to combine a 3rd program.

...R

True that but when you have a deadline closing in and pressure building up, you just do what you gotta do lol... but i see what you are saying and i agree :)

so millis is the correct function, however variable name may not be appropriate.

The variable type isn't, either. That was my point.

Do you think i need resistors?

Yes, I do. The internal ones are perfectly good, if you use them.

I like to see Pin in the name of variables that hold pin numbers, and State in the name of variables that hold values returned by digitalRead().

It's way to easy to compare the pin variable to HIGH or LOW, when you meant to compare the state variable to HIGH or LOW, if the names don't clearly indicate which is which. THAT is why I harp about names.

PaulS:
The variable type isn’t, either. That was my point.
Yes, I do. The internal ones are perfectly good, if you use them.

I like to see Pin in the name of variables that hold pin numbers, and State in the name of variables that hold values returned by digitalRead().

It’s way to easy to compare the pin variable to HIGH or LOW, when you meant to compare the state variable to HIGH or LOW, if the names don’t clearly indicate which is which. THAT is why I harp about names.

Thank you PaulS, now it makes sense :slight_smile:

You are the best 8)

When you talk about internal resistors, you mean I need INPUT_PULLUP or OUTPUT_PULLUP commands on all the defined pins??

PaulS: The variable type isn't, either. That was my point.

Got it. So what you are saying is that any variable that has to do with TIME, must be unsigned long because it can not be negative?!

I have attached a quick schematic of what i am doing.

New code with very few mods as was advised. Still has the same issues as the original problem. :frowning:

Any other ideas?

Can someone check the ANALOG BUTTONS for lcd coding. It has been defined twice, once for the menu and other for the normal LCD example. I am struggling to combine both.

Also, can someone check if RTC clashes with any delays or millis used in the code?

Thank you all.

schematic_20180313.pdf (214 KB)

mark19_20180313.ino (15.3 KB)

farhanengr: True that but when you have a deadline closing in and pressure building up, you just do what you gotta do lol..

Especially then it is important to make progress in small steps so that problems can be fixed quickly.

I also did a combination of 2 programs then add another one and so on. However, I think HardwareTimer and RTC are the problems

That suggests that there was a version that was working and the next version (with an extra part added) which was not working.

Can you post the working and non-working versions at that point so we can see the changes that probably caused the problem.

...R

Robin2:
Can you post the working and non-working versions at that point so we can see the changes that probably caused the problem.

Hi Robin, thanks for your reply. Sorry for late reply.

Yes and no. What you see in the code is the actual working code. Instead of re-writing the new code (combined version), I simply copy and pasted the working code. Once copied and pasted, it doesn’t work which I think it might be clashing with each other.

I am not very good with ‘programming logic’ ie. ands and ifs and elses. I guess that’s where the problem is.

So for example if you copied and pasted all the relevant commands for menu from my code, they will work fine on their own.

If you still want me to post all the single programs, let me know.

Cheers,
MF

farhanengr:
Yes and no. What you see in the code is the actual working code.

Which two programs are you referring to - which Posts are they in?

…R

Robin2: Which two programs are you referring to - which Posts are they in?

...R

Mark 18 and 19 (posts 1 and 9 respectively), both are the same program with no major change. I just changed the variable names and changed time variables from long to unsigned long.

They way I setup the VOID LOOP is such that you can run each individual program. Comment and uncomment as desired.

Thermocouple - program 1 - it reads temp in C and F on serial comm

Main Button - program 2 - hold the pushbutton for 5 secs to turn on the red led and buzzer, it will automatically change to green led with buzzer sound and then both leds go off

TimeSerial and TimeLCD - program 3 - this is the RTC program to print on serial comm and lcd display 16x2

switch screen - program 4 - i want to switch between main screen (could be any text) to the menu screen

If you want to run just the menu, you can initiate program called "screenB()" in void loop section.

farhanengr: Mark 18 and 19 (posts 1 and 9 respectively),

Posts are not numbered like that. You mean your Original Post and Reply #8

There are more than 600 lines of code in those programs. I'm not going to trawl through all of that looking for changes. If you want help please post a list of the lines with differences.

...R

Robin2: Posts are not numbered like that. You mean your Original Post and Reply #8

There are more than 600 lines of code in those programs. I'm not going to trawl through all of that looking for changes. If you want help please post a list of the lines with differences.

...R

Hi Robin, sorry for late reply.

I have some other important tasks that came up, I will upload the sketches asa i get a chance.

Cheers, M

Hi all,

Thank you everyone for your help.

Unfortunately I had to handover the project to the experts due to strict deadlines, so I don't have any control.

I still want to learn and I still have the code and will look into it in near future when I have sometime to try and workout what was going wrong but as for now, hardware is gone.

Admin, can you please close this thread/topic.

Cheers, Muhammad

farhanengr: Admin, can you please close this thread/topic.

Threads are not normally closed. However if you edit your Original Post you can add the word [CLOSED] to the title. Please do not delete the title.

Sorry to hear that shortage of time got the better of you.

...R

Anyone who is reading this post, just a quick update/tip on menu issue:

working on a similar program for spot welder, similar setup and same interface i.e.

main screen displaying info > click a button to enter menu > navigate and change values within the menu > click a button to return to main screen

I resolved this issue by combining the double definition of LCD shield buttons on pin A0. The code I posted initially have LCD shield buttons defined twice with different variable names and it just got too confusing to work with. Once they are combined, it is relatively easy to debug and make it work.

Just an FYI.

I can't post the spot welder code here for public use but if someone is struggling with a similar issue, maybe I can help them - fingers-crossed as I am a beginner :)

Thank you all for your support and help - much appreciate it :)