Assorted Gubbins on toy tank

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
New Arduinos arrived! :)

(If I'm spamming the forum with all my updates, or in the wrong place, please let me know! I'm just excited about getting this project up and running! :) )
 

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
Latest news:
New Arduinos arrived.
Shift-registers for input and output wired up and coded.

Something up with wiring. Gently touching some of the jumper cables affects the inputs

Also, all input bits seem to be the same. Mostly 11111111, occasionally 00000000

Will rebuild circuit, and see if someone can't look over my code.
The input code was copied/adjusted from this site:
http://forum.arduino.cc/index.php?topic=22117.0
 

Gryd3

Jun 25, 2014
4,098
Joined
Jun 25, 2014
Messages
4,098
Latest news:
New Arduinos arrived.
Shift-registers for input and output wired up and coded.

Something up with wiring. Gently touching some of the jumper cables affects the inputs

Also, all input bits seem to be the same. Mostly 11111111, occasionally 00000000

Will rebuild circuit, and see if someone can't look over my code.
The input code was copied/adjusted from this site:
http://forum.arduino.cc/index.php?topic=22117.0
I'm sure we can poke at the code and wiring you have.
My suggestion would be poking at the datasheet for the input shift register instead of the Arduino forum.
You've crossed a number of hurdles so far, and I'm positive that once you determine how those things work you can spin up your own code.
 

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
OK, still getting nowhere.
I'm now convinced that my wiring is OK. I've checked the datasheet and double-checked the board.

I've just found a blog that says Arduino is incompatible wtih 74HC166, and to use 74HCT166 instead. Is this true? They don't explain why.
http://eq-av.com/?p=AVBR&PHPSESSID=ced18f58fbba1a1403817274dfe310cf

It also seems that most people are using the 165, but I couldn't get those without paying £20 postage! :(
The code is different for 165 and 166, and I can't get my head round it.

What I'm trying to do is write an Input Function that reads the state of multiple switches attached to the 166, and put the results into a byte.
I then assign the bits from the byte into my variables.

My current code is a mess, hacked from numerous examples, so I need to start over.
The code mentioned in previous post LOOKS like it should work, but not for me.

in Main Loop:
byte incoming = readbits();

function for readbits()
set clock high
set latch low
delay
set latch high //to get rising edge to begin read
for i=0 to 7
set clock low
read input pin
set clock high
add pin state to newbyte
end for
set latch high // to end reading
return newbyte
 

Gryd3

Jun 25, 2014
4,098
Joined
Jun 25, 2014
Messages
4,098
OK, still getting nowhere.
I'm now convinced that my wiring is OK. I've checked the datasheet and double-checked the board.

I've just found a blog that says Arduino is incompatible wtih 74HC166, and to use 74HCT166 instead. Is this true? They don't explain why.
http://eq-av.com/?p=AVBR&PHPSESSID=ced18f58fbba1a1403817274dfe310cf

It also seems that most people are using the 165, but I couldn't get those without paying £20 postage! :(
The code is different for 165 and 166, and I can't get my head round it.

What I'm trying to do is write an Input Function that reads the state of multiple switches attached to the 166, and put the results into a byte.
I then assign the bits from the byte into my variables.

My current code is a mess, hacked from numerous examples, so I need to start over.
The code mentioned in previous post LOOKS like it should work, but not for me.

in Main Loop:
byte incoming = readbits();

function for readbits()
set clock high
set latch low
delay
set latch high //to get rising edge to begin read
for i=0 to 7
set clock low
read input pin
set clock high
add pin state to newbyte
end for
set latch high // to end reading
return newbyte

The 74HC166 is almost identical to a 74HCT166. There is a difference in the working voltage though.
CMOS (74HC166) works with 5V logic levels... whereas the 74HCT166 works with TTL which considers a 'High' to be about 2V, The CMOS version expects a higher voltage to call it 'High'.
You can measure the output of the Arduino you have, you need to have an output higher than 3.3V for the 74HC166 to work properly.
I'm curious about the code and wiring you are using though... There is a 'Clock Enable' pin, as well as a 'Parallel Enable' ping that both need to be low in order to properly 'load' the state of the buttons. Parallel Enable can return to high then you begin the 'read' section.
Perhaps the 'Parallel Enable' is called 'Latch' in the code? That should be connected to pin 15. Pin 6 can remain connected to ground.

in Main Loop:
byte incoming = readbits();

function for readbits()
set latch low // Do this first.
delay
set clock high // The 'rising' edge triggers the 'load' of parallel inputs to the buffer
delay
set latch high // Return to high. (Loading is done)
for i=0 to 7 // Begin Shift out.
set clock low
read input pin
set clock high
add pin state to newbyte
end for
return newbyte

Minor tweak. Hopefully this does it for you... I'm not sure if 'Latch' is meant for pin 15 or 6... it 'should' be for 15 if I am correct.
Of course, hopefully the output voltage of the Arduino you have is high enough to properly drive this IC... if not, it's time to bust out some transistors, or buy a logic level converter.
 

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
I have checked voltages at numerous points on the board. The +ve rail is 5V at all points.

PE is called latch in the examples I am looking at, and is Pin 15.The Arduino is supplying it with 5V.
Pin 6 (Clock Inhibit) is connected to ground.

I have now connected all unused input pins on the 166 to GND to ensure they are GND, not floating.

I have tweaked my code.

I am still getting odd results.
Situation 1: No switches pressed. Input 1 goes HIGH. Inputs 0 and 2-7 stay LOW.
Situation 2: After ANY switch is pressed ALL inputs go HIGH
Situation 3: Sometimes, and I can't see what causes it, it reverts to Situation 1. And then back to 2.

Next: Wire up a copy, using just the INPUT side of my circuit. No LEDs, no output shift register. Just rely on the Serial output of the Arduino IDE to see what is happening.
 

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
Built a new circuit on a different breadboard, using a 2nd Arduino and spare components.

I am getting very similar results. :(

Code:
//Define Variables here

int latchPinInput = 7;
int clockPinInput = 9;
int dataPinInput = 10;

byte incoming;
int state;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  pinMode(latchPinInput, OUTPUT);
  pinMode(clockPinInput, OUTPUT);
  pinMode(dataPinInput, INPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
  incoming = readbits();
  Serial.print(incoming);
}

byte readbits(){
  byte newbyte = 00000000;
  digitalWrite(latchPinInput, LOW);  // loads her up to be read
  delay(15);
  digitalWrite(clockPinInput, HIGH);
  digitalWrite(latchPinInput, HIGH);  // (on a rising signal)
  for (int i=0; i <= 7; i++){  // count through the items (bits) in the array
    digitalWrite(clockPinInput, LOW);
    state =  digitalRead(dataPinInput);
    digitalWrite(clockPinInput, HIGH);
    Serial.print(" Pin ");
    Serial.print(i);
    Serial.print(": ");
    Serial.print(state);
    bitWrite (newbyte, i, state);
    Serial.print(" ");
    Serial.print(newbyte, BIN);
    delay(15);
  }
  Serial.print(newbyte);
Serial.println(); ///////// ==========
digitalWrite(latchPinInput, HIGH);  // close her down and switch to display mode
return newbyte;
}

Are there any glaring errors?
I have included the Serial.print debugging code. Is something amiss in there?
 

Gryd3

Jun 25, 2014
4,098
Joined
Jun 25, 2014
Messages
4,098
I have now connected all unused input pins on the 166 to GND to ensure they are GND, not floating.
Have you poked around in http://www.nxp.com/documents/data_sheet/74HC_HCT166.pdf ?
Pin. 9 is a 'Master Reset' which should be high... not low (ground)
Pin. 13 is the serial out of course, and it will spit out Serial Data with the most significant bit first...
D7 to D1
Double-check and make sure you are not holding anything to ground that you shouldn't.
(Can you provide a basic schematic, or simply tell us what pins on the 74HC166 go where?

Code:
//Define Variables here

int latchPinInput = 7;
int clockPinInput = 9;
int dataPinInput = 10;

byte incoming;
int state;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  pinMode(latchPinInput, OUTPUT);
  pinMode(clockPinInput, OUTPUT);
  pinMode(dataPinInput, INPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
  incoming = readbits();
  Serial.print(incoming);
}

byte readbits(){
  byte newbyte = 00000000;
  digitalWrite(latchPinInput, LOW);  // Prepare Load
  delay(15);
  digitalWrite(clockPinInput, HIGH);  // Begin Load. (On Rising Clock Edge)
  delay(15);
  digitalWrite(latchPinInput, HIGH);  // End 'Load'
  for (int i=0; i <= 7; i++){  // count through the items (bits) in the array
    digitalWrite(clockPinInput, LOW);  // Reset Clock to Low
    state =  digitalRead(dataPinInput);  // Read Current Bit
    digitalWrite(clockPinInput, HIGH);  // Shift Buffer. (Next iteration will read next bit)
    Serial.print(" Pin ");
    Serial.print(i);
    Serial.print(": ");
    Serial.print(state);
    bitWrite (newbyte, i, state);
    Serial.print(" ");
    Serial.print(newbyte, BIN);
    delay(15);
  }
  Serial.print(newbyte);
Serial.println(); ///////// ==========
digitalWrite(latchPinInput, HIGH);  // close her down and switch to display mode
return newbyte;
}

Are there any glaring errors?
I have included the Serial.print debugging code. Is something amiss in there?
I put another 'delay' in there and adjusted comments. It looks fine to me at the moment...
There is a graph on the datasheet I linked that shows what happens with the various pin states.
The only thing I can think of is perhaps the 'Latch' is connected to the wrong pin?
You can also hugely slow down the program and monitor the output with an LED if it really gets to you.
 

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
Yes, I've got Pin 9 HIGH.

I'm about ready to give up on this thing! I'm blaming my less-than-full understanding of Shift Registers, and the 166 being a lot less documented/discussed than the 165.

I've had some ideas on how to save pins using Analogue inputs:
e.g. for the stop/slow/fast, have a 3-way switch that goes across 2 resistors as a Voltage Divider. Stop = 0V, Slow = between resistors. ~2.5V, Fast = 5V.
e.g. something similar for Hull Points up/down. Hold a line at ~2.5V, press UP to momentarily push it to 5V, DOWN button momentarily connects to GND. Arduino spots these pulses and changes Hull Point value accordingly.
(Opens a new question: What value resistors for voltage divider? 10K? Both the same, to get 0V/2.5V/5V)

As I've saved loads of pins using the Output Shift Register (which works fine), I have more Input pins left, less need for Input Shift Register. Would have been nice to get it running, but this is not a "Get a Shift Register to Work" project. :)
 

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
Pin List for 166:
(I'm really struggling with Eagle and ExpressSCH, so no schematic, sorry)

Pin 1 (DS - Serial input) Not Connected
Pin 2 (D0 - Input) - Button 1 (Stop)
Pin 3 (D1 - input) - Button 2 (Slow)
Pin 4 (D2 - input) - Button 3 (Fast)
Pin 5 (D3 - input) - GND (Unused)
Pin 6 (/CE) - GND
Pin 7 (CP - clock) - Arduino Pin D9
Pin 8 (GND) - GND

Pin 9 (/MR) - HIGH (5V)
Pin 10 (D4 - input) - button 4 (HP Up)
Pin 11 (D5 - input) - button 5 (HP down)
Pin 12 (D6 - input) - GND (unused)
Pin 13 (Q7 - Serial Output) - Arduino Pin D10
Pin 14 (D7 - input) - GND (unused)
Pin 15 (SH/LD - latch) - Arduino Pin D7
Pin 16 (Vcc) - 5V

The buttons have pull-down resistors (10k) and connect to 5V when closed.
 

Gryd3

Jun 25, 2014
4,098
Joined
Jun 25, 2014
Messages
4,098
Yes, I've got Pin 9 HIGH.

I'm about ready to give up on this thing! I'm blaming my less-than-full understanding of Shift Registers, and the 166 being a lot less documented/discussed than the 165.

I've had some ideas on how to save pins using Analogue inputs:
e.g. for the stop/slow/fast, have a 3-way switch that goes across 2 resistors as a Voltage Divider. Stop = 0V, Slow = between resistors. ~2.5V, Fast = 5V.
e.g. something similar for Hull Points up/down. Hold a line at ~2.5V, press UP to momentarily push it to 5V, DOWN button momentarily connects to GND. Arduino spots these pulses and changes Hull Point value accordingly.
(Opens a new question: What value resistors for voltage divider? 10K? Both the same, to get 0V/2.5V/5V)

As I've saved loads of pins using the Output Shift Register (which works fine), I have more Input pins left, less need for Input Shift Register. Would have been nice to get it running, but this is not a "Get a Shift Register to Work" project. :)
You can technically use almost any value... but it's safe to consider the 'input' pin on an Arduino to be 1MΩ .. and if you use resistor values that are too low, you will be wasting energy heating them up. 10k sounds perfect to me :)

Pin List for 166:
(I'm really struggling with Eagle and ExpressSCH, so no schematic, sorry)

Pin 1 (DS - Serial input) Not Connected
Pin 2 (D0 - Input) - Button 1 (Stop)
Pin 3 (D1 - input) - Button 2 (Slow)
Pin 4 (D2 - input) - Button 3 (Fast)
Pin 5 (D3 - input) - GND (Unused)
Pin 6 (/CE) - GND
Pin 7 (CP - clock) - Arduino Pin D9
Pin 8 (GND) - GND

Pin 9 (/MR) - HIGH (5V)
Pin 10 (D4 - input) - button 4 (HP Up)
Pin 11 (D5 - input) - button 5 (HP down)
Pin 12 (D6 - input) - GND (unused)
Pin 13 (Q7 - Serial Output) - Arduino Pin D10
Pin 14 (D7 - input) - GND (unused)
Pin 15 (SH/LD - latch) - Arduino Pin D7
Pin 16 (Vcc) - 5V

The buttons have pull-down resistors (10k) and connect to 5V when closed.
Pinout looks fine to me.. I'm curious why you are having the problems you are... You said Arduino was putting out 5V...
To be honest, the only thing I can suggest at the moment is rewiring the Input Shift register so you can do some basic tests on it... Setup a basic Arduino Sketch that simply toggles the CLK pin every 250ms . After the CLK goes low, read Pin 13 (and print to debug so you can see the state).
Forget about using the Parallel Load, and put a button on Pin 1 (Serial Input) ... you should be able to press and hold this single button and then about 3-4 seconds later, see the output of the Shift Register Change.
This will test and ensure the reading and shifting is being done correctly.
You can then leave Pin 1 alone and try a parallel load simply by toggling the state of Pin 15 to low, then high again. Watch the output (With the slow clock still) and see if the output toggles. (You can hard-wire one or two inputs to 'High' so you don't have to worry about pushing buttons during this test.)

I see no reason those should fail, but it's worth a shot to take a step back and try the most simple task first, then build from that.

(Chack page 5 of 20 on that datasheet I linked. You don't need a forum and super documents. Ideally, you need to understand how these datasheets work. They cover almost everything)
 

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
Thanks for your continued help. It is much appreciated.

I am moving on from Shift Register inputs*, and have wired/coded a proof-of-concept Analogue input.
I have a 3-way switch connected to a voltage divider, giving Analogue values of ~0, ~512, ~1023 (actually pretty solid, stable values!), and an if ... else statement assigning variables accordingly.
I shall see about building this into the larger Project.
(Minor issue is the 3-way switch is not designed for breadboard! Have jammed it in, but it falls out easily! :( Oh well.)

*I might come back to them later. But for now, I'd rather have something that works! :)
The output register works just fine.
 

Gryd3

Jun 25, 2014
4,098
Joined
Jun 25, 2014
Messages
4,098
Thanks for your continued help. It is much appreciated.

I am moving on from Shift Register inputs*, and have wired/coded a proof-of-concept Analogue input.
I have a 3-way switch connected to a voltage divider, giving Analogue values of ~0, ~512, ~1023 (actually pretty solid, stable values!), and an if ... else statement assigning variables accordingly.
I shall see about building this into the larger Project.
(Minor issue is the 3-way switch is not designed for breadboard! Have jammed it in, but it falls out easily! :( Oh well.)

*I might come back to them later. But for now, I'd rather have something that works! :)
The output register works just fine.
Good to hear.
You can use that resistor trick with multiple buttons as well.
Take a look at a 'Voltage Ladder' . You can read 5 or 6 buttons pretty easy with a single Analogue pin.
Let me know if you touch the input shift register again :p
 

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
(Was typing when your reply came in!)

3-way speed-selector wired into analogue input on "real" circuit.
Have had to remove all wiring and code for all other inputs.
NEXT: Analogue Input for HP +/-
I was going to have the input wired to the centre of two 10K resistors (holding it at 2.5V) and two switches. One shorts the input to 5V, the other shorts the input to GND.
This looks fine unless both are pressed together (which should never happen, but if I don't protect it, it WILL happen!), shorting 5V to GND - BOOM!
Can each switch have a resistor (what value?) to protect the circuit? (Picture attached)

Voltage Ladder. I vaguely recall that phrase! ;)
Currently I'm thinking of keeping unrelated switches on separate inputs, but yes, can squeeze more in that way! :)
 

Attachments

  • HP Switch.jpg
    HP Switch.jpg
    50.6 KB · Views: 151

Gryd3

Jun 25, 2014
4,098
Joined
Jun 25, 2014
Messages
4,098
(Was typing when your reply came in!)

3-way speed-selector wired into analogue input on "real" circuit.
Have had to remove all wiring and code for all other inputs.
NEXT: Analogue Input for HP +/-
I was going to have the input wired to the centre of two 10K resistors (holding it at 2.5V) and two switches. One shorts the input to 5V, the other shorts the input to GND.
This looks fine unless both are pressed together (which should never happen, but if I don't protect it, it WILL happen!), shorting 5V to GND - BOOM!
Can each switch have a resistor (what value?) to protect the circuit? (Picture attached)

Voltage Ladder. I vaguely recall that phrase! ;)
Currently I'm thinking of keeping unrelated switches on separate inputs, but yes, can squeeze more in that way! :)
It's pretty similar to your switch idea... the protection resistor will prevent he analogue input from reaching 0 or 1023 depending on which end the analogue input gets connected to.

HP Voltage Ladder:
10K Protection Resistor <> 5.1K <> 2.4K <> GND
You can put the Analogue input any of those resistors. Put your HP + and - buttons in parallel with the 5.1K and the 2.4K resistors. When pressed, it shorts that resistor.
Resting value of ~512, When - button is pressed and shorts the 2.4K, it should drop to 128.
When the + is pressed and shorts the 5.1K is should drop to 256.
It will be 0 if both buttons are pressed.
 

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
Something like this pic?
Sounds good. Better than my idea of shorting 5V to GND! ;)

I can read/display the values to adjust the code (I'm sure you're values are right, but my circuit may be a little off :) )

This means I can put all 3 guns on one button, too! :) (Again, should only press one at once, BUT ...)

Excellent! Ditching the shift-register hasn't been a waste of time, we've made great progress here!

Big Thanks again. I owe you a drink! :)
 

Attachments

  • Hp switch3.jpg
    Hp switch3.jpg
    47 KB · Views: 110

Gryd3

Jun 25, 2014
4,098
Joined
Jun 25, 2014
Messages
4,098
Something like this pic?
Sounds good. Better than my idea of shorting 5V to GND! ;)

I can read/display the values to adjust the code (I'm sure you're values are right, but my circuit may be a little off :) )

This means I can put all 3 guns on one button, too! :) (Again, should only press one at once, BUT ...)

Excellent! Ditching the shift-register hasn't been a waste of time, we've made great progress here!

Big Thanks again. I owe you a drink! :)
Yessir! Just like the picture... but you should ignore the values I quoted earlier... The buttons will still provide unique values to the Arduino.
The goal with those 'voltage' ladders is to use a combination of resistors so that no matter which combination of buttons you push, you will always get a unique value. (You could in theory have a large number of buttons... depends on how much you trust the accuracy of the Analogue input. Problem could be caused by the resistors drifting due to age or temperature, so try to look for a small 'range' of values instead of one specific value from the analogue pin.)
 

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
Yup, I'm remembering voltage ladders now (after some research). I've built a test circuit using a 10k protection resistor with 6.8k and 2.2k on the switches (it's what I have handy). I'm getting analogue values of 512, 412 and 250.
Should be fine.
I'm catching up on TV now. Will update you as I go along.
 

Gryd3

Jun 25, 2014
4,098
Joined
Jun 25, 2014
Messages
4,098
Yup, I'm remembering voltage ladders now (after some research). I've built a test circuit using a 10k protection resistor with 6.8k and 2.2k on the switches (it's what I have handy). I'm getting analogue values of 512, 412 and 250.
Should be fine.
I'm catching up on TV now. Will update you as I go along.
Sounds good. I'm in no rush ;)
I'd like to get my hands on that shift register though to see what's going on with it. I've got a couple spare arduino at home I can use.
 

Crystal Wizard

Feb 10, 2016
100
Joined
Feb 10, 2016
Messages
100
These analogue inputs are a lark, aren't they! :)
Speed-Selector working.
Hull-Points +/- working.

I know I won't be able to use quite as many inputs as shift-registers, but there should be plenty of room for all the ones I need! :)

NEXT: Status effects and movement (simple on/off buttons). I think 3 on one input will work.
 
Top