Hey Guys!
My first arduino project and i’m currently writing a little program to be used on my bike for controlling the lights, the code is below -
#define GREENPIN 11
#define REDPIN 10
#define buttonPin 12
#define buttonPin2 8
int buttonCount=0;
char COLOUR;
int levels[0];
int arrayKey = 0;
int count = 0;
void setup() {
pinMode(REDPIN, OUTPUT);
pinMode(GREENPIN, OUTPUT);
pinMode(buttonPin,INPUT_PULLUP);
pinMode(buttonPin2,INPUT_PULLUP);
Serial.begin(9600);
}
void loop(){
int held=0;
int brightness=0;
if (buttonCount==0){
COLOUR = GREENPIN;
arrayKey = 11;
}else if (buttonCount==1){
COLOUR = REDPIN;
arrayKey = 10;
}//buttonCount being 2 is the normal state of operation
else if (buttonCount==3){
COLOUR = 0;
for (count=10;count<12;count++) {
//kill the lights!
levels[count] = 0;
}
buttonCount = 0;
}
while (digitalRead(buttonPin)==LOW){
held++;
//slow the fade up a little.
brightness=held/5;
Serial.print(brightness);
if (brightness < 252){
analogWrite(COLOUR,brightness);
}
if (brightness >253){
//max value reached, flash to show that.
analogWrite(COLOUR,0);
delay(300);
analogWrite(COLOUR,254);
delay(300);
brightness = 254;
}
//record that LEDs state.
levels[arrayKey] = brightness;
}
while (digitalRead(buttonPin2)==LOW){
analogWrite(GREENPIN,254);
analogWrite(REDPIN,254);
}
for (count=10;count<12;count++) {
//and have it flash
analogWrite(count,levels[count]);
delay(200);
analogWrite(count,0);
}
if(held>1){
buttonCount++;
}
}
the issue i have is if i take out the line -
Serial.print(brightness);
then the brightness variable just equals the held value, so scales up way to quickly! I can’t see any reason for that - anyone have any ideas? Any other comments/suggestions on the code are of course welcome
Thanks!
Chris