Help. I do not understand why this code does not work

First of all thanks to anyone who takes the time to read and respond to this.
I am using the ATiny85 USB board to make and control functions in a Model Railway(Railroad) dcc system. The basis of this sytem was developed by a guy in the netherlands Ruud Boer and the link to this topic is given here:

Essentially I am trying to turn on and off two bi-colour (red/white) 5v two lead-out leds. As a start I used the following simple piece of code.
int leds1=0;
int leds2=3;
int leds3=1;
int leds4=4;

//All pins are capable of Digital output, though P5 is 3 V at HIGH instead of 5 V
// pinMode(0, OUTPUT); //0 is P0, 1 is P1, 2 is P2, etc. - unlike the analog inputs, for digital outputs the pin number matches.
void setup()
pinMode (leds1, OUTPUT);
pinMode (leds2, OUTPUT);
pinMode (leds3, OUTPUT);
pinMode (leds4, OUTPUT);

void loop()
digitalWrite(leds1, HIGH);
digitalWrite(leds2, LOW);
digitalWrite(leds3, HIGH);
digitalWrite(leds4, LOW);


digitalWrite(leds2, HIGH);
digitalWrite(leds1, LOW);
digitalWrite(leds4, HIGH);
digitalWrite(leds3, LOW);
And sure enough this turns the red and white on in turn, with a slight delay and since I did not use any current limiting resistors I got good bright light when the leds lit.

So now I needed to integrate this simple code into the more complex code which involves the digital command and control. The base code is attached as the file Base code:
I then modified the base code to the version Class 29 and the code works but there are clearly problems which i do not understand and I could do with some help to try and find where the problem lies:
I am using Pins 0 & 3 and 1 & 4 as the two pairs to Output.
How it then works is that I hook the ATTiny through a circuit board which supplies 5v to the ATTiny85 and the dcc signal from the track through an optocoupler circuit. As illustrated in the picture. When F9 is activated the LED goes red but with no real brightness when F10 is activated then the white light is as expected. The same is true for the f11 and F12 functions. If I reverse the polarity of the leds then the red is bright and the white dim. It would appear that the problem is in the code not the circuit. Clearly I should have much the same brightness in both conditions.
The two aspect I do not understand are:
At line 91 and 112 I have a condition that if both f9 and f10 and f11 and f12 are both activated set all values to low. This is to avoid trying to light both of the colours at the same time.
if ((Func[2]&B00000001) && (Func[2]&B00000010)) {
That does not appear to work can anyone see what error I have made in the code?
What is causing the led to be dim. In the F9 condition activated i.e Pin 0 HIGH and Pin 3 LOW and also in the F11 condition Pin 1 HIGH and Pin 4 LOW the leds are dim and the measured volatge is about 1v not 5v. It worksin the simple code why not in the more complex code scenario.

Any help or insight would be greatly appreciated

Base code.pdf (69.5 KB)

Class 29.pdf (70.4 KB)

Your topic was Moved to it's current location / section as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.


Please follow the advice on posting code given in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

If the code exceeds the 9000 character inline limit then attach it to a post

Without current limiting resistors you may have damaged the output pin(s).

Does the chip feel warm or hot to the touch? If so, you damaged the IO pin by not putting current limit resistors in series with the LEDs. Replace the chip or module.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.