IR led will not transmit if I try to digitalread in my code

I am trying to make a remote for my sat receiver.

The channel upcode works, but then when I insert the code to read my push buttons it will not.

I have even put the upcode command before my “GetBtn” routine.

I have tried serial printing all of the timer registers to see if anything is going wrong here.

Here is my code::-

/*********************************************************************
    TV remote for edision sat reciever
    Uses:-
          Nokia 3310 display, ProMini running at 16mhz and 2 pencels, 3.1V - might be borderline!
              ledpin needs to be earthed on my display (pinouts diff order too)
              leds seem to light ok with 400 ohm (prob 6 mA on 3V)
              Contrast setting: Ada set it to 50.
                  40 I see nothing
                  60 v good contrast without backlight (daytime no better with backlight)
          IR led with 75R for 3V
          
    Pins:-

    DO:-
          settings menu - at mo hitting ok on settings will prob crash
          backlight code
  
*********************************************************************/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
//#include "Edision.h"
#define BLDelay 120     // backlight on time
#define MenuMax 8
static int BLPin = 2;         // these were all byte, but reference says should be int
static int BtnNxtPin  = 9;    // 9
static int BtnOkPin = 8;      // 8
static int IRledPin =  13;
byte BtnNxt = 0;
byte BtnOk = 0;
byte MenuIndex = 1; 
byte BLTimer = 0;
Adafruit_PCD8544 display = Adafruit_PCD8544(3,  4,  5,  6,  7);
    //  display pins                        clk Din D/C CS  RST

void setup()   {
  Serial.begin(9600);
  pinMode(IRledPin, OUTPUT);      
  pinMode(BLPin, OUTPUT);              // £ set low for on
  pinMode(BtnNxtPin,INPUT_PULLUP);
  pinMode(BtnOkPin,INPUT_PULLUP);
  digitalWrite(BLPin,LOW);
  display.begin();                     // init done
  display.setContrast(60);             // orig Ada setting is 50
  display.clearDisplay();              // clear the screen and buffer
  XChnUp();                                                        // *** this works provided I comment out GetBtn function below!
}
void loop() {
  GetBtn();                                                       // *** this makes above work!
  Menu();
  Display();
  delay(200);
}

// ************************************* FUNCTIONS *************************************************************
void GetBtn() {
    BtnNxt = 0; BtnOk = 0;
//    if(digitalRead(BtnNxtPin) == 0) BtnNxt = 1; if(digitalRead(BtnOkPin) == 0) BtnOk = 1;
    if(BtnNxt == 1 || BtnOk == 1)  BLTimer = BLDelay;
//    do { /*wait for button release */} while (digitalRead(BtnNxtPin) == 0 || digitalRead(BtnOkPin) == 0);  
    delay(10); //debounce
}
void Menu() {
    if (BtnNxt == 1) MenuIndex ++;  if (MenuIndex > MenuMax) MenuIndex = 1;
//    if ((BtnOk == 1) && (MenuIndex < 8)) TxCode(); // which code Txd is based on MenuIndex
    if ((BtnOk == 1) && (MenuIndex == 2)) XChnUp(); // which code Txd is based on MenuIndex
}
void Display() {
    display.clearDisplay();
    display.setTextSize(2);
    display.setTextColor(BLACK);
    switch(MenuIndex) {
     case 1:
      display.println(F("Power"));
      break;
     case 2:
      display.println(F("Chn Up"));      display.print(F("Cur Up"));
      break;
     case 3:
      display.println(F("Chn Dn"));      display.print(F("Cur Dn"));
      break;
     case 4:
      display.println(F("Vol Up"));      display.print(F("Cur R"));
      break;
     case 5:
      display.println(F("Vol Dn"));      display.print(F("Cur L"));
      break;
     case 6:
      display.println(F("Info"));
      break;
     case 7:
      display.println(F("OK"));
      break;
     case 8:
      display.println(F("Settngs"));
      break;
    }
  display.display();
}
void TxCode() {
  byte CodeIdx = MenuIndex - 1;
  for (int i=0; i <= 66; i++){          // £ 67?
    pulseIR(EdCode[CodeIdx][i]);
    i++;                               // this gets 67th element on final loop = 68th item
    delayMicroseconds(EdCode[CodeIdx][i]);
  }
}
void pulseIR(long microsecs) {     // count down from the number of microseconds we are told to wait
  cli();                           // this turns off any background interrupts
  while (microsecs > 0) {          // 38 kHz is about 13 microseconds high and 13 microseconds low
   digitalWrite(IRledPin, HIGH);   // this takes about 3 microseconds to happen
   delayMicroseconds(10);          // hang out for 10 microseconds
   digitalWrite(IRledPin, LOW);    // this also takes about 3 microseconds
   delayMicroseconds(10);          // hang out for 10 microseconds
                                   // so 26 microseconds altogether
   microsecs -= 26;
  }
  sei();  // this turns them back on
}
void XChnUp() {
pulseIR(9240);delayMicroseconds(4620);
pulseIR(560);delayMicroseconds(540);
pulseIR(600);delayMicroseconds(540);
pulseIR(600);delayMicroseconds(540);
pulseIR(580);delayMicroseconds(540);
pulseIR(600);delayMicroseconds(540);
pulseIR(580);delayMicroseconds(560);
pulseIR(580);delayMicroseconds(540);
pulseIR(600);delayMicroseconds(540);
pulseIR(580);delayMicroseconds(1740);
pulseIR(560);delayMicroseconds(1740);
pulseIR(540);delayMicroseconds(1740);
pulseIR(580);delayMicroseconds(1720);
pulseIR(560);delayMicroseconds(1740);
pulseIR(560);delayMicroseconds(1740);
pulseIR(560);delayMicroseconds(1720);
pulseIR(580);delayMicroseconds(1720);
pulseIR(560);delayMicroseconds(540);
pulseIR(600);delayMicroseconds(1740);
pulseIR(560);delayMicroseconds(1720);
pulseIR(560);delayMicroseconds(580);
pulseIR(560);delayMicroseconds(540);
pulseIR(600);delayMicroseconds(540);
pulseIR(580);delayMicroseconds(540);
pulseIR(600);delayMicroseconds(540);
pulseIR(600);delayMicroseconds(1720);
pulseIR(560);delayMicroseconds(580);
pulseIR(560);delayMicroseconds(560);
pulseIR(560);delayMicroseconds(1740);
pulseIR(560);delayMicroseconds(1820);
pulseIR(480);delayMicroseconds(1760);
pulseIR(540);delayMicroseconds(1800);
pulseIR(500);delayMicroseconds(1840);
pulseIR(440);delayMicroseconds(50000);
//Serial.println("here 2 ");
}
// END

