Малък напредък - версия 2

Размерът на цялата джаджа вече е значително по-малък.
Това е груба схема на свързването:

- Untitled Sketch_bb.png (166.03 KiB) Преглеждано 2261 пъти
Кода на Arduino-то:
Код за потвърждение: Избери целия код
/*
Matthew McMillan @matthewmcmillan - http://matthewcmcmillan.blogspot.com
Digital Pot Control (MCP4251)
This example controls a Microchip MCP4251 digital potentiometer.
The MCP4251 has 2 potentiometer channels. Each channel's pins are labeled:
A - connect this to voltage
W - this is the pot's wiper, which changes when you set it
B - connect this to ground.
The MCP4251 also has Terminal Control Registers (TCON) which allow you to
individually connect and disconnect the A,W,B terminals which can be useful
for reducing power usage or motor controls.
A value of 0 is no resistance
A value of 128 is approximately half ohms
A value of 255 is maxim of resistance
The MCP4251 is SPI-compatible. To command it you send two bytes,
one with the memory address and one with the value to set at that address.
The MCP4251 has few different memory addresses for wipers and tcon (Terminal Control)
*Wiper 0 write
*Wiper 0 read
*Wiper 1 write
*Wiper 1 read
*TCON write
*TCON read
The circuit:
* All A pins of MCP4251 connected to +5V
* All B pins of MCP4251 connected to ground
* W0 pin to A0
* W1 pin to A1
* VSS - to GND
* VDD - to +5v
* SHDN - to digital pin 7 (and a 4.7k pull down resistor)
* CS - to digital pin 10 (SS pin) or 53 at Mega
* SDI - to digital pin 11 (MOSI pin) or 51 at Mega
* SDO - to digital pin 12 (MISO pin) or 50 at mega
* CLK - to digital pin 13 (SCK pin) or 52 at Mega
created 12 Mar 2014
Thanks to Heather Dewey-Hagborg and Tom Igoe for their original tutorials
adapted by Nicu FLORICA (aka niq_ro) - http://www.tehnic.go.ro
& http://www.arduinotehniq.com/
& http://nicuflorica.blogspot.ro/
& http://arduinotehniq.blogspot.com/
*/
// inslude the SPI library:
#include <SPI.h>
// set pin 10 as the slave select for the digital pot:
const int slaveSelectPin = 10; // Uno
//const int slaveSelectPin = 53; // Mega
const int shutdownPin = 7;
const int wiper0writeAddr = B00000000;
const int wiper1writeAddr = B00010000;
const int tconwriteAddr = B01000000;
const int tcon_0off_1on = B11110000;
const int tcon_0on_1off = B00001111;
const int tcon_0off_1off = B00000000;
const int tcon_0on_1on = B11111111;
const int VOL_UP=1;
const int VOL_DN=2;
const int PREV_TR=3;
const int NEXT_TR=4;
const int MODE=5;
const int MUTE=6;
int prevButton=-1;
int wheelPin=A2; // steering wheel resistance reading pin
int wheelPin2=A1; // steering wheel resistance reading pin
void setup() {
// set the slaveSelectPin as an output:
pinMode (slaveSelectPin, OUTPUT);
// set the shutdownPin as an output:
pinMode (shutdownPin, OUTPUT);
// start with all the pots shutdown
digitalWrite(shutdownPin,LOW);
// initialize SPI:
SPI.begin();
}
void loop() {
int currButton=getR(); // get current pressed button code
if (currButton!=prevButton) { // if it has changed since last reading
delay(10);
currButton=getR(); // wait 10ms and read again to make sure this is not just some noise
if (currButton!=prevButton) {
prevButton=currButton;
// send command to car stereo
SPI.transfer(0);
switch(currButton) {
case VOL_UP: MoveWiper(165); break;
case VOL_DN: MoveWiper(130); break;
case PREV_TR: MoveWiper(190); break;
case NEXT_TR: MoveWiper(207); break;
case MODE: MoveWiper(242); break;
default: MoveWiper(0); break; // nothing
}
}
}
delay(5);
}
void MoveWiper(int step){
if(step==0){
digitalWrite(shutdownPin,LOW);
return;
}
digitalWrite(shutdownPin,HIGH);
digitalPotWrite(wiper0writeAddr, step);
digitalPotWrite(wiper1writeAddr, step);
}
// This function takes care of sending SPI data to the pot.
void digitalPotWrite(int address, int value) {
// take the SS pin low to select the chip:
digitalWrite(slaveSelectPin,LOW);
// send in the address and value via SPI:
SPI.transfer(address);
SPI.transfer(value);
// take the SS pin high to de-select the chip:
digitalWrite(slaveSelectPin,HIGH);
}
int getR() { // this function returns pressed button code or 0 if all buttons are released
// read resistance value from the steering wheel buttons
int r=analogRead(wheelPin);
int r2=analogRead(wheelPin2);
if (r>=90 && r<=97) return(VOL_UP);
if (r>=238 && r<=250) return(VOL_DN);
if (r>=30 && r<=34) return(PREV_TR);
if (r>=0 && r<=5) return(NEXT_TR);
if (r2>=0 && r2<=5) return(MODE);
return (0);
}
Малко снимки как се получи:

- 20180312_200518.jpg (166.23 KiB) Преглеждано 2261 пъти

- 20180312_223954.jpg (208.91 KiB) Преглеждано 2261 пъти

- 20180312_224009.jpg (171.16 KiB) Преглеждано 2261 пъти

- 20180312_222055.jpg (282.75 KiB) Преглеждано 2261 пъти
Остава да запоя жиците и да скрия всичко зад таблото...