Help...

Hello guys. So... i have 3 buttons and 5 (+4 more, at the end...) leds. What i want to do is to make a remote controll. Basicaly 1 button is for reset or jump (i will tell you what jump means), the other 2 are used to connect to either TV, DVD, RADIO or CD using 00 01 10 11 combinations. After it connects to one of the devices, i can choose either PROGRAMS or VOLUME using 10 and 01 combinations. After i choose programs/volume, i must use the buttons to Increase/Decrease volume, and move to the next/previous program using 00 01 10 11 combinations. The jump button will be used to exit these modes:

Ex: you connected to the TV, selected Programs, moved to the 31st program and you want to increase volume: you press JUMP and you get to the point where you select Program or Volume. But you can also press JUMP again, and you get to the connection phase (where the led blinks).

The graph is this one. I made only for the RADIO but its the same for the rest of them. Hope its easier to understand it now.

How i want to do the program:

First: i have 1 led that blinks untill one of the DEVICES combination is pressed (00 01 10 11). After it connects, the first led stays ON and also the LED of the DEVICE turns on. After that, you choose Programs or Volume and turn on 4 more leds (P+ P- V+ or V-). If i press JUMP, the last led must turn off...

I get problems registering the 00 combination...

this is my program so far (i did not add the last 4 leds for p+ p- v+ v-, first i want to make it connect to the devices, and be able to select program or volume)

const int b1 = 2; //button 1
const int b2 = 3; //button 2
const int ledPin =  8; //Connectivty led (which blinks first and then stays turned on)
const int ledTV = 9; //led TV
const int ledDVD = 10; //led DVD
const int ledCD = 11; //led CD
const int ledRAD = 12; //led RAD

int bState1 = 0; //state button 1
int bState2 = 0; //state button 2

void setup() 
{
  pinMode(ledPin, OUTPUT);
  pinMode(b1, INPUT);
  pinMode(b2, INPUT);
  pinMode(ledTV, OUTPUT);
  pinMode(ledDVD, OUTPUT);
  pinMode(ledCD, OUTPUT);
  pinMode(ledRAD, OUTPUT);
  Serial.begin(9600);
}

int devicec = 6;
int revert = 0;
int connection = 0;

void loop() 
{
  bState1 = digitalRead(b1);
  bState2 = digitalRead(b2);
  if(bState1 == HIGH && bState2 == HIGH)
    connection = 1;
  if(connection == 1)
  {
    digitalWrite(ledPin, HIGH);
    delay(500);
    digitalWrite(ledPin, LOW);
    delay(500); //this should be the part where it blinks untill it`s connected
    devicec=d();
    switch (devicec) //where will it connect? 
    {
      case 0:
        digitalWrite(ledTV, HIGH);
        digitalWrite(ledPin, HIGH);
        connection=0;
      break;
      case 1:
        digitalWrite(ledDVD, HIGH);
        digitalWrite(ledPin, HIGH);
        connection=0;
      break;
      case 2:
        digitalWrite(ledCD, HIGH);
        digitalWrite(ledPin, HIGH);
        connection=0;
      break;
      case 3:
        digitalWrite(ledRAD, HIGH);
        digitalWrite(ledPin, HIGH);
        connection=0;
      break;
    }
  }
}

int d()
{
  int state11 = 0;
  int state22 = 0;
  state11 = digitalRead(b1);
  state22 = digitalRead(b2);
  int nr = state11 + 2*state22;
  return nr;
}

Thanks very much!

What is your question?

You are only reading the state of the switches once a second. Are you holding them long enough?

  int state11 = 0;
  int state22 = 0;
  state11 = digitalRead(b1);
  state22 = digitalRead(b2);
  int nr = state11 + 2*state22;
  return nr;

or

  return digitalRead(b1) + 2*digitalRead(b2);

My questions are:

  1. How do i make the first led BLINK untill something is registered (00/01/10/11)
  2. How do i make it register 00?
  3. How do i jump to the previous phase?

How do i make the first led BLINK untill something is registered (00/01/10/11)