I have tried tried to highlight the code and use the code symbol at top of page, but I can not highlight it - sorry.

This is what happens:-

If I comment out the two lines where I digitalread (lines 71 and 73), then when I transmit the code (at the end of SetUp, line 56, “XChnUp” the sat receiver goes up one channel.

If I uncomment the digitalread (so I can check my button state), then I see nothing at the IR led when I look at it with my camera.

Any help would be appreciated! Thank you

Hi,
What model Arduino are you using?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

Sorry, Missed the obvious out.

I am using a pro-mini with a nokia display.

The 2 menu buttons are wired to ground and pin 8 and Pin 9, pulling these pins down when pressed.

The pinouts, instead of a wiring diagram for the moment are as follows:-

D7 to Nokia RST
D6 to Nokia CE
D5 to nokia DC
D4 to nokia DIN
D3 to nokia CLK
D2 to nokia backlight via 400 ohm
Nokia supply to vcc
IR led to pin D13 via 75 ohm
Button Nxt to D9 and -ve
Button OK to D8 and -ve

Thank you

Can you please make that code a little more readable? Making it harder to read isn't going to get more people to help. One statement per line. And use auto-format to line up the indentation and blocks. But mostly get it to one statement per line.

I have removed as much code as possible, and auto-formatted it:-

static int BtnNxtPin  = 9;
static int BtnOkPin = 8;
static int IRledPin =  13;
byte BtnNxt = 0;
byte BtnOk = 0;
void setup()   {
  Serial.begin(9600);
  pinMode(IRledPin, OUTPUT);
  pinMode(BtnNxtPin, INPUT_PULLUP);
  pinMode(BtnOkPin, INPUT_PULLUP);
  Send_ChnUp();
}
void loop() {
  GetBtn();
  delay(200);
}

void GetBtn() {
  BtnNxt = 0; BtnOk = 0;
  if (digitalRead(BtnNxtPin) == 0) BtnNxt = 1;
  if (digitalRead(BtnOkPin) == 0) BtnOk = 1;
  do {
    /*wait for button release */
  } while (digitalRead(BtnNxtPin) == 0 || digitalRead(BtnOkPin) == 0);
  delay(10); //debounce
}

void pulseIR(long microsecs) {     // count down from the number of microseconds we are told to wait
  cli();                           // this turns off any background interrupts
  while (microsecs > 0) {          // 38 kHz is about 13 microseconds high and 13 microseconds low
    digitalWrite(IRledPin, HIGH);   // this takes about 3 microseconds to happen
    delayMicroseconds(10);          // hang out for 10 microseconds
    digitalWrite(IRledPin, LOW);    // this also takes about 3 microseconds
    delayMicroseconds(10);          // hang out for 10 microseconds
    // so 26 microseconds altogether
    microsecs -= 26;
  }
  sei();  // this turns them back on
}
void Send_ChnUp() {
  pulseIR(9240); delayMicroseconds(4620);
  pulseIR(560); delayMicroseconds(540);
  pulseIR(600); delayMicroseconds(540);
  pulseIR(600); delayMicroseconds(540);
  pulseIR(580); delayMicroseconds(540);
  pulseIR(600); delayMicroseconds(540);
  pulseIR(580); delayMicroseconds(560);
  pulseIR(580); delayMicroseconds(540);
  pulseIR(600); delayMicroseconds(540);
  pulseIR(580); delayMicroseconds(1740);
  pulseIR(560); delayMicroseconds(1740);
  pulseIR(540); delayMicroseconds(1740);
  pulseIR(580); delayMicroseconds(1720);
  pulseIR(560); delayMicroseconds(1740);
  pulseIR(560); delayMicroseconds(1740);
  pulseIR(560); delayMicroseconds(1720);
  pulseIR(580); delayMicroseconds(1720);
  pulseIR(560); delayMicroseconds(540);
  pulseIR(600); delayMicroseconds(1740);
  pulseIR(560); delayMicroseconds(1720);
  pulseIR(560); delayMicroseconds(580);
  pulseIR(560); delayMicroseconds(540);
  pulseIR(600); delayMicroseconds(540);
  pulseIR(580); delayMicroseconds(540);
  pulseIR(600); delayMicroseconds(540);
  pulseIR(600); delayMicroseconds(1720);
  pulseIR(560); delayMicroseconds(580);
  pulseIR(560); delayMicroseconds(560);
  pulseIR(560); delayMicroseconds(1740);
  pulseIR(560); delayMicroseconds(1820);
  pulseIR(480); delayMicroseconds(1760);
  pulseIR(540); delayMicroseconds(1800);
  pulseIR(500); delayMicroseconds(1840);
  pulseIR(440); delayMicroseconds(50000);
}

I still have the same problem. If I comment out the lines with digitalread then the function call at the end of setup does knock my receiver up a channel.

Alternatively if I comment out "GetBtn" in the main loop, then again it will work.

However without commenting out one of these two things means the led does not flash, despite the "Send_ChnUp" being called before the main loop.

Thank you all for looking at this problem, here is a quick update, as I spent a lot of time looking at what was going wrong,

Reading the datasheet for the 328 and the error in the compiled code I finally found it!

Hi,

Asymic:
Thank you all for looking at this problem, here is a quick update, as I spent a lot of time looking at what was going wrong,

Reading the datasheet for the 328 and the error in the compiled code I finally found it!

What was the cause of your problem?
Please post the cause here so that the spirit of the forum is preserved.

  • You had a problem and asked the forum for help.
  • You have solved your problem.
  • This forum is to help everybody.
  • So to help someone else who might end up in your situation, PLEASE post your solution.
  • Please post a working copy of your code.

Thanks.. Tom... :slight_smile:

Hi,

I will post this when I have a little more time, but at present the code is not written in your preferred format, so I doubt if Delta_G will understand it.

With regard to your comment about "The spirit of the forum", I must say that despite asking for help, I got none. Same as my last post. In both instances I have simply had the lackings of my post highlighted straight away (although I did not include a circuit, the pinouts were in the code). Once I had fulfilled those shortfalls, nobody made any attempt at making a suggestion. Expanding on this I feel that some of one of the above posts had a definite negative vein to it, which I also experienced in my previous thread!

I do not want to appear negative, but my general feeling is that the spirit of this forum is not actually very positive. maybe if you take a back seat view and look at the wording of some of these posts, and maybe those in my last thread, you might be able to achieve a better spirit.

Hi,
I think you don't realise that forum members cannot answer questions the minute after they are posted.

You posted your last post at 19:36 my time
Its now 21:55, sorry I could not respond quicker.
Other members are asleep, at work, busy or on a National holiday, weekend.

After my first post to help you provide some basic info, I just observed, as I am aiming to make a big remote for my portable Aircon.

Tom... :slight_smile:

but at present the code is not written in your preferred format, so I doubt if Delta_G will understand it.

Oh I could understand it. I just have better things to do than try to sort through some poorly formatted code to try to find an error. If you make it hard to read then I'll just go help someone else who doesn't think it's too much to ask to press control-T before they post their code. I don't personally care if you get your stuff working or not. If you want help and don't make it too hard for me to help you then I will. But I'm not going to go out of my way and do a bunch of extra work to fix your code.

Hope that makes sense.