Help Noobie question

Hello I am new to playing with this arduino I'm making a ir remote project and i cannot seem to replace delay() with millis()

so i have two versions of my code one works but does not accept any other input when the code is on the delay line
and the other i tried to implement millis() but i get nothing on the serial monitor I've totally broken it and I cannot see why. I'm sure its something simple so thank you for your help in advance.

//working dont touch

#include <IRremote.h>

const int ledPin = 8; // the pin that the PC Power switch is attached to
signed long int incomingByte;      // a variable to read incoming ir data into
const int RECV_PIN = 4;   // Ir configs
IRrecv irrecv(RECV_PIN);  // Ir configs
decode_results results;   // Ir configs

void setup() {
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  irrecv.enableIRIn(); // Start the receiver


}

void loop() {
  
  
  
  if (irrecv.decode(&results) > 0) {
    Serial.println(results.value, DEC);
    irrecv.resume(); //testing if it made any difference 
    
    incomingByte =(results.value);
    Serial.println(incomingByte);
    
    
    if (incomingByte == 550720173) {
        Serial.println("Small button press");
        digitalWrite(ledPin, LOW);
        delay(4500);
        digitalWrite(ledPin, HIGH);                            
         
        
    
        
      }
    irrecv.resume();
  }
  
}
//Broken
#include <IRremote.h>

const int ledPin = 8; // the pin that the PC Power switch is attached to
signed long int incomingByte;      // a variable to read incoming ir data into
const int RECV_PIN = 4;   // Ir configs
IRrecv irrecv(RECV_PIN);  // Ir configs
decode_results results;   // Ir configs
unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 4500;
void setup() {
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
  digitalWrite(ledPin, HIGH);
  irrecv.enableIRIn(); // Start the receiver
  startMillis = ;  //initial start time


}

void loop() {
  currentMillis = millis(); {  //get the current "time" (actually the number of milliseconds since the program started)




    if (irrecv.decode(&results) > 0) {
      Serial.println(results.value, DEC);
      irrecv.resume();
      incomingByte = (results.value);
      Serial.println(incomingByte);
      if (currentMillis - startMillis >= period)  //test whether the period has elapsed
      {
        if (incomingByte == 550720173) {
          Serial.println("Small button press");
          digitalWrite(ledPin, LOW);
        }
      }
      digitalWrite(ledPin, HIGH);




    }
    irrecv.resume();
  }
}

Something like this should work (not tested...)

//Broken (or is it?)
#include <IRremote.h>

const int ledPin = 8; // the pin that the PC Power switch is attached to
signed long int incomingByte;      // a variable to read incoming ir data into
const int RECV_PIN = 4;   // Ir configs
IRrecv irrecv(RECV_PIN);  // Ir configs
decode_results results;   // Ir configs
unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 4500;

void setup() 
{
    Serial.begin(115200);
    pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
    digitalWrite(ledPin, HIGH);
    irrecv.enableIRIn(); // Start the receiver
    //startMillis = ;  //initial start time
    
}//setup

void loop() 
{
    static bool
        bSwitchFlag = false;

    //if the switch is 'closed'...
    if( bSwitchFlag == true )
    {
        //check to see if the elapsed time is 4500mS or more
        if( millis() - startMillis >= 4500 )
        {
            //...it is; open the switch and cancel the flag
            digitalWrite(ledPin, HIGH);     
            bSwitchFlag = false;
        
        }//if
        
    }//if
    
    if (irrecv.decode(&results) > 0) 
    {
        Serial.println(results.value, DEC);
        irrecv.resume();
        incomingByte = (results.value);
        Serial.println(incomingByte);
        
        if (incomingByte == 550720173) 
        {
            //if we're already timing a previous message
            //of this type we ignore this message
            if( !bSwitchFlag )
            {
                //switch was open; close it, set the
                //flag and get the current millis() count
                Serial.println("Small button press");
                digitalWrite(ledPin, LOW);
                startMillis = millis();
                bSwitchFlag = true;
            
            }//if
            
        }//if

        //you can put other IR code checks and actions in here
        //.
        //.
        //.
        
    }//if
    
    irrecv.resume();
    
}//loop

