0

I'm building a state machine using 6 flip-flopsD, and an G input coming from an infrared sensor. This circuit should count from 0-35. The problem is that it always starts in state 111111. That is, after it passes through Flip-flop D A, B, C, D, E, F = 1;

Here's my code:

#include "flipflopD.h"


boolean a1,b1,c1,d1,e1,g1;//display 1
boolean a2,b2,c2,d2,e2,f2,g2;//display 2
boolean Aa=false;
boolean Bb=false;
boolean Cc=false;
boolean Dd=false;
boolean Ee=false;
boolean Ff=false;
boolean A=false;
boolean B=false;
boolean C=false;
boolean D=false;
boolean E=false;
boolean F=false;
boolean G=false;//receiver


void setup() {

    //DISPLAYS
    pinMode(A0, OUTPUT);
    pinMode(2, OUTPUT);
    pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
    pinMode(5, OUTPUT);
    pinMode(6, OUTPUT);
    pinMode(7, OUTPUT);
    pinMode(8, OUTPUT);
    pinMode(9, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(11, OUTPUT);
    pinMode(12, OUTPUT);
    pinMode(13, OUTPUT);


    pinMode(A2, INPUT);// IR RECEIVER
}

void loop() {
    //  Counter
    G = digitalRead(A2);
    Aa = flipflopD((A||!B||!C||!D && !A||B||C||D||E||F||G), A, G);
    Bb = flipflopD((!A||B||!C||G && !A||B||F||!G && !A||B||E||!F && !A||B||D||!E && !A||B||C||!D && !A||!B||C||D||E||F||G), B, G);
    Cc = flipflopD((!A||C||!D && !A||C||!E && !A||C||!F && !A||C||!G && !A||!C||D||E||F||G), C,G);
    Dd = flipflopD((!A||D||!E && !A||D||!F && !A||D||!G && !A||!B||!C||D && !A||!D||E||F||G && !A||!B||C||!E||!F||!G), D, G);
    Ee = flipflopD((!A||E||!F && !A||E||!G && !A||!E||F||G && A||!B||!C||!D||E && !B||!C||!D||!E||F||G), E, G);
    Ff = flipflopD((!A||!F||G && !A||F||!G && !B||!C||!D||!F||G && !B||!C||!D||F||!G && A||!B||!C||!D||E||G), F, G);

    A=Aa;
    B=Bb;
    C=Cc;
    D=Dd;
    E=Ee;
    F=Ff;


    //   ABCDEF TO 7 Segments
    a1 = !A||B||D && !A||B||C & A||!B||!C||!D;
    b1 = !A||B && !A||C||E && !A||C||D && A||!B||!C||!D;
    c1 = !A||!B||C||E && !A||!B||C||D && !A||C||D||E && !A||B||!C||!D + A||!B||!C||!D;
    d1 = !A||B||D && !A||B||C && A||!B||!C||!D;
    e1 = !A||B||!C||D && !A||B||D||!E && !A||B||C||!D;
    g1 = !A||B||D && !A||B||C & A||!B||!C||!D;

    a2 = (!A||!C||E  &&  !A||!B||!D||!F  &&  !A||!B||D||F && !A||!B||C||!E && !A||C||D||!E && !A||B||!C||!F && !A||B||!D||F && !A||B||E||!F && !B||!C||!D||E||F && A||!B||!C||!D||!E);
    b2 = (!A||B||D && !B||!C||!D||!E && !B||!C||!D||!F && !A||!B||!E||!F && !A||!C||!D||F && !A||!C||!D||E && !A||!C||E||F && !A||!D||E||F && !A||!B||C||!E && !A||!B||C||!F && !A||C||!E||!F);
    c2 = (!A||F && !A||!D||!E && !A||C||E && !A||B||!D && !A||B||!E && !B||!C||!D||F && !A||!B||!C||D && A||!B||!C||!D||E);
    d2 = (!A||!B||!C||!D||!F && !A||!B||!C||E||!F && !B||!C||!D||E||F && !A||!C||D||!E||F && !A||!B||C||!D||!E && !A||!B||C||!E||!F && !A||C||!D||!E||!F && !A||!B||C||E||F && !A||C||!D||E||F && !A||C||D||E||!F && !A||B||!C||!E||F && !A||B||!D||E||!F && !A||B||!C||D||!E && !A||B||!C||D||F && !A||B||D||!E||F && A||!B||!C||!D||!E);
    e2 = (!A||!C||!D||!F && !A||!C||E||!F && !A||!D||E||!F && !A||B||D||!F && !B||!C||!D||!E||!F && !A||!B||C||!E||!F);
    f2 = (!A||!C||!E||!F && !A||C||!D||!E && !A||C||E||!F && !A||B||C||!E && !A||!B||!C||D||!E && !A||!B||!C||D||!F && !A||!B||C||D||E && !A||B||!C||!D||E && A||!B||!C||!D||E);
    g2 = (!A||C||!E && !A||!C||!D||E && !A||!C||E||!F && !A||!B||D||!E && !A||!B||C||D && !A||B||!D||!F && !A||B||!C||E && A||!B||!C||!D);



    // OUTPUTS

    //DISPLAY 2
    digitalWrite(A0,a2);
    digitalWrite(2,b2);
    digitalWrite(3,c2);
    digitalWrite(4,d2);
    digitalWrite(5,e2);
    digitalWrite(6,f2);
    digitalWrite(7,g2);

    //DISPLAY 1
    digitalWrite(8,a1);
    digitalWrite(9,b1);
    digitalWrite(10,c1);
    digitalWrite(11,d1);
    digitalWrite(12,e1);
    digitalWrite(13,g1);
}

Here's my Flip-FlopD:

boolean flipflopD(boolean D, boolean Q, boolean CLK){
  boolean Qo=false;
    if (CLK=true) {
      Qo=D;
    } else {
  Qo=Q;
  }
  return Qo;
}

Can someone tell me what's wrong?

3

One potential error source: In your D-Flip-Flop code, you have

boolean flipflopD(boolean D, boolean Q, boolean CLK){
  boolean Qo=false;
    if (CLK=true) {
      Qo=D;
    } else {
  Qo=Q;
  }
  return Qo;
}

You probably want to do a comparison with double == instead of an assignment with = in the line

if(CLK=true)

This causes the if to always evaluate to true and set Qo=D.

Also, I am not really sure if this is what you want:

a1 = !A||B||D && !A||B||C & A||!B||!C||!D;

First, you're mixing && (logical-and) with the bitwise & here. Next, C++ operator precedence dictates that the &&has a higher priority than ||. This looks like a conjunctive-normal-form of some boolean function, thus I would group the AND terms in parenthesis, like

a1 = (!A||B||D) && (!A||B||C) && (A||!B||!C||!D);

In the c1 expression you are also suddenly using the + operator in a chain of logical expressions, which looks wrong.

Overall I don't quite get how this should work. If you have an IR receiver connected to A2, from which you read the "CLK" input, it will get some 39kHz input wave when you point a IR remote at it and press a button. And you want that on each CLK rise/fall the Arduino reacts to it by calculating the output for a seven-segment display? Your Arduino Uno/Nano with its 16MHz will take some time to execute each of the statements in the loop, sequentally. So what you're doing is actually sampling the A2 input every now and then -- how often exactly, I don't know, maybe with a few kilohertz. Your Arduino might sample the input at the wrong time or not fast enough to react the way you expect. digitalRead and digitalWrite are notoriously slow, maybe try direct port access or an interrupt-based approach. Of course, calculating all these AND, OR and NOT expressions will take some processing time, too.

This sort of combinational logic is more suited for an FPGA implementation, not for an Arduino.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.