Anyone good with arduino or C language?

Discussion in 'Tech Discussion' started by Mr Pancakes, Apr 17, 2018.

  1. Mr Pancakes

    Mr Pancakes Well-Known Pancake

    Joined:
    May 9, 2016
    Messages:
    670
    Likes Received:
    326
    Reading List:
    Link
    I'm working on a project, and I need help with a part of my code. It's supposed to read data from sensors and then display it on an LCD 16x4. I am using an ir remote to control that , so when I press "1" , it displays the temp , when I pres"2" it displays the humidity and so on. My problem is that it doesnt update in real time.I found out I have to use some functions to do that but idk how. I also posted this on stackowerflow but got no answers. I need to finish this today.
    Here is the code

    Code:
    #include <dht.h>
    
    #include <LiquidCrystal.h>
    #include <IRremote.h>
    #include <IRremoteInt.h>
    LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
    
    dht DHT;
    int RECV_PIN = 10;         
    IRrecv receiver(RECV_PIN); 
    decode_results results;
    #define DHT11_PIN 9
    int vodaPin = A5;
    int voda;
    int photocellPin = A0;
    int svjetlo;
    const int numReadings = 7;
    int readings[numReadings];
    int readIndex = 0;
    int total = 0;
    int average = 0;
    
    void svjetlost(){
     svjetlo = analogRead(photocellPin);
    
          delay(10);
          lcd.setCursor(0, 0);
          lcd.print("Svjetlost:");
          lcd.print(svjetlo);
           lcd.noDisplay();
          delay(100);
          lcd.display();
          delay(500);
        
    }
    
    void setup() {
    
      Serial.begin(9600);
      lcd.begin(16, 2);
      lcd.begin(16, 2);
    
      receiver.enableIRIn();
    }
    
    void loop()
    {
      if (receiver.decode(&results)) {         
    
        if (results.value ==  0xFF30CF ) {
    
        
    
        }
        if (results.value == 0xFF18E7) {
    svjetlost();
    
        }
    
        if (results.value == 0xFF7A85) {
     
      
        
        }
    
        receiver.resume();
      }
    }

    This only shows the data in the moment the button is pressed and it doesnt change until its pressed again.
     
  2. Drake98

    Drake98 Concerned Fan

    Joined:
    Jan 23, 2016
    Messages:
    1,553
    Likes Received:
    890
    Reading List:
    Link
    may stackeroverflow bless you!!
     
    Ltsandwich and gheist1234 like this.
  3. Valcryst

    Valcryst 『Theorizing Panda』 ʕ-ᴥ – ʔ 『Ddraigslayer』 ︻デ═一

    Joined:
    Jan 12, 2018
    Messages:
    954
    Likes Received:
    1,623
    Reading List:
    Link
    I've mostly forgotten my c... ._.
    But don't you need a timer of some sort to display realtime stuff?
    Sorry i can't be of more help ._.
     
  4. Mr Pancakes

    Mr Pancakes Well-Known Pancake

    Joined:
    May 9, 2016
    Messages:
    670
    Likes Received:
    326
    Reading List:
    Link
    I only got 2 downvotes and an edit. And since I got downvotes I can't even post sh!t again for 7 days
     
  5. Darkere

    Darkere Active Member

    Joined:
    Oct 20, 2015
    Messages:
    8
    Likes Received:
    4
    Reading List:
    Link
    Updating in real time really only means update often enough. Instead of doing it once you need to write a loop that checks the sensor every second or so. Can't help you with the code too much cause that looks like a nightmare to me but there is a method that is already called loop but doesn't do any looping at all?
     
    Mr Pancakes and Nyarlathotep like this.
  6. madpinger

    madpinger Well-Known Member

    Joined:
    May 8, 2016
    Messages:
    67
    Likes Received:
    40
    Reading List:
    Link
    On mobile, so short no code.

    When you press a button on the remote you need to just set a flag or variable, etc and the main loop just blindly polls and updates the lcd based on which value is set via the remote. Most simple way ^_^
     
    Nyarlathotep and Mr Pancakes like this.
  7. Valcryst

    Valcryst 『Theorizing Panda』 ʕ-ᴥ – ʔ 『Ddraigslayer』 ︻デ═一

    Joined:
    Jan 12, 2018
    Messages:
    954
    Likes Received:
    1,623
    Reading List:
    Link
    From what I am starting to remember, you're currently doing:
    Whenever you push the button you call the function to show the desired display.
    Looping in that function to show current data won't work because the loop is only called after pressing the button.

    You need a function that handles what is being displayed, loops it with a real time timer... i believe there is a in built function somewhere you can use... i forget...
    You just have to set the function to display temp, humidity, etc with a variable holder of some sort...

    *sigh*
    I don't think i explained that well...
     
    Mr Pancakes likes this.
  8. XD

    XD Well-Known Member

    Joined:
    Feb 6, 2017
    Messages:
    536
    Likes Received:
    1,074
    Reading List:
    Link
    Ahh, I don't remember arduino codes since I've been learning python and using matlab, but:
    1. create a loop and have a timer delay of 1000 ms. Once the button is pressed, run the loop to show and update the display you want.
    OR
    2. google arduino real time display. Like this one: https://www.hackster.io/YoussefSabaa/lcd-display-in-real-time-ea0b7b
     
    Mr Pancakes and Drake98 like this.
  9. bear

    bear Well-Known Member

    Joined:
    Feb 26, 2017
    Messages:
    54
    Likes Received:
    49
    Reading List:
    Link
    Can't you just say:
    while(1), % so it does it the whole time
    {
    your functions
    nop (100)/or timer delay % no operation function, so it stays idle for that time, / timer delay
    }
    Then you time it so that it takes one second to make the loop, and you have your update every second.
    not a nice solution but i am not a good programmer.
     
  10. Meekh

    Meekh Well-Known Member

    Joined:
    Dec 9, 2015
    Messages:
    87
    Likes Received:
    59
    Reading List:
    Link
    The problem is with your loop function. svjetlost() which updates your display, is called only when IRrecv recieves some input, if it doesnt't recieve a signal, it returns false, so svjetlost() isn't even called - it is called only when you press the button, IRrecv.decode() recieves a signal so it returns True and the condition is met. Try something like this:
    Code:
    void loop()
    {
    
      static int option = 0;
    
      if (receiver.decode(&results)) {        
    
        if (results.value ==  0xFF30CF ) {
          option = 0;
        }
        if (results.value == 0xFF18E7) {
          option = 1;
        }
    
        if (results.value == 0xFF7A85) {
            option = 2;
        }
    
        receiver.resume();
      }
    
      if(option == 1) svjetlost();
      // similary add more contitionals for other buttons
    
      //add some delay so the loop wouldn't take to much of cpu power
      delay(200)
     
    }
     
  11. JadeWhiteSkin

    JadeWhiteSkin please rescue~

    Joined:
    Sep 2, 2016
    Messages:
    558
    Likes Received:
    481
    Reading List:
    Link
    thats not how stackoverflow works though, you need to follow the (un)written rules of how to ask for help! (Yes I looked at your post)

    ummm only read about 4 lines and got lazy, sorry just came home from work so my brain is tired right now~
     
  12. Mr Pancakes

    Mr Pancakes Well-Known Pancake

    Joined:
    May 9, 2016
    Messages:
    670
    Likes Received:
    326
    Reading List:
    Link
    I kinda got the feeling that I asked the wrong question, but idk how to ask it any other way and im desperate now.
    Some people told me to add more functions but idk how. even when I add them it doesnt work properly.
     
  13. Mr Pancakes

    Mr Pancakes Well-Known Pancake

    Joined:
    May 9, 2016
    Messages:
    670
    Likes Received:
    326
    Reading List:
    Link
    thanks I'll try this.
     
  14. kemal1132

    kemal1132 Active Member

    Joined:
    Oct 20, 2015
    Messages:
    12
    Likes Received:
    2
    Reading List:
    Link
    I assume
    svjetlost();
    is your displaying function and you will add other displaying functions in other if's
    (Because your problem seems to be only looping the button check)

    solution to your problem is creating a state variable and keep calling update through state variable and update state variable through checking
    an example code would be


    int state = 0;
    void loop()
    {
    if(state == 0){
    //0xFF30CF's function
    }
    if(state==1){
    svjetlost();
    }
    if(state==2){
    /0xFF7A85's function
    }
    if (receiver.decode(&results)) {

    if (results.value == 0xFF30CF ) {
    state = 0;
    }
    if (results.value == 0xFF18E7) {
    state = 1;
    }
    if (results.value == 0xFF7A85) {
    state = 2;
    }

    receiver.resume();
    }

    this happens because even though you are looping the listener (is button pressed) you are not looping the display. So it only calls display when button is pressed.

    //I have little experience in c and none in arduino so sorry if I am wrong
     
  15. Mr Pancakes

    Mr Pancakes Well-Known Pancake

    Joined:
    May 9, 2016
    Messages:
    670
    Likes Received:
    326
    Reading List:
    Link
    Thanks for the help, I'll try it. Svjetlost is a function thats supposed to read from the photocell.
     
  16. lohwengk

    lohwengk Well-Known Member

    Joined:
    Apr 13, 2017
    Messages:
    304
    Likes Received:
    180
    Reading List:
    Link
    My coding days are long in the past, so I can't help you directly. But I did reach senior developer and team lead, and my experience tells me that a lot of "coding bugs" actually happen because the programmer doesn't even know how the system is supposed to work in the first place. Your replies to various commenters trying to help you seem to support this. You may want to take a bit of time out to research how your lcd and sensor package is supposed to work before you start churning out code.

    Based on your words in the OP, your code does 2 things: 1) read the sensor; 2) display the value on the LCD. But it sounds like you don't even know which part is failing. Try to calm down and do some basic debugging/investigation before you panic.
     
  17. kjoke

    kjoke Well-Known Member

    Joined:
    Nov 20, 2015
    Messages:
    72
    Likes Received:
    111
    Reading List:
    Link
    This'll generate input lag, though. Probably not very important in this case but something to keep in mind
     
  18. Mr Pancakes

    Mr Pancakes Well-Known Pancake

    Joined:
    May 9, 2016
    Messages:
    670
    Likes Received:
    326
    Reading List:
    Link
    I do know which part is failing but Idk how to fix it. A lot of people told me where the problem is, and now I'm trying to fix it. And I'm panicing because I have to finish the whole project today and I still have to add a servo, wifi module and leds
     
  19. Fryz

    Fryz Well-Known Member

    Joined:
    May 7, 2016
    Messages:
    95
    Likes Received:
    120
    Reading List:
    Link
    There is this library included in the code:
    #include <IRremoteInt.h>
    So I think the remote control input can support interrupt.

    Do you want to add all of those module to a single arduino uno???
    I think it will be quite hard to manage the timing without using interrupt.
     
  20. Mr Pancakes

    Mr Pancakes Well-Known Pancake

    Joined:
    May 9, 2016
    Messages:
    670
    Likes Received:
    326
    Reading List:
    Link
    Everything else is already working. The only problem is the lcd and I'm using a mega so everything can fit