Go Down

Topic: PRO needed! (Read 482 times) previous topic - next topic

omarico13

I am working on this project that I have 20 LEDs and 1 rgb connected to the arduino and controlled by the IR receiver. I am still adding features to it but I had this idea that I could not code.

I am using the Mega 2560.

Here is the script...

Code: [Select]

#include "IRremote.h"

#define PIN_COUNT 23


#define Receiver 11
#define RedOne 22
#define YellowOne 23
#define GreenOne 24
#define BlueOne 25
#define WhiteOne 26
#define RedTwo 27
#define YellowTwo 28
#define GreenTwo 29
#define BlueTwo 30
#define WhiteTwo 31
#define RedThree 32
#define YellowThree 33
#define GreenThree 34
#define BlueThree 35
#define WhiteThree 36
#define RedFour 37
#define YellowFour 38
#define GreenFour 39
#define BlueFour 40
#define WhiteFour 41
#define rgbRed 3
#define rgbBlue 6
#define rgbGreen 5

int allPins[PIN_COUNT] = {22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 3, 5, 6, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41};
int state[PIN_COUNT] = {LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};
int currentPointerPosition = -1;


bool powerButtonStatus = false;

IRrecv irrecv(Receiver);    
decode_results results;    

void ApplyState()
{
  for (int i = 0; i <PIN_COUNT; i++)
  {
  digitalWrite(allPins[i], state[i]);
  }
}

void MoveRight()
{
  currentPointerPosition++;
  if (currentPointerPosition == 23)
    currentPointerPosition = 0;
    
  for (int i = 0; i <PIN_COUNT; i++)
  {
  if (currentPointerPosition == i)
    state[i] = HIGH;
  else
    state[i] = LOW;
  }
  
  ApplyState();
}

void MoveLeft()
{
  currentPointerPosition--;
  if (currentPointerPosition <0)
    currentPointerPosition = 22;
    
  for (int i = 0; i <PIN_COUNT; i++)
  {
  if (currentPointerPosition == i)
    state[i] = HIGH;
  else
    state[i] = LOW;
  }
  
  ApplyState();
}


void TurnAllOff()
{
  for (int i = 0; i <PIN_COUNT; i++)
  {
  state[i] = LOW;
  }
  ApplyState();
}

void TurnAllOn()
{
  for (int i = 0; i <PIN_COUNT; i++)
  {
  state[i] = HIGH;
  }
}



void translateIR()
{
  switch(results.value)
  {
  case 0xFFA25D: TogglePowerButton(); break;
  case 0xFFE21D: Serial.println("FUNC/STOP"); break;
  case 0xFF629D: Serial.println("VOL+"); break;
  case 0xFF22DD: MoveRight();    break;
  case 0xFFC23D: MoveLeft();   break;
  case 0xFF02FD: Serial.println("PAUSE");    break;
  case 0xFFE01F: Serial.println("DOWN");    break;
  case 0xFFA857: Serial.println("VOL-");    break;
  case 0xFF906F: Serial.println("UP");    break;
  case 0xFF9867: Serial.println("EQ");    break;
  case 0xFFB04F: Serial.println("ST/REPT");    break;
  case 0xFF6897: Serial.println("0");    break;
  case 0xFF30CF: Serial.println("1");    break;
  case 0xFF18E7: Serial.println("2");    break;
  case 0xFF7A85: Serial.println("3");    break;
  case 0xFF10EF: Serial.println("4");    break;
  case 0xFF38C7: Serial.println("5");    break;
  case 0xFF5AA5: Serial.println("6");    break;
  case 0xFF42BD: Serial.println("7");    break;
  case 0xFF4AB5: Serial.println("8");    break;
  case 0xFF52AD: Serial.println("9");    break;  
  default: Serial.println(" other button   ");
  }
  delay(500);
}
void setup()  
{
  Serial.begin(9600);
  Serial.println("IR Receiver Button Decode");
  irrecv.enableIRIn();
  
  for (int i = 0; i <PIN_COUNT; i++)
  {
  pinMode(allPins[i], OUTPUT);
  }
}

void TogglePowerButton()
{
  if (powerButtonStatus)
  {
    TurnAllOff;
    powerButtonStatus = false;
  }
  else
  {
    TurnAllOn;
    powerButtonStatus = true;
  }
  
}
void loop()
{
  if (irrecv.decode(&results))
  {
    translateIR();
    irrecv.resume();
  }  
}


The MoveRight and MoveLeft functions turn one LED at a time. BTW I know that the defining is not necessary but I wrote it so I can remember which pins do what. And I know that the translate cases will not do anything except on the COM but kept it there so I can know which button is which.

My idea is that the pause button keeps the LED that is on HIGH as it is (keeps it HIGH but lets the user mess with the other LEDs until the power button is clicked again).

Sorry about my crappy coding but I am 13 and I got the arduino a few weeks ago so I have 0 expericence :smiley-confuse:

wildbill

I suggest that you allow your state array to have three possible values: LOW, HIGH and LOCKED_HIGH.

You will need to avoid having routines such as MoveRight etc. change any led from LOCKED_HIGH. Since you touch the state array directly in so many routines, that'll be painful so create a function SetState that takes an array index and a state and have the check for LOCKED_HIGH happen there.

Then make sure that all your other functions only change state values via SetState.

Go Up