Use a state machine and switch/case as you have in another area of the program. Don't use delay() for timing, instead use millis() as in the BlinkWithoutDelay example. This will allow the program to run freely and respond to button presses.

  1. How do i make the first led BLINK untill something is registered (00/01/10/11)

You need to use a boolean variable:

boolean waitingForUserToPressSwitch = true;

void loop()
{
   int state1 = digitalRead(b1);
   int state2 = digitalRead(b2);
   if(state1 == HIGH || state2 == HIGH)
     waitingForUserToPressSwitch = false;

   if(waitingForUserToPressSwitch)
   {
      // blink the LED WITHOUT USING DELAY()
   }
}

Look at the blink without delay example.

  1. How do i make it register 00?

Wouldn't 00 mean no switch is pressed? Doesn't that correspond to state1 == LOW and state2 == LOW?

  1. How do i jump to the previous phase?

Previous to what?

Lets forget about the blinking led. Lets make it simpler:

This led that shows connections at the beggining is OFF, then i press the both buttons to start connection. After this i have 4 combinations: LOW LOW / LOW HIGH / HIGH LOW / HIGH HIGH. When one of these combinations is pressed (or not in the case of low low): the first led will turn ON and also the led of the DEVICE. After this, the same buttons will be used to select Program / Volume using 01 and 10 combinations. After chosing program or volume, i have another 4 combinations: LOW LOW / LOW HIGH / HIGH LOW / HIGH HIGH to increase/decrease volume, move to the next/previous program.

The problem is: how do i register that LOW LOW easily... i mean, the user must know when to not press anything, but in the same time, the program knows that he didn`t press anything and the LOW LOW is a working combination...

Xeyow:
The problem is: how do i register that LOW LOW easily... i mean, the user must know when to not press anything, but in the same time, the program knows that he didn`t press anything and the LOW LOW is a working combination...

You can't. "register" when? The user has to initiate some action. The machine can't read the users mind. The only time you can "register" 00 is when you enter that state from 01,10, or 11.

You can't pick one of 5 states with just two switches. The 5 states you seem to want are nothing selected, the TV selected, the DVD selected, the CD selected, or the RADIO selected.

Forget everything... I will use 5 buttons. 1 for each device and 1 for reset. After i connect to the device, 2 of the 4 buttons will be used to select programs/volume, after that, the 4 buttons will be used to select p+ / p- / v+ / v-... The 5th button will be used to jump backwards.

Any help with this?

Any help with this?

We're back to the "what help do you need?" stage.

I don`t know how to do it... i am using now 4 buttons for devices + 1 for jump. This is my code so far:

int b1 = 2;
int b2 = 3; 
int b3 = 4;
int b4 = 5;
int b5 = 6;

int ledCON = 12;
int ledTV = 11;
int ledDVD = 10;
int ledCD = 9;
int ledRAD = 8;

void setup() 
{
  pinMode(ledTV, OUTPUT);
  pinMode(ledDVD, OUTPUT);
  pinMode(ledCD, OUTPUT);
  pinMode(ledRAD, OUTPUT);
  pinMode(ledCON, OUTPUT);
  pinMode(b1, INPUT);
  pinMode(b2, INPUT); 
  pinMode(b3, INPUT); 
  pinMode(b4, INPUT); 
  pinMode(b5, INPUT);  
}

int bs1 = 0;
int bs2 = 0;
int bs3 = 0;
int bs4 = 0;
int bs5 = 0;

void loop() 
{
  bs1=digitalRead(b1);
  bs2=digitalRead(b2);
  bs3=digitalRead(b3);
  bs4=digitalRead(b4);
  if(bs1 == HIGH)
  {
    digitalWrite(ledRAD, HIGH);
    digitalWrite(ledCON, HIGH); 
  }
  else if(bs2 == HIGH)
       {
        digitalWrite(ledCD, HIGH);
        digitalWrite(ledCON, HIGH); 
       }
       else if(bs3 == HIGH)
            {
              digitalWrite(ledDVD, HIGH);
              digitalWrite(ledCON, HIGH); 
            }
            else if(bs4 == HIGH)
                 {
                  digitalWrite(ledTV, HIGH);
                  digitalWrite(ledCON, HIGH); 
                 }
}

and i don`t know how to continue from here

