Help with my code!

Hi everyone. Im a beginner in Arduino. I tried the basic examples like blinking led, controlling led’s brightness using potentiometer and it turned out quite well. But this activity that I’m doing now is somewhat difficult for me, i tried all my efforts but still it won’t work. =( Can you please help me what’s wrong with my code please? =(

Materials: Three Leds, Three 2N2222 NPN Transistor, Three 220 ohms resistor, 1 10K Potentiometer

What I want to do: There are three leds and three transistors. When i turn the knob of the potentiometer in an increasing manner, the first and second led blinks but their brightness must be dependent from the potentiometer whether increasing or decreasing then after a while, let us say 50 mS, the first led must be off, the second led is blinking, the third led is blinking but their brightness must depend to the potentiometer whether increasing or decreasing. Then again: after let us say 50 ms, second led is off, third led is blinking and first led is blinking but their brightness must depend to the potentiometer.

But when i turn the potentiometer in a decreasing manner, the brightness of the leds must decrease but still they are blinking in an alternate manner.

Its like blinking and increasing/decreasing its brightness IN ONE MANNER. The blinking is constant and doesn’t depend on the potentiometer but the brightness does.

Something like this:

Cycle 1: LED 1 BLINKS, LED 2 BLINKS, LED 3 OFF
Cycle 2: LED 1 OFF, LED 2 BLINKS, LED 3 BLINKS
Cycle 3: LED 1 BLINKS, LED 2 OFF, LED 3 BLINKS

Blinking is constant. Whether or not I turn the knob of the potentiometer, it should still blink alternately. Potentiometer is for brightness of the led only.

int TransistorpinA = 3;			// Base of the transistor A connected at pin 3	
int TransistorpinB = 5;			// Base of the transistor B connected at pin 3	
int TransistorpinC = 6;			// Base of the transistor c connected at pin 3	
		

int PotPin=A0;			// Potentiometer connected at analog pin A0
int val=0; 			// Store value from potentiometer
                                        		
void setup()
{
pinMode(TransistorpinA,OUTPUT);	// Transistor A as output
pinMode(TransistorpinB,OUTPUT);	// Transistor B as output
pinMode(TransistorpinC,OUTPUT);	// Transistor C as output

pinMode(PotPin,INPUT);		// Potentiometer as input
}

void loop()
{

val = analogRead(PotPin);

digitalWrite(TransistorpinA, HIGH);   			
delay(50);                  				
digitalWrite(TransistorpinA, LOW);    				
delay(50);

digitalWrite(TransistorpinB, HIGH);   			
delay(50);                  				
digitalWrite(TransistorpinB, LOW);    				
delay(50);

digitalWrite(TransistorpinC, HIGH);   			
delay(50);                  				
digitalWrite(TransistorpinC, LOW);    				
delay(500);

}
for (int i=0; i<=255; i++){
analogWrite(TransistorpinA, i);					
}
for (int i=255; i>=0; i--){
analogWrite(TransistorpinA, i);
}
for (int i=0; i<=255; i++){
analogWrite(TransistorpinB, i);	
}
for (int i=255; i>=0; i--){
analogWrite(TransistorpinB, i);	
}
for (int i=0; i<=255; i++){
analogWrite(TransistorpinC, i);	
}
for (int i=255; i>=0; i--){
analogWrite(TransistorpinC, i);
 }

Help me with my code. I know its wrong. =( Im a newbie in arduino. HELP ME PLEASE. =(

delay(50);                                 
digitalWrite(TransistorpinC, LOW);                    
delay(500);

The blinking is constant because you're using constants in your delays. Use variables, or better yet, look at the blink without delay tutorial to get an idea of how to eliminate calls to "delay()" altogether.

(Arrays would shorten your code too)

I can't do YouTube at work so I can't give you a direct link but search for SciGuy14. He does a series of 14 tutorials for the Arduino. One of them is how to control LED brightness using a Pot. Get one working and then expand the code to include the other two.

The rest of his tutorials are fantastic so check them all out. I learned a ton. I have had my Arduino for less than a week and this weekend I had it reading an RFID tag, switching 'modes' with a tactile button, displaying that on LED's as well as through the terminal server. All of this was made possible by watching all of his tutorials.

Luck,

Wade

That's right. The blinking must be constant. Can someone help me with my code? It doesnt work. =(

It doesn't compile as it stands, due to all the for loops after loop().

You are reading the value of the potPin, but never doing anything with it. Instead of the digitalWrites, you should be using analogWrite using val as a parameter...

analogWrite(TransistorpinA, val);

You will also need to scale the value - analogRead() returns values from 0-1023, analogWrite accepts 0-255, so a divide by 4 will do.

  val = analogRead(PotPin)/4;

^ Thanks for the suggestion. I tried it but still it wont work. Can somebody help me with this? =( I want to make the blinking constant but makes the brightness variable. Help. :blush:

Show the code you currently have.

int TransistorpinA = 3;			// Base of the transistor A connected at pin 3	
int TransistorpinB = 5;			// Base of the transistor B connected at pin 3	
int TransistorpinC = 6;			// Base of the transistor c connected at pin 3	
		

int PotPin=A0;			// Potentiometer connected at analog pin A0
int val=0; 			// Store value from potentiometer
                                        		
void setup()
{
pinMode(TransistorpinA,OUTPUT);	// Transistor A as output
pinMode(TransistorpinB,OUTPUT);	// Transistor B as output
pinMode(TransistorpinC,OUTPUT);	// Transistor C as output

pinMode(PotPin,INPUT);		// Potentiometer as input
}

void loop()
{

val = analogRead(PotPin);

digitalWrite(TransistorpinA, HIGH);   			
delay(50);                  				
digitalWrite(TransistorpinA, LOW);    				
delay(50);

digitalWrite(TransistorpinB, HIGH);   			
delay(50);                  				
digitalWrite(TransistorpinB, LOW);    				
delay(50);

digitalWrite(TransistorpinC, HIGH);   			
delay(50);                  				
digitalWrite(TransistorpinC, LOW);    				
delay(500);

}
for (int i=0; i<=255; i++){
analogWrite(TransistorpinA, i);					
}
for (int i=255; i>=0; i--){
analogWrite(TransistorpinA, i);
}
for (int i=0; i<=255; i++){
analogWrite(TransistorpinB, i);	
}
for (int i=255; i>=0; i--){
analogWrite(TransistorpinB, i);	
}
for (int i=0; i<=255; i++){
analogWrite(TransistorpinC, i);	
}
for (int i=255; i>=0; i--){
analogWrite(TransistorpinC, i);
 }

I'm pretty sure that doesn't even compile (I think this was pointed out earlier), so it can't be said to not work. Move the "for" loops INSIDE the braces for "loop ()"/

That's the same as the first time. You're still using digitalWrites for the LEDs, so the brightness won't vary.

Try this…

int TransistorpinA = 3;			// Base of the transistor A connected at pin 3	
int TransistorpinB = 5;			// Base of the transistor B connected at pin 3	
int TransistorpinC = 6;			// Base of the transistor c connected at pin 3	


int PotPin=A0;			// Potentiometer connected at analog pin A0
int val=0; 			// Store value from potentiometer

void setup()
{
  pinMode(TransistorpinA,OUTPUT);	// Transistor A as output
  pinMode(TransistorpinB,OUTPUT);	// Transistor B as output
  pinMode(TransistorpinC,OUTPUT);	// Transistor C as output

  pinMode(PotPin,INPUT);		// Potentiometer as input
}

void loop()
{

  val = analogRead(PotPin)/4;

  digitalWrite(TransistorpinA, val);   			
  delay(50);                  				
  digitalWrite(TransistorpinA, 0);    				
  delay(50);

  digitalWrite(TransistorpinB, val);   			
  delay(50);                  				
  digitalWrite(TransistorpinB, 0);    				
  delay(50);

  digitalWrite(TransistorpinC, val);   			
  delay(50);                  				
  digitalWrite(TransistorpinC, 0);    				
  delay(500);
}

It compiles, at least.

Thanks. Will this code makes the led blinks alternately also? Im really sorry. Im a newbie and I'm having a hard time on the basics. =(

Not in the pattern you are after. You'll need to maintain a variable so that you know which led should be off. Look at the 'blink without delay' example to learn how to do things based on elapsed time.

Do you have an example code related to it? =(

Do you have an example code related to it?

You already have it - it is built into the IDE, under “File”/“Examples”