When you receive the correct byte, that is when you need to start timer and then check the timer every time through loop()

//working dont touch

#include <IRremote.h>

const int ledPin = 8; // the pin that the PC Power switch is attached to
signed long int incomingByte;      // a variable to read incoming ir data into
const int RECV_PIN = 4;   // Ir configs
IRrecv irrecv(RECV_PIN);  // Ir configs
decode_results results;   // Ir configs

unsigned long startMillis();
const unsigned long period = 4500;
bool timerRunning = false;

void setup() {
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {

  if ( timerRunning ) {
    if ( millis() - startMillis >= period ) {
      timerRunning = false;
      digitalWrite(ledPin, HIGH);
    }
  }

  if (irrecv.decode(&results) > 0) {
    Serial.println(results.value, DEC);
    irrecv.resume(); //testing if it made any difference

    incomingByte = (results.value);
    Serial.println(incomingByte);

    if (incomingByte == 550720173) {
      Serial.println("Small button press");
      digitalWrite(ledPin, LOW);
      startMillis = millis();
      timerRunning = true;
    }
    irrecv.resume();
  }
}

blh64:
When you receive the correct byte, that is when you need to start timer and then check the timer every time through loop()

//working dont touch

#include <IRremote.h>

const int ledPin = 8; // the pin that the PC Power switch is attached to
signed long int incomingByte;      // a variable to read incoming ir data into
const int RECV_PIN = 4;  // Ir configs
IRrecv irrecv(RECV_PIN);  // Ir configs
decode_results results;  // Ir configs

unsigned long startMillis();
const unsigned long period = 4500;
bool timerRunning = false;

void setup() {
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {

if ( timerRunning ) {
    if ( millis() - startMillis >= period ) {
      timerRunning = false;
      digitalWrite(ledPin, HIGH);
    }
  }

if (irrecv.decode(&results) > 0) {
    Serial.println(results.value, DEC);
    irrecv.resume(); //testing if it made any difference

incomingByte = (results.value);
    Serial.println(incomingByte);

if (incomingByte == 550720173) {
      Serial.println("Small button press");
      digitalWrite(ledPin, LOW);
      startMillis = millis();
      timerRunning = true;
    }
    irrecv.resume();
  }
}

Thanks but I receive these errors with your changes

/home/richards/Arduino/power_btn_test_rev_3-1/power_btn_test_rev_3-1.ino: In function 'void loop()':
power_btn_test_rev_3-1:23:21: error: invalid operands of types 'long unsigned int' and 'long unsigned int()' to binary 'operator-'
     if ( millis() - startMillis >= period ) {
                     ^
power_btn_test_rev_3-1:39:19: error: assignment of function 'long unsigned int startMillis()'
       startMillis = millis();
                   ^
power_btn_test_rev_3-1:39:19: error: cannot convert 'long unsigned int' to 'long unsigned int()' in assignment
exit status 1
invalid operands of types 'long unsigned int' and 'long unsigned int()' to binary 'operator-'

Blackfin:
Something like this should work (not tested...)

//Broken (or is it?)

#include <IRremote.h>

const int ledPin = 8; // the pin that the PC Power switch is attached to
signed long int incomingByte;      // a variable to read incoming ir data into
const int RECV_PIN = 4;  // Ir configs
IRrecv irrecv(RECV_PIN);  // Ir configs
decode_results results;  // Ir configs
unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 4500;

void setup()
{
    Serial.begin(115200);
    pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
    digitalWrite(ledPin, HIGH);
    irrecv.enableIRIn(); // Start the receiver
    startMillis = ;  //initial start time
   
}//setup

void loop()
{
    static bool
        bSwitchFlag = false;

//if the switch is 'closed'...
    if( bSwitchFlag == true )
    {
        //check to see if the elapsed time is 4500mS or more
        if( millis() - startMillis >= 4500 )
        {
            //...it is; open the switch and cancel the flag
            digitalWrite(ledPin, HIGH);   
            bSwitchFlag = false;
       
        }//if
       
    }//if
   
    if (irrecv.decode(&results) > 0)
    {
        Serial.println(results.value, DEC);
        irrecv.resume();
        incomingByte = (results.value);
        Serial.println(incomingByte);
       
        if (incomingByte == 550720173)
        {
            //if we're already timing a previous message
            //of this type we ignore this message
            if( !bSwitchFlag )
            {
                //switch was open; close it, set the
                //flag and get the current millis() count
                Serial.println("Small button press");
                digitalWrite(ledPin, LOW);
                startMillis = millis();
                bSwitchFlag = true;
           
            }//if
           
        }//if

//you can put other IR code checks and actions in here
        //.
        //.
        //.
       
    }//if
   
    irrecv.resume();
   
}//loop

Thanks this verifies fine but does nothing when i upload with your changes :frowning:

unsigned long startMillis();

The parentheses in this statement do not belong there.

PaulS:

unsigned long startMillis();

The parentheses in this statement do not belong there.

Correct. My bad since I didn't have the IRRemote library loaded and didn't verify the code.

I noticed a line reading "startMillis = ; //initial start time" that actually generates an error. The code you loaded was not mine if it compiled okay :slight_smile:

Try commenting that line out and trying again.

FWIW, I tried this code, which is basically the same but disables the IR stuff and uses a count on incomingByte and it works for timing the LED:

//Broken (or is it?)
//#include <IRremote.h>

const int ledPin = 13; // the pin that the PC Power switch is attached to

signed long int incomingByte;      // a variable to read incoming ir data into
const int RECV_PIN = 4;   // Ir configs
//IRrecv irrecv(RECV_PIN);  // Ir configs
//decode_results results;   // Ir configs
unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 4500;

void setup() 
{
    Serial.begin(115200);
    pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
    digitalWrite(ledPin, HIGH);
    //irrecv.enableIRIn(); // Start the receiver
    //startMillis = ;  //initial start time

    incomingByte = 550700000;
    
}//setup

void loop() 
{
    static bool
        bSwitchFlag = false;

    //if the switch is 'closed'...
    if( bSwitchFlag == true )
    {
        //check to see if the elapsed time is 4500mS or more
        if( millis() - startMillis >= 4500 )
        {
            //...it is; open the switch and cancel the flag
            digitalWrite(ledPin, HIGH);     
            bSwitchFlag = false;
        
        }//if
        
    }//if
    
    //if (irrecv.decode(&results) > 0) 
    //{
        incomingByte++;
        Serial.println( incomingByte );
        //Serial.println(results.value, DEC);
        //irrecv.resume();
        //incomingByte = (results.value);
        //Serial.println(incomingByte);
        
        if (incomingByte == 550720173) 
        {
            //if we're already timing a previous message
            //of this type we ignore this message
            if( !bSwitchFlag )
            {
                //switch was open; close it, set the
                //flag and get the current millis() count
                Serial.println("Small button press");
                digitalWrite(ledPin, LOW);
                startMillis = millis();
                bSwitchFlag = true;
            
            }//if
            
        }//if

        //you can put other IR code checks and actions in here
        //.
        //.
        //.
        
    //}//if
    
    //irrecv.resume();
    
}//loop

thanks every one who helped me with this and now I have different question.
I have arduino uno and i want to use it to control a python program running listening to the serial data coming from the arduino but i cannot get the python program to work. Here is both my code

//Helped by blh64
//Fixed by PaulS
#include <IRremote.h>

// constants won't change. Used here to set a pin number:
const int ledPin2 =  LED_BUILTIN;// the number of the LED pin

// Variables will change:
int ledState2 = LOW;             // ledState used to set the LED


const int ledPin = 8; // the pin that the PC Power switch is attached to
signed long int incomingByte;      // a variable to read incoming ir data into
const int RECV_PIN = 4;   // Ir configs
IRrecv irrecv(RECV_PIN);  // Ir configs
decode_results results;   // Ir configs

unsigned long startMillis;
const unsigned long period = 4500;
bool timerRunning = false;
unsigned long lastCode;

void setup() {
  pinMode(ledPin2, OUTPUT); // set the digital pin as output:
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {

  if ( timerRunning ) {
    if ( millis() - startMillis >= period ) {
      timerRunning = false;
      digitalWrite(ledPin, HIGH);
    }
  }

  if (irrecv.decode(&results) > 0) {
//    Serial.println(results.value, DEC);
    if (results.value == 4294967295) {
      results.value = lastCode;
    }
    //    irrecv.resume(); //testing if it made any difference

    incomingByte = (results.value);
    //    Serial.println(incomingByte);

    if (incomingByte == 550720173) {
//      lastCode = results.value;
      Serial.println("Small button press");
      digitalWrite(ledPin, LOW);
      startMillis = millis();
      timerRunning = true;
      lastCode = results.value;
    }
    if (incomingByte == 550726293) {
      Serial.println("Other button press");
      if (ledState2 == LOW) {
        ledState2 = HIGH;
      } else {
        ledState2 = LOW;
      }
      lastCode = results.value;
      digitalWrite(ledPin2, ledState2);
    }
    if (incomingByte == 550701813) {
      Serial.println("play");
      lastCode = results.value;
    }
    if (incomingByte == 550709973) {
//      lastCode = results.value;
      Serial.println("pause");
      lastCode = results.value;
    }
    if (incomingByte == 550712013) {
//      lastCode = results.value;
      Serial.println("stop");
      lastCode = results.value;
    }
    if (incomingByte == 550728333) {
//      lastCode = results.value;
      Serial.println("volumeUp");
      lastCode = results.value;
    }
    if (incomingByte == 550718133) {
//      lastCode = results.value;
      Serial.println("volumeDown");
      lastCode = results.value;
    }
    if (incomingByte == 550744143) {
//      lastCode = results.value;
      Serial.println("fast forward");
      lastCode = results.value;
    }
    if (incomingByte == 550727823) {
//      lastCode = results.value;
      Serial.println("fast backward");
      lastCode = results.value;
    }
    if (incomingByte == 550717623) {
//      lastCode = results.value;
      Serial.println("forward");
      lastCode = results.value;
    }
    if (incomingByte == 550733943) {
//      lastCode = results.value;
      Serial.println("backward");
      lastCode = results.value;
    }
    if (incomingByte == 550734453) {
//      lastCode = results.value;
      Serial.println("mute");
      lastCode = results.value;
    }
    if (incomingByte == 550723743) {
//      lastCode = results.value;
      Serial.println("up");
      lastCode = results.value;
    }
    if (incomingByte == 550719663) {
//      lastCode = results.value;
      Serial.println("down");
      lastCode = results.value;
    }
    if (incomingByte == 550748733) {
//      lastCode = results.value;
      Serial.println("left");
      lastCode = results.value;
    }
    if (incomingByte == 550731903) {
//      lastCode = results.value;
      Serial.println("right");
      lastCode = results.value;
    }
    if (incomingByte == 550715583) {
//      lastCode = results.value;
      Serial.println("ok");
      lastCode = results.value;
    }

    irrecv.resume();
  }
}

and here is my python code but i cant seem to get it working

#!/usr/bin/python3.6

import serial
import vlc
import subprocess



# "COM11" is the port that your Arduino board is connected.set it to port that your are using        
ser = serial.Serial('/dev/ttyUSB0',115200);
x = ser.readline()
#get_media_player(self)
while True:
    cc=str(ser.readline())
    print(cc[2:][:-5])
    if (ser.inWaiting()>0):
             a = ser.readline()
             x = ser.readline()
                if x == play:
                    subprocess.run(["dbus-send--type=method_call --dest=org.mpris.MediaPlayer2.vlc /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause"])

If any one has a better idea on how to make this happen please let me know

signed long int incomingByte;      // a variable to read incoming ir data into

It id dumb to use a type (byte) in the name of a variable when the type of the variable is not the type in the named.

There was no reason to copy the value in results.value to another variable.

      if (ledState2 == LOW) {
        ledState2 = HIGH;
      } else {
        ledState2 = LOW;
      }

That's a lot of code to do this:

   ledState2 = !ledState2;

Your python script reads up to three lines each execution of the loop. The Arduino only writes one. What ARE you actually reading? Why don't you know?

Hello PaulS the arduino code has a few bits of code i was experimenting with but that works ok as it is.
As i said in my previous post I want to make a python script that will respond from the serial data the arduino is providing and in my first tests i cannot get vlc player to respond to any of the commands.

Do you have any better ideas to make the arduino control vlc directly? If so please help me.

As i said in my previous post I want to make a python script that will respond from the serial data the arduino is providing and in my first tests i cannot get vlc player to respond to any of the commands.

You are sending one "command", such as "Small button press", "play", "pause", etc. You read the "command", and store the result in cc. If there is more serial data (it would be extremely rare that there would be, you read two more lines, storing the results in the usefully named variables a and x (not that cc means a thing, mind you).

You ONLY concern yourself with what is stored in x. I can't imagine that there is ever anything stored in x. So, it seems obvious to me why vlc never responds.

I'll ask again. Why are you reading the one "command" with three readline() calls?

PaulS:
You are sending one "command", such as "Small button press", "play", "pause", etc. You read the "command", and store the result in cc. If there is more serial data (it would be extremely rare that there would be, you read two more lines, storing the results in the usefully named variables a and x (not that cc means a thing, mind you).

You ONLY concern yourself with what is stored in x. I can't imagine that there is ever anything stored in x. So, it seems obvious to me why vlc never responds.

I'll ask again. Why are you reading the one "command" with three readline() calls?

OK I see what you are saying I've been testing different ways of getting what I want but none of them are working.
here is another version of code that doesn't work and I don't know why

while True:
    cc=str(ser.readline())
    print(cc[2:][:-5])
    if (ser.inWaiting()>0):
             a = ser.readline()
             x = ser.readline()
    if (ser.readline(), (cc[2:][:-5])) == "play":
#        get_media_player(self)
#        media = vlc.MediaPlayer()
        "dbus-send --type=method_call --dest=org.mpris.MediaPlayer2.vlc /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause"
    
    elif x == "stop":
         media.stop()
         'dbus-send --type=method_call --dest=org.mpris.MediaPlayer2.vlc /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause'

and I don't know why

I'm certainly not a python expert. I saw one in a zoo once. But, I'm going to show some lines of your code, and tell you what they suggest to me that they are doing. You can confirm, or refute, or explain, as needed.

while True:
    cc=str(ser.readline())

In an infinite loop, read a line from the serial port (which returns a string), and convert the string to a string, and store it in cc. So, cc should contain "play" or "go up in smoke" or "stop" or "volume up".

Am I right?

If I am, then

    print(cc[2:][:-5])

Print some mysterious portion of cc somewhere. I would guess that it is printing everything but the first two and last 5 characters. So, when the command is "go up in smoke", you print " up in". When the command is "play" or "stop" or "pause", you print nothing.

Am I anywhere close to right?

If I am, then

    if (ser.inWaiting()>0):

If there is any more serial data to be read, do some stuff.

Am I right?

If I am, then

             a = ser.readline()
             x = ser.readline()

Read two more strings from the serial port.

Now, the Arduino is only sending one line, so I suspect that this code won't be executed, but, even if it was, a and x would not contain anything useful, because you only send one line of data.

    if (ser.readline(), (cc[2:][:-5])) == "play":

I can't even begin to guess what this is trying to do. Reading ANOTHER line from the serial port, when the Arduino hasn't sent any data is pointless. I don't know what python does with the comma operator, so I can't guess what is actually being evaluated, and I suspect the portion of cc being accessed does not contain "play", so I doubt that the if statement evaluates to true.

So, am I all wet?