Xeyow:
i am using now 4 buttons for devices + 1 for jump.

What does that mean, "one for jump?" Ah, "jump backwards"? To where? From where?

Let`s say i connect to the TV. The tv has 2 modes: PROGRAM and VOLUME. If i select PROGRAM, the JUMP button will take me to the 2 modes of the TV. So i can select again PROGRAM or VOLUME. If i am already at TV, and i press JUMP again, it takes me to the connection process, where i choose: TV / RAD / DVD / CD.

i solved this one...

      if(digitalRead(b5)==HIGH)
        turnOff();

Take these lines of code out of the switch case and into the main body of loop() maybe, but you will also need to reset the device variable.

const int b1 = 2;
const int b2 = 3; 
const int b3 = 4;
const int b4 = 5;
const int b5 = 7;

int ledCON = 12; //conexiune
int ledTV = 11;
int ledDVD = 10;
int ledCD = 9;
int ledRAD = 8;
int ledPro = 15; //program
int ledVol = 17; //volum

void setup() 
{
  pinMode(ledTV, OUTPUT);
  pinMode(ledDVD, OUTPUT);
  pinMode(ledCD, OUTPUT);
  pinMode(ledRAD, OUTPUT);
  pinMode(ledCON, OUTPUT);
  pinMode(ledPro, OUTPUT);
  pinMode(ledVol, OUTPUT);
  pinMode(b1, INPUT);
  pinMode(b2, INPUT); 
  pinMode(b3, INPUT); 
  pinMode(b4, INPUT); 
  pinMode(b5, INPUT);
}

int bs1 = 0;
int bs2 = 0;
int bs3 = 0;
int bs4 = 0;
int bs5 = 0;

void loop() 
{
  bs1=digitalRead(b1);
  bs2=digitalRead(b2);
  bs3=digitalRead(b3);
  bs4=digitalRead(b4);
  int device = bs1 + bs2*2 + bs3*4 + bs4*8;
  switch(device)
  {
    case 1:
      digitalWrite(ledCON, HIGH);
      digitalWrite(ledRAD, HIGH);
      break;
    case 2:
      digitalWrite(ledCON, HIGH);
      digitalWrite(ledCD, HIGH);
      break;
    case 4:
      digitalWrite(ledCON, HIGH);
      digitalWrite(ledDVD, HIGH);
      break;
    case 8:
      digitalWrite(ledCON, HIGH);
      digitalWrite(ledTV, HIGH);
      break;
  }
}

void turnOff()
{
  static int lastbs5 = LOW;
      bs5=digitalRead(b5);
      if((bs5==HIGH)&&(bs5!=lastbs5))
      {
        digitalWrite(ledTV, LOW);
        digitalWrite(ledDVD, LOW);
        digitalWrite(ledRAD, LOW);
        digitalWrite(ledCD, LOW);
        digitalWrite(ledCON, LOW);
      }
      lastbs5=bs5;
}

I-ll be working on this code from now on. I got stuck at this: After i connect to a device, it let`s me connect to the 2nd also... But this remote must be connected to only one device at a time... How do i "reject" other connections if the remote is already connected?

Last problem, and i thing the topic can be closed...

const int b1 = 2;
const int b2 = 3; 
const int b3 = 4;
const int b4 = 5;
const int b5 = 7;

int ledCON = 12; //conexiune
int ledTV = 11;
int ledDVD = 10;
int ledCD = 9;
int ledRAD = 8;
int ledPro = 15; //program
int ledVol = 16; //volum

void setup() 
{
  pinMode(ledTV, OUTPUT);
  pinMode(ledDVD, OUTPUT);
  pinMode(ledCD, OUTPUT);
  pinMode(ledRAD, OUTPUT);
  pinMode(ledCON, OUTPUT);
  pinMode(ledPro, OUTPUT);
  pinMode(ledVol, OUTPUT);
  pinMode(b1, INPUT);
  pinMode(b2, INPUT); 
  pinMode(b3, INPUT); 
  pinMode(b4, INPUT); 
  pinMode(b5, INPUT);
}

