D
D
deeprootedevil2018-10-05 11:57:01
Arduino
deeprootedevil, 2018-10-05 11:57:01

How to switch between buttons?

#include <IRremote.h>

int inches = 0;
int k = 0;
int RECV_PIN = 2;
IRrecv irrecv(RECV_PIN);
decode_results results;

int cm = 0;

long readUltrasonicDistance(int triggerPin, int echoPin)
{
  pinMode(triggerPin, OUTPUT);  
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(5);
  
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  pinMode(echoPin, INPUT);
  
  return pulseIn(echoPin, HIGH);
}

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn();

}

void loop()
{
  if (irrecv.decode(&results)) {//irrecv.decode(&results) returns true if anything is recieved, and stores info in varible results 
    unsigned int value = results.value; //Get the value of results as an unsigned int, so we can use switch case 
    Serial.println(value);
  
    switch (value) { 
    case 255: 
       
        
      cm = readUltrasonicDistance(12, 13) / 46;
  
        inches = (cm / 2.54);
        Serial.print(inches);
        Serial.print("in, ");
        Serial.print(cm);
        Serial.println("cm");
        delay(100);
      
      break;
     
      
    case 20655:	
        
      cm = readUltrasonicDistance(12, 13) / 46 * 1.01;
  
        inches = (cm / 2.54);
        Serial.print(inches);
        Serial.print("in, ");
        Serial.print(cm);
        Serial.println("cm");
        delay(100);
   
    break;
    }
    //irrecv.resume();
  }
  
}

It is necessary that the case with a value of 255 works constantly, but at the same time other buttons on the remote also work.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Skusnov, 2018-10-05
@AlexSku

It is necessary that the case with a value of 255 works constantly

It must be the first one and it must not have a break.

V
Vasily F, 2018-11-08
@Vasilii_B2

Not a clear question?!?
- If you want a piece of code (contained in case 255) to always be executed, take it out of switch() completely.
- If you need case 255 to be executed: provided that there is no suitable condition for the switch(value) code - that is, such a thing as default:
- If you need the code from case 255 to be executed for any described case value - make a piece of code from case 255 function and insert a call to that function in each case.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question