Smart Speaker | Proximity Potentiometer Working Prototype

I got the digital potentiometer working! It was very simple using the SPI library built into the Arduino software. All you have to do is address the chip and then write it a value between 0 and 255 to set the resistance value. Here is the code for this version, I’ve cleaned it up a bit since last time:

C

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

#include <SPI.h> //for using the digital pot

constintslaveSelectPin=10;//for SPI, from example code

//Shift Register Setup, taken from www.bildr.og

intSER_Pin=7;//pin 14 on the 75HC595

intRCLK_Pin=8;//pin 12 on the 75HC595

intSRCLK_Pin=9;//pin 11 on the 75HC595

#define number_of_74hc595s 4 //How many of the shift registers - change this

#define numOfRegisterPins number_of_74hc595s * 8

booleanregisters[numOfRegisterPins];

intIR_rangefinder=0;//The pin attached to the rangefinder

intdetect_led=2;

intsetlevelMode0_led=3;

intsetlevelMode1_led=4;

intseeking_led=6;

intcheck_val=8;//The arbitrary position above the sensor that indicates a "check" - the position that must be held in order to change the volume

intcycle_delay=30;//a universal delay time for refreshing the check functions

voidsetup(){

//shift register setup

pinMode(SER_Pin,OUTPUT);

pinMode(RCLK_Pin,OUTPUT);

pinMode(SRCLK_Pin,OUTPUT);

clearRegisters();

writeRegisters();

//spi setup

pinMode(slaveSelectPin,OUTPUT);

SPI.begin();

//general LED's

pinMode(detect_led,OUTPUT);

pinMode(setlevelMode0_led,OUTPUT);

pinMode(setlevelMode1_led,OUTPUT);

pinMode(seeking_led,OUTPUT);

//Serial setup

Serial.begin(9600);

}

//integers to remember values off of the IR sensor.

intprelevel_0=0;

intprelevel_1=0;

intprelevel_2=0;

intprelevel_3=0;

intprelevel_4=0;

intprelevel_5=0;

intprelevel_6=0;

intprelevel_7=0;

intprelevel_8=0;

intprelevel_9=0;

intpre_positions[10]={prelevel_0,prelevel_1,prelevel_2,prelevel_3,prelevel_4,prelevel_5,prelevel_6,prelevel_7,prelevel_8,prelevel_9};// an array holding the positions

voidloop(){

/*

The system works by sampling the sensor a number of times. It puts these values into an array.

Once all sample have been made, each value is compared to a like value. If every && evaluates to true, this means whatever object above the sensor has been there for the "cycle_delay" * the number of comparisions made.

It will confirm that the user wants their hand the be there and it was not acciential.

Think of the following loop as the ambient mode, the user can't adjust the volume from here, but they can enter the mode where they can adjust the volume.

It has much less precision by design.

*/

for(inti=0;i<=9;i=i+1){

writebargraph(0,map(analogRead(IR_rangefinder),20,600,0,9));

pre_positions[i]=map(analogRead(IR_rangefinder),20,600,0,9);

if(pre_positions[i]==check_val){

Serial.println("Check Detected");

digitalWrite(detect_led,HIGH);

}

else{

digitalWrite(detect_led,LOW);

}

delay(cycle_delay);

}

for(inti=0;i<=9;i=i+1){

Serial.print(pre_positions[i]);

Serial.print(",");

}

//Once it has been determined that the object above the sensor has been there for a long enough time, the system enters the secondary level set mode.

//Instead of comparing to a predetermined value, it compares it to the first value sampled. If this if statement is true, it means the users hand has stopped moving, indicating they would like to set the volume at that position.

seeking=false;//Stops the loop and holds the last value on the bar graph and digital pot.

}

else{

Serial.print(" - No Set");

digitalWrite(setlevelMode1_led,LOW);

}

Serial.println("");

}

}

//This function will write to the shift registers -> the bar graph. It will write all of the values below the one specified HIGH and all above LOW. It also allows multiple sets of bar graphs

voidwritebargraph(intset,intled){

if(set==0){

for(inti=0;i<=9;i=i+1){

if(i<=led){

setRegisterPin(i,HIGH);

writeRegisters();

}

elseif(i>led){

setRegisterPin(i,LOW);

writeRegisters();

}

}

}

if(set==1){

for(intk=10;k<=29;k=k+1){

if(k<=10+led){

setRegisterPin(k,HIGH);

writeRegisters();

}

elseif(k>10+led){

setRegisterPin(k,LOW);

writeRegisters();

}

}

}

}

//A very simple function to write values to the Digital Pot

voiddigitalpot(intvalue){

digitalWrite(slaveSelectPin,LOW);

SPI.transfer(0);// enables the chip

SPI.transfer(value);

digitalWrite(slaveSelectPin,HIGH);

}

//SHIFT REGISTER FUNCTIONS.

//set all register pins to LOW

voidclearRegisters(){

for(inti=numOfRegisterPins-1;i>=0;i--){

registers[i]=LOW;

}

}

//Set and display registers

//Only call AFTER all values are set how you would like (slow otherwise)

voidwriteRegisters(){

digitalWrite(RCLK_Pin,LOW);

for(inti=numOfRegisterPins-1;i>=0;i--){

digitalWrite(SRCLK_Pin,LOW);

intval=registers[i];

digitalWrite(SER_Pin,val);

digitalWrite(SRCLK_Pin,HIGH);

}

digitalWrite(RCLK_Pin,HIGH);

}

//set an individual pin HIGH or LOW

voidsetRegisterPin(intindex,intvalue){

registers[index]=value;

}

If you have questions about the code please leave it in the comments.

Hey! This post was written a long time ago, but I'm leaving it up on the off-chance it may help someone, but proceed with caution. It may not be a good idea to blindly integrate this code or work into your project, but instead use it as a starting point.