help

I am having trouble figuring this stuff out. This is my first day using any type of microcontroller so I apologize if I do not understand something.

I am trying to make a counter using an asynchronous set up on J/K FFs to count down from 9 to 0. When the counter counts to 15, it is cleared on bit 2 and 3 and also preset on bit 1 and 4 to repeat the count 9 to 0 again.

So in the code, I was trying to use the nonPWM pins to "digitalRead" all 4 bits and when all 4 bits = HIGH, a pin on the Arduino would output a 1 or a HIGH to preset the count back to 9.

The code compiles but when I put the Arduino to use, it may not be reading the 4 bits or my code may not be what I am wanting it to do. I'm not sure on the statements I should be making for this such as "while" or "if, else". Any help on the code would be much appreciated.

const int clockpin = 3;
const int bit1pin = 2;
const int bit2pin = 4;
const int bit4pin = 7;
const int bit8pin= 8;
const int clearpin = 5;
int updateclrpin;

void setup()
{
  pinMode(clockpin, OUTPUT);
  pinMode(bit1pin, INPUT);
  pinMode(bit2pin, INPUT);
  pinMode(bit4pin, INPUT);
  pinMode(bit8pin, INPUT);
  pinMode(clearpin, OUTPUT);
 }

void loop() //clock loop
{
do{
  digitalWrite(clockpin, HIGH);
  delay(500);
  digitalWrite(clockpin, LOW);
  delay(500);
}
while(clockpin == HIGH);{
  digitalRead (bit1pin);
  digitalRead(bit2pin);
  digitalRead(bit4pin);
  digitalRead(bit8pin);
 
  if(bit1pin == HIGH && bit2pin == HIGH && bit4pin == HIGH && bit8pin == HIGH)
{
	digitalWrite(clearpin, HIGH);
	delay(1000);
}
  else
  {
    digitalWrite(clearpin, LOW);
  }
}
}

what model of flip flop do you have?

and why are you fussing with a flip flop for a countdown, you have a computer on a chip :wink:

and yes your do-while loop seems fishy, you could do something like

const int clockpin = 3;
const int bit1pin = 2;
const int bit2pin = 4;
const int bit4pin = 7;
const int bit8pin= 8;
const int clearpin = 5;
int updateclrpin;

void setup()
{
  pinMode(clockpin, OUTPUT);
  pinMode(bit1pin, INPUT);
  pinMode(bit2pin, INPUT);
  pinMode(bit4pin, INPUT);
  pinMode(bit8pin, INPUT);
  pinMode(clearpin, OUTPUT);
}

void loop() //main loop
{
  digitalWrite(clockpin, HIGH);
  delay(500);
  digitalWrite(clockpin, LOW);
  delay(500);
  
  digitalRead(bit1pin);
  digitalRead(bit2pin);
  digitalRead(bit4pin);
  digitalRead(bit8pin);
 
  if(bit1pin == HIGH && bit2pin == HIGH && bit4pin == HIGH && bit8pin == HIGH)
  {
    digitalWrite(clearpin, HIGH);
    delay(1000);
  }
  else
  {
    digitalWrite(clearpin, LOW);
  }
}

A schematic to see exactly what you're doing would help, probably. A quad AND chip would make it simple, too. One thing to check(or do an interrupt) instead of four.

This is my first day using any type of microcontroller

or do an interrupt

heck, this is like my 4th year with arduino and AVR's and I avoid interrupts unless I really need them lol, its a flip flop, the output is not going to change until the clock does, 2 seconds worth in the above script, I think we can deal with it without bringing that headache into context :sweat_smile:

I'm using two 7476s and this is just for practice to learn how to use an Arduino lol. I don't have a schematic but I was originally using an AND gate to reset the counter back to 9 on the 1111 binary count. How do interupts work?

The problem I have here is, the on-line reference is fairly explicit about how the do...while construct works, so I am not sure what I can add to it so that you understand what you have done wrong. As others have asked, we need a better understanding of what you are trying to do.

inttrupts are awesome when you need them, they look for a condition, internal or external, stop the whole feaking world and deal with that condition, and are referenced well in the ... reference

BUT, I think its just your original script, your do while is messed up then its looking for a condition that never happens, try my thinned out script, I have no idea if it works or not but its not "funny" (I have never personally found a use for DO .. WHILE loops in systems that did not specifically have an advantage to them, such as speed, its really just a while loop with old syntax)

I'm just making an asynchronous counter clocked at 1 Hz to count down from 9 to 0, decoded through a 7447 into a 7 seg display. The Q NOT output is at the next bit's clock input for the asynchronous countdown. The 4 Q outputs not inverted can be ANDed together so when all 4 bits are high, it will provide a high. This high can be tied to the preclear of the J/K on the second and third bit and on the preset on the first and fourth bit. This preloads a 1001 binary count into the counter to restart the count at 9. So instead of counting 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 it counts only the 9 to 0.
I would like to learn how to do this through an arduino for the practice of writing code and learning.

Sorry, I wish I had a schematic..

Also, when I do a "digitalRead", does it read as a HIGH/LOW or does it read an actual analog value? I'm guessing it would be a HIGH/LOW since it is digital but I just want to clarify.. lol

Alright thanks Osgeld. I'm actually going to try it out right now!

It reads as a Boolean, ie 1 or 0, TRUE or FALSE, HIGH or LOW, for what your really wanting to accomplish, I would drop the flip slop and just do it in software, its a ton easier, and will output exactly what you want for your display decoder

BUT I will hand it to you, getting the arduino to interface with anything under the sun is part of the learning experience so kudos

I did try your code and it didn't work. I didn't realize that the delay() stops everything and I am pretty sure that is the problem I was having so I wrote a code to output bits of information to a 7447 decoder to the display and got rid of the flip flops like you recommended. The new code that actually works perfectly is:

const int bit1pin = 2;
const int bit2pin = 4;
const int bit4pin = 7;
const int bit8pin= 8;


void setup()
{
  pinMode(bit1pin, OUTPUT);
  pinMode(bit2pin, OUTPUT);
  pinMode(bit4pin, OUTPUT);
  pinMode(bit8pin, OUTPUT);
}

void loop() //main loop
{
  digitalWrite(bit1pin, HIGH);
   digitalWrite(bit2pin, LOW);
    digitalWrite(bit4pin, LOW);
      digitalWrite(bit8pin, HIGH);
  delay(1000);

  digitalWrite(bit1pin, LOW);
   digitalWrite(bit2pin, LOW);
    digitalWrite(bit4pin, LOW);
      digitalWrite(bit8pin, HIGH);
  delay(1000);


  digitalWrite(bit1pin, HIGH);
   digitalWrite(bit2pin, HIGH);
    digitalWrite(bit4pin, HIGH);
      digitalWrite(bit8pin, LOW);
  delay(1000);


  digitalWrite(bit1pin, LOW);
   digitalWrite(bit2pin, HIGH);
    digitalWrite(bit4pin, HIGH);
     digitalWrite(bit8pin, LOW);
  delay(1000);


  digitalWrite(bit1pin, HIGH);
   digitalWrite(bit2pin, LOW);
     digitalWrite(bit4pin, HIGH);
      digitalWrite(bit8pin, LOW);
  delay(1000);


  digitalWrite(bit1pin, LOW);
   digitalWrite(bit2pin, LOW);
    digitalWrite(bit4pin, HIGH);
      digitalWrite(bit8pin, LOW);
  delay(1000);


  digitalWrite(bit1pin, HIGH);
   digitalWrite(bit2pin, HIGH);
    digitalWrite(bit4pin, LOW);
      digitalWrite(bit8pin, LOW);
  delay(1000);


  digitalWrite(bit1pin, LOW);
   digitalWrite(bit2pin, HIGH);
    digitalWrite(bit4pin, LOW);
      digitalWrite(bit8pin, LOW);
  delay(1000);


  digitalWrite(bit1pin, HIGH);
   digitalWrite(bit2pin, LOW);
    digitalWrite(bit4pin, LOW);
      digitalWrite(bit8pin, LOW);
  delay(1000);


  digitalWrite(bit1pin, LOW);
   digitalWrite(bit2pin, LOW);
    digitalWrite(bit4pin, LOW);
     digitalWrite(bit8pin, LOW);
  delay(1000);

}

That code would certainly work but what I think you will find is that you would actually start losing time since it takes some very small amount of time to do all of the DigitalWrites.

Also, because you use delays, this code is completely unworkable if you every wanted the same sketch to do anything else.

It is still cool that you got it to work without the external components. So next step is to look at the Blink without Delay example and figure out how to use the millis() function to do the same thing and shorten that code way down.

what he said, and off topic OMFG another user from Tennessee, that makes like 3 of us lol

Yeah I know what you mean about being unworkable. I didn't have the understanding that the delay function stops everything but I condensed the code to hopefully a more workable status. I haven't tested it yet but it compiles.. This is the revision:

const int clockpin = 3;
const int bit1pin = 2;
const int bit2pin = 4;
const int bit4pin = 7;
const int bit8pin= 8;
const int clearpin = 5;

void setup()
{
  pinMode(clockpin, OUTPUT);
  pinMode(bit1pin, INPUT);
  pinMode(bit2pin, INPUT);
  pinMode(bit4pin, INPUT);
  pinMode(bit8pin, INPUT);
  pinMode(clearpin, OUTPUT);
 }
 
 void loop()
 {
   digitalWrite(clockpin, HIGH);
   
   digitalRead (bit1pin);
    digitalRead(bit2pin);
     digitalRead(bit4pin);
      digitalRead(bit8pin);
   delay(500) 
      
     if(bit1pin == HIGH && bit2pin == HIGH && bit4pin == HIGH && bit8pin == HIGH)
     {
       	digitalWrite(clearpin, HIGH);
	delay(1000);
     }
     else
     {
       digitalWrite(clockpin, LOW);
       delay(500);
     }
 }

You are still using delays though in that example.

Go to youtube and do a search for sciguy14. He does a great series of tutorials for the Arduino and he starts really basic turning a light on and off sort of like what you have done. then he moves into using millis() and debounce and RFID and Xbee and all kinds of fun things. There are 14 tutorials total and they are a great resource for anyone just starting out.

And off topic some more. Osgeld, where in Tennessee are you? I have looked for maker/hacker spaces in the Chattanooga area to no avail. I think there was one in Knoxville which is not too far away but I think the one I found was not focused on electronics as much as I would like.

Thanks, I will definitely check that out. I actually got the counter to work with the flipflops so I'm pretty excited I got it to at least function.

It also looks as though you've gone back to using all the extra hardware.

Here is how I would do it. Using millis and just the decoder driver. You may need to adjust the pin numbers for your use.

const int pin0 = 4;
const int pin1 = 5;
const int pin2 = 6;
const int pin3= 7;
int count = 9;
long baseT = 0;
long period = 1000;
unsigned long currentT;

void setup() {
  pinMode(pin0, OUTPUT);
  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
}

void loop(){
  display(count);
  currentT = millis();
  if((currentT - baseT) > period) {
    baseT = currentT;
    count--;  
    if (count < 0) count = 9;
  }
}

void display(int digit) {
  digitalWrite(pin0, bitRead(digit, 0));
  digitalWrite(pin1, bitRead(digit, 1));
  digitalWrite(pin2, bitRead(digit, 2));
  digitalWrite(pin3, bitRead(digit, 3));
}

Thanks! Question though, where you have bit1, bit2, bit3, and bit4 in the void setup(), these are suppose to be pin 0, pin1, pin2, and pin3 aren't they?

Yes, good catch. I'll see if I can edit it....

Okay, made the changes. Sorry about that. It was code that I copied out of a bigger sketch and thought I had made all the changes. Just tried it and it works.

Sacman:
And off topic some more. Osgeld, where in Tennessee are you? I have looked for maker/hacker spaces in the Chattanooga area to no avail. I think there was one in Knoxville which is not too far away but I think the one I found was not focused on electronics as much as I would like.

Nashville, there is a hacker space around, its like an hour away from me, and they have a 80 buck a month membership (which is fine, but I am not going to use 80$ a month's worth of time there)