Go Down

Topic: pt2258 controller (Read 2316 times) previous topic - next topic

nrajan_2007

byte  VUP = 1;
byte  VDW = 0;
#include <Wire.h>
#include <LiquidCrystal.h>
#include <EEPROM.h>

char    tmp[30];
LiquidCrystal lcd(22,23,18,19,20,21);
unsigned int RC5addr = 0, RC5data = 0;
int8_t tmpdata;


 #define pt2258_addr 0x88
 #define LOGO "SUSEE ELECTRONICS"
 #define   VOLUME   "VOLUME      DB"

 #define   MAX_VDB   (0)
 #define   MIN_VDB   (80)
 #define   INC_VDB   (5)
   int vol;
   int vdb;
   int volume_up = 0;
   int volume_down = 0;
   int buttonState = 0;         // current state of the button
   int lastButtonState = 0;     // previous state of the button
   int button2State = 0;         // current state of the button
   int lastButton2State = 0;


void setup(){
   lcd.begin(16, 2);
   lcd.print("    VOLUME   ");
 
 Serial.begin(9600);
 Serial.println("Hello world!");
 CLKPR = (1<<CLKPCE);
 CLKPR = 0;

 
  pinMode(VUP,INPUT);
  pinMode(VDW,INPUT);
  Wire.begin();
 Wire.beginTransmission( pt2258_addr); // transmit to device #4
 Wire.send(0XC0);        // sends five bytes
 
 Wire.send(0xD7);
 Wire.send(0xE9);  // sends one byte  
 Wire.endTransmission();    // stop transmitting
 }
  void selectvolume(int vdb) {
  switch (vdb){
   
case 0:

   Wire.beginTransmission( pt2258_addr); // transmit to device #4
 
   Wire.send(0xD7);
   Wire.send(0xE9);  // sends one byte  
   Wire.endTransmission();    // stop transmitting
   lcd.setCursor(0,7);    
   lcd.print("zero");
break;

case 5:
  Wire.beginTransmission( pt2258_addr); // transmit to device #4

  Wire.send(0XD7);
  Wire.send(0XE5);  
  Wire.endTransmission();    // stop transmitting
break;

case 10:
 Wire.beginTransmission( pt2258_addr); // transmit to device #4

 Wire.send(0XD6);    
 Wire.send(0XE9);  
 Wire.endTransmission();    // stop transmitting
break;

case 15:
 Wire.beginTransmission( pt2258_addr); // transmit to device #
 Wire.send(0XD6);
 Wire.send(0XE5);    
 Wire.endTransmission();    // stop transmitting
break;

case 20:
 Wire.beginTransmission( pt2258_addr); // transmit to device #4
 Wire.send(0XD5);
 Wire.send(0XE9);
 Wire.endTransmission();    // stop transmitting
break;

case 25:
 Wire.beginTransmission( pt2258_addr); // transmit to device #4
 Wire.send(0XD5);
 Wire.send(0XE5);  
 Wire.endTransmission();    // stop transmitting
break;

case 30:
 Wire.beginTransmission( pt2258_addr); // transmit to device #4  
 Wire.send(0XD4);
 Wire.send(0XE9);  
 Wire.endTransmission();    // stop transmitting
break;

case 35:
 Wire.beginTransmission( pt2258_addr); // transmit to device #4
 Wire.send(0XD4);
 Wire.send(0XE5);
 Wire.endTransmission();    // stop transmitting
break;

case 40:
  Wire.beginTransmission( pt2258_addr); // transmit to device #4
  Wire.send(0XD4);
  Wire.send(0XE9);
  Wire.endTransmission();    // stop transmitting
break;

case 45:
 Wire.beginTransmission( pt2258_addr); // transmit to device #4
 Wire.send(0XD3);
 Wire.send(0XE5);
 Wire.endTransmission();    // stop transmitting
break;

case 50:
 Wire.beginTransmission( pt2258_addr); // transmit to device #4
 Wire.send(0XD2);
 Wire.send(0XE9);
 Wire.endTransmission();    // stop transmitting
break;

case 55:
 Wire.beginTransmission( pt2258_addr); // transmit to device #4
 Wire.send(0XD2);              // sends one byte
 Wire.send(0XE5);  
 Wire.endTransmission();    // stop transmitting
break;

case 60:
 Wire.beginTransmission( pt2258_addr); // transmit to device #4
 Wire.send(0XD1);              // sends one byte
 Wire.send(0XE9);
 Wire.endTransmission();    // stop transmitting
break;


case 65:

 Wire.beginTransmission( pt2258_addr); // transmit to device #
 Wire.send(0XD1);              // sends one byte
 Wire.send(0XE5);  
 Wire.endTransmission();    // stop transmitting
break;

case 70:
 Wire.beginTransmission( pt2258_addr); // transmit to device #4
 Wire.send(0XD1);              // sends one byte
 Wire.send(0XE1);  
 Wire.endTransmission();    // stop transmitting
break;

case 75:
 Wire.beginTransmission( pt2258_addr); // transmit to device #4
 Wire.send(0XD0);              // sends one byte
 Wire.send(0XE5);
 Wire.endTransmission();    // stop transmitting
 lcd.setCursor(0,7);    
 lcd.print(" max");
   break;
 
 }
  }  
   
 void checkbuttons(){
    buttonState = digitalRead(VDW);
    button2State = digitalRead(VUP);
 
 
   if (buttonState != lastButtonState) {
   
   if (buttonState == HIGH) {
   vdb += INC_VDB;
   vdb = constrain(vdb, 0, 80);

 
   Serial.print("Counter value: ");
   Serial.println(vdb, DEC);
   selectvolume(vdb);
 
   lcd.setCursor(0,7);
   lcd.print("     ");
   lcd.setCursor(0,7);    
   lcd.print(vdb,DEC);
 
   
       }
       else
       {
      
       }


   
   lastButtonState = buttonState;
 }
 
  if (button2State != lastButton2State) {
   
   if (button2State == HIGH) {
   

      vdb -= INC_VDB;
   vdb = constrain(vdb, 0, 80);

   tmpdata = vdb ;
   selectvolume(vdb);
   Serial.print("Counter value: ");
   Serial.println(vdb, DEC);
 
 
   lcd.setCursor(0,7);
   lcd.print("     ");
   lcd.setCursor(0,7);    
   lcd.print( vdb,DEC);
 
 
       }
       else
       {
      
       }
 

   
   lastButton2State = button2State;
 }


  }
void loop (){
 checkbuttons();
}

robtillaart

PLease modify your post, select the code and press the # button so proper tags are included... Looks so much better..
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

B-TECH

Do you have a Schematic for this setup?
Wouldn't mind building this myself..

Thanks.
Computer says "No".. :-P

robtillaart

there is a conflict between comments and code (code always wins)
Code: [Select]

   Wire.begin();
  Wire.beginTransmission( pt2258_addr); // transmit to device #4
  Wire.send(0XC0);        // sends five bytes
 
  Wire.send(0xD7);
  Wire.send(0xE9);  // sends one byte 
  Wire.endTransmission();    // stop transmitting
  }


the comment says 5 bytes or one byte but in fact you are sending 3.


if you divide  vdb by 5 your switch statement will get the numbers 0,1,2,3,4,5 .. 25;

Than enables you to refactor the commands in an array commands, something like

Code: [Select]
byte commands[25][2] = { {D7, E9 }, { , }  .... { , } } // numbers to be filled in

and make a function

Code: [Select]
sendCommand(byte cmd[2])
{
  Wire.beginTransmission( pt2258_addr); 
  Wire.send(cmd[0]);
  Wire.send(cmd[1]);  // sends one byte 
  Wire.endTransmission();
}


The switch will become simpler

Code: [Select]

void selectVolume(int vdb)
{
  int idx = vdb/5;
  sendCommand( commands[idx] );

  if (vdb == 75)
  {
    lcd.setCursor(0,7); 
    lcd.print(" max");
  }
}

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up