int bs1 = 0;
int bs2 = 0;
int bs3 = 0;
int bs4 = 0;
int bs5 = 0;

void loop() 
{
  read_bs();
  int device = bs1 + bs2*2 + bs3*4 + bs4*8;
  int mode;
  int nr;
  switch(device)
  {
    case 1:
      digitalWrite(ledCON, HIGH);
      digitalWrite(ledRAD, HIGH);
      reset_bs(); 
      nr=0;
      while(nr<2)
      {
        read_bs();
        mode = bs1 + bs2*2 + bs3*4 + bs4*8;
        if(mode==3)
          while(nr<1)
          {
            digitalWrite(ledVol, HIGH);
            if(digitalRead(b5)==HIGH)
              nr++;
            if(nr==1)
              digitalWrite(ledVol, LOW);
          }
        if(mode==12)
          while(nr<1)
          {
            digitalWrite(ledPro, HIGH);
            if(digitalRead(b5)==HIGH)
              nr++;
            if(nr==1)
              digitalWrite(ledPro, LOW);
          }
        if(digitalRead(b5)==HIGH)
          nr++; 
      }
      break;
    case 2:
      digitalWrite(ledCON, HIGH);
      digitalWrite(ledCD, HIGH);
      reset_bs(); 
      nr=0;
      while(nr<1)
      {
        read_bs();
        mode = bs1 + bs2*2 + bs3*4 + bs4*8;
        if(mode==3)
          digitalWrite(ledPro, HIGH);
        else if(mode==6)
          digitalWrite(ledVol, HIGH);
        if(digitalRead(b5)==HIGH)
            nr++;
      }
      bs5=LOW;
      break;
    case 4:
      digitalWrite(ledCON, HIGH);
      digitalWrite(ledDVD, HIGH);
      reset_bs(); 
      nr=0;
      while(nr<1)
      {
        read_bs();
        mode = bs1 + bs2*2 + bs3*4 + bs4*8;
        if(mode==3)
          digitalWrite(ledPro, HIGH);
        else if(mode==6)
          digitalWrite(ledVol, HIGH);
        if(digitalRead(b5)==HIGH)
            nr++;
      }
      bs5=LOW;
      break;
    case 8:
      digitalWrite(ledCON, HIGH);
      digitalWrite(ledTV, HIGH);
      reset_bs(); 
      nr=0;
      while(nr<1)
      {
        read_bs();
        mode = bs1 + bs2*2 + bs3*4 + bs4*8;
        if(mode==3)
          digitalWrite(ledPro, HIGH);
        else if(mode==6)
          digitalWrite(ledVol, HIGH);
        if(digitalRead(b5)==HIGH)
            nr++;
      }
      bs5=LOW;
      break;
  }
  turnOff();
}
void read_bs()
{
  bs1=digitalRead(b1);
  bs2=digitalRead(b2);
  bs3=digitalRead(b3);
  bs4=digitalRead(b4);
}
void reset_bs()
{
  bs1=bs2=bs3=bs4=0;
}

void turnOff()
{
  static int lastbs5 = LOW;
  bs5=digitalRead(b5);
  if((bs5==HIGH)&&(bs5!=lastbs5))
  {
    digitalWrite(ledTV, LOW);
    digitalWrite(ledDVD, LOW);
    digitalWrite(ledRAD, LOW);
    digitalWrite(ledCD, LOW);
    digitalWrite(ledCON, LOW);
    digitalWrite(ledPro, LOW);
    digitalWrite(ledVol, LOW);
  }
  lastbs5=bs5;
}

If i connect to TV and press JUMP, it takes me to the connection process. If i connect to TV and then select, let`s say, VOLUME, if i press JUMP, it takes me again to the connection process. How can i make it to take me to the TV, and not at the beggining....

It`s related to nr and bs5, but i can not find the solution

How do i "reject" other connections if the remote is already connected?

Set a flag variable to true when you are connected and to false when you disconnect and only allow a new connection when the flag is false