Hi Guys
I wasn't sure where to post this question, as it could be a programming or electronics query. Apologies if its in the wrong forum and if a Mod could move it to the correct place - thanks!
Here's my problem:
I designed and made a pcb on which an ATTiny85 with 5v voltage regulation is installed. Connections are provided to enable connections to and from the data pins of the ATTiny (plus a 10k resistor holding the reset pin high). I programmed the ATTiny before soldering it to the board and all worked well in its role as a general purpose led controller.
Next I redesigned the board layout by adding 6 pins, to allow onboard programming, and an ULN2803 to enable the ATTiny to drive more leds. Again, the new design all worked well.
However, now I need to program the ATTiny with a sketch that fades from one set of leds to another, requiring an input (from a switch). I assumed I could use one of programming pins as an input, because the programming pins are connected to the ATTiny before the transistor array, but here's the problem - the switch has no effect. The switch was wired between a programming pin (MOSI, pin 0) and ground.
The sketch used is one that worked perfectly on a UNO, but when used with the ATTiny (with relevant pin number changes), it doesn't work at all. It seems that the switch input is not being seen. I know that the switch pin is being configured as an input ok, as I've invoked the internal pull-up resistor on that pin and the pin is definately being held high (there's sufficient voltage at the pin to drive an led) and definitely goes low when the switch is operated, so what I'm asking is, can anyone see if have I missed a blatantly obvious mistake in the (albeit rather naive) programming, or is it a wiring misdesign?
Here's the code:
/*
**************************************************************
* LED fade sequence using ATTiny85 *
**************************************************************
Physical Connections:
Red leds : + to B2+, - to B2- (OP pins)
Blue leds : + to St+, - to St- (OP pins)
Switch : across MOSI and 0V (Prog pins)
*/
// Declarations
int redPin = 1; // Red led o/p is connected to pin 1 (chip pin 6, pad B2)
int bluePin = 4; // Blue led o/p is connected to pin 4 (chip pin 3, pad St)
int WswitchPin = 0; // Switch is connected across pin 0 (chip pin 5, pin MOSI) and 0V
int Wval; // Variable for reading the switch pin status
int Wval2; // Variable for reading the switch delayed/debounced status
int WbuttonState; // Variable to hold the button state
int WMode = 0; // Variable to hold switch status
int SeqNo = 0; // Variable to hold next function selection
//SetUp
void setup() {
//Serial.begin(9600);
pinMode(WswitchPin, INPUT_PULLUP); // Set the switch pin as input and enable internal pull-up resistor
pinMode(redPin, OUTPUT); // Set the red pin for output
pinMode(bluePin, OUTPUT); // Set bluie Pin for output
digitalWrite(redPin, LOW); // Set red off
digitalWrite(bluePin, LOW); // Set blue off
}
void loop()
{
// TASKS
WMode = 0; // Set switch status to OFF
Wval = digitalRead(WswitchPin); // Read switch input value and store it in Wval
delay(10); // 10 milliseconds is a good amount of time to wait
Wval2 = digitalRead(WswitchPin); // Read the input again to check for bounces
if (Wval == Wval2) { // 2 consistant readings?
if (Wval != WbuttonState) { // The button state has changed
if (Wval == LOW) { // Is the button pressed?
if (WMode == 0) { // Is the switch status OFF?
WMode = 1; // Then turn status ON
++SeqNo; // Increment the task counter by 1
}
}
WbuttonState = Wval; // Save the new state in status variable
}
//Fade up to red
if (WMode == 1) { // Check to see if button has been pressed and debounced
if (SeqNo == 1) { // Is Task number 1 wanted?
for (int i = 0; i <= 255; i++){ // Fade red led from off to fully on
analogWrite(redPin, i);
delay (10);
}
}
//Fade to blue
else if (SeqNo == 2){ // Is Task number 2 wanted?
for (int i = 0; i <= 255; i++){ // Fade from red to blue
analogWrite(bluePin, i);
analogWrite(redPin, 255 - i);
delay (6);
}
}
// Fade to Impulse
else if (SeqNo == 3){ // Is Task number 3 wanted?
for (int i = 0; i <= 255; i++){ // Fade blue to red
analogWrite(bluePin, 255 - i);
analogWrite(redPin, i);
delay (6);
}
SeqNo = 1;
}
}
}
}
the circuit diagram is here:
Thanks
Bernie