Control volume of speaker by Photocell

Hi everyone,
I recently got an Arduino UNO kit and started to learn how to use it into my project.
My idea is to build a speaker whose volume can be controlled by Photocells.
Ex: There are total 4 photocells and each one represents one level of volume.
So, if photocell #1 detects the light under Min light, it will increase one level of the volume, and so on..

I bought a portable speaker which has line-in and can read music sources from laptop or cellphone. And I took its power supply's wire connecting to the Arduino.
I was thinking to parallel different resistors with the speaker, so that I can have different levels of volume.
But it didn't really work...
So, can anyone give me some suggestions or maybe tutorials about how to change volume of a speaker...

And here's my code...

const int aSensorPin = A0; // a (photocell)  anallog pin A0
const int bSensorPin = A1; // b (photocell)  anallog pin A1
const int cSensorPin = A2; // c (photocell)  anallog pin A2
const int xSensorPin = A3; // x (photocell)  anallog pin A3
int aSensorVal = 0; // photocell variable
int bSensorVal = 0;
int cSensorVal = 0;
int xSensorVal = 0;

int minLight = 380;   // 

int dSpeakerPin = 10;
int eSpeakerPin = 11;
int fSpeakerPin = 12;


void setup() {

  for(int pinNumber = 10; pinNumber<13; pinNumber++){
    pinMode(pinNumber,OUTPUT);
    digitalWrite(pinNumber, LOW);
  }

  Serial.begin(9600);

}

 

void loop(){

  // Serial Port

  aSensorVal = analogRead(aSensorPin);
  delay(5);
  bSensorVal = analogRead(bSensorPin);
  delay(5);
  cSensorVal = analogRead(cSensorPin);
  delay(5);
  xSensorVal = analogRead(xSensorPin);
  delay(5);

  Serial.print("raw sensor Values \t a: ");
  Serial.print(aSensorVal);
  Serial.print("\t b: ");
  Serial.print(bSensorVal);
  Serial.print("\t c: ");
  Serial.print(cSensorVal);
  Serial.print("\t x: ");
  Serial.println(xSensorVal);

   

  // 1.a:x b:x c:x x:o

  if (aSensorVal > minLight && bSensorVal > minLight && cSensorVal > minLight && xSensorVal < minLight) {

    digitalWrite(10, LOW); // turn off 
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    
  }// 2.a:o b:x c:x x:o
  else if (aSensorVal < minLight && bSensorVal > minLight && cSensorVal > minLight && xSensorVal < minLight) {

    digitalWrite(10, HIGH); // turn on pin12
    digitalWrite(11, HIGH);
    digitalWrite(12, HIGH);
    
  }// 3.a:x b:o c:x x:o
  else if (aSensorVal > minLight && bSensorVal < minLight && cSensorVal > minLight && xSensorVal < minLight) {

    digitalWrite(10, HIGH); // turn on pin12
    digitalWrite(11, HIGH);
    digitalWrite(12, HIGH);
    
    }// 4.a:x b:x c:o x:o
  else if (aSensorVal > minLight && bSensorVal > minLight && cSensorVal < minLight && xSensorVal < minLight) {

    digitalWrite(10, HIGH); // turn on pin12  
    digitalWrite(11, HIGH);
    digitalWrite(12, HIGH);
    
    }// 5.a:o b:o c:x x:o
  else if (aSensorVal < minLight && bSensorVal < minLight && cSensorVal > minLight && xSensorVal < minLight) {

    digitalWrite(10, HIGH); // turn on pin11  
    digitalWrite(11, HIGH);
    digitalWrite(12, LOW);
    
     }// 6.a:o b:x c:o x:o
  else if (aSensorVal < minLight && bSensorVal > minLight && cSensorVal < minLight && xSensorVal < minLight) {

    digitalWrite(10, HIGH); // turn on pin11  
    digitalWrite(11, HIGH);
    digitalWrite(12, LOW);
    
     }// 7.a:x b:o c:o x:o
  else if (aSensorVal > minLight && bSensorVal < minLight && cSensorVal < minLight && xSensorVal < minLight) {

    digitalWrite(10, HIGH); // turn on pin11 
    digitalWrite(11, HIGH);
    digitalWrite(12, LOW);
    
     }// 8.a:o b:o c:o x:o
  else if (aSensorVal < minLight && bSensorVal < minLight && cSensorVal < minLight && xSensorVal < minLight) {

    digitalWrite(10, HIGH); // turn on pin10 
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    
    }// 9.a:o b:o c:o x:x
  else if (aSensorVal < minLight && bSensorVal < minLight && cSensorVal < minLight && xSensorVal > minLight) {

    digitalWrite(10, LOW); // turn on pin12  
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    
    }// 10.a:o b:o c:x x:x
  else if (aSensorVal < minLight && bSensorVal < minLight && cSensorVal > minLight && xSensorVal > minLight) {

    digitalWrite(10, LOW); // turn on pin12  
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    
     }// 11.a:o b:x c:o x:x
  else if (aSensorVal < minLight && bSensorVal > minLight && cSensorVal < minLight && xSensorVal > minLight) {

    digitalWrite(10, LOW); // turn on pin12 
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    
     }// 12.a:x b:o c:o x:x
  else if (aSensorVal > minLight && bSensorVal < minLight && cSensorVal < minLight && xSensorVal > minLight) {

    digitalWrite(10, LOW); // turn on pin12  
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    
     }// 13.a:o b:x c:x x:x
  else if (aSensorVal < minLight && bSensorVal > minLight && cSensorVal > minLight && xSensorVal > minLight) {

    digitalWrite(10, LOW); // turn on pin12   
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    
    }// 14.a:x b:o c:x x:x
  else if (aSensorVal > minLight && bSensorVal < minLight && cSensorVal > minLight && xSensorVal > minLight) {

    digitalWrite(10, LOW); // turn on pin12  
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    
    }// 15.a:x b:x c:o x:x
  else if (aSensorVal > minLight && bSensorVal > minLight && cSensorVal < minLight && xSensorVal > minLight) {

    digitalWrite(10, LOW); // turn on pin12   
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    
    }// 16.a:x b:x c:x x:x
  else if (aSensorVal > minLight && bSensorVal > minLight && cSensorVal > minLight && xSensorVal > minLight) {

    digitalWrite(10, LOW); // turn off
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    }

   

  delay(100);      

}

You need a chip called a digital pot.

Thanks Grumpy_Mike ,

Are these the digital potentiometer that you mentioned?

Sorry, I am still not really familiar with Arduino and coding...
could you name one digital potentiometer so that i can get from internet or from stores...
And if you can share a sketch about how to use it to control the volume, I will be really appreciated...

Are these the digital potentiometer that you mentioned?

Yes.

well the AD5206 in the tutorial is as good as ant but there are lots.

There are nearly 300 to choose from here:-

And if you can share a sketch about how to use it to control the volume

Are those tutorials not enough?
Basically you connect one end to to ground, the other to the audio in you want to control and the wiper to the audio output. Look at the data sheets for some examples.