New project! First, here’s a video:
This is the proof of concept for the volume control of the speaker system. In it’s final form, this sensor will be exposed to the outside and will allow users to control the volume without opening the system, preserving the fidelity of the inside. For example, imagine a user is at the beach and wants to change the volume but they have sand covered hands or wet hands. This system will solve that problem.
The sensor is theĀ Sharp GP2Y0A41SK0F. Here are some very very macro shots of sensors inner workings.
But now for what you came here for, the code. It’s very poorly commented as this is just a prototype, but it’s better than nothing. As this project progresses I’ll posted updated versions of this code.
This demo also relies heavily this shift register. I still haven’t decided if i’m going to use a buzzer to interact with the user or if I’m going to use these bar graphs.
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 |
int SER_Pin = 10; //pin 14 on the 75HC595 int RCLK_Pin = 11; //pin 12 on the 75HC595 int SRCLK_Pin = 12; //pin 11 on the 75HC595 //How many of the shift registers - change this #define number_of_74hc595s 4 //do not touch #define numOfRegisterPins number_of_74hc595s * 8 boolean registers[numOfRegisterPins]; int detect_led = 2; int setlevelMode0_led = 3; int setlevelMode1_led = 4; int setlevelMode2_led = 5; int seeking_led = 6; int check_val = 8; void setup(){ pinMode(SER_Pin, OUTPUT); pinMode(RCLK_Pin, OUTPUT); pinMode(SRCLK_Pin, OUTPUT); //reset all register pins clearRegisters(); writeRegisters(); pinMode(detect_led, OUTPUT); pinMode(setlevelMode0_led, OUTPUT); pinMode(setlevelMode1_led, OUTPUT); pinMode(setlevelMode2_led, OUTPUT); pinMode(seeking_led, OUTPUT); Serial.begin(9600); } int prelevel_0 = 0; int prelevel_1 = 0; int prelevel_2 = 0; int prelevel_3 = 0; int prelevel_4 = 0; int prelevel_5 = 0; int prelevel_6 = 0; int prelevel_7 = 0; int prelevel_8 = 0; int prelevel_9 = 0; int pre_positions[10] = {prelevel_0, prelevel_1, prelevel_2, prelevel_3, prelevel_4, prelevel_5, prelevel_6, prelevel_7, prelevel_8, prelevel_9}; void loop(){ for(int i = 0; i <= 9; i = i + 1){ writebargraph(0,map(analogRead(0),20,600,0,9)); pre_positions[i] = map(analogRead(0),20,600,0,9); if(pre_positions[i] == check_val){ Serial.println("Check Detected"); digitalWrite(detect_led, HIGH); } else { digitalWrite(detect_led, LOW); } delay(30); } for(int i = 0; i <= 9; i = i + 1){ Serial.print(pre_positions[i]); Serial.print(","); } if (pre_positions[0] == check_val && pre_positions[1] == check_val && pre_positions[2] == check_val && pre_positions[3] == check_val && pre_positions[4] == check_val && pre_positions[5] == check_val && pre_positions[6] == check_val && pre_positions[7] == check_val && pre_positions[8] == check_val && pre_positions[9] == check_val ){ Serial.print(" - Pre Level Set"); Serial.println(""); delay(500); setlevel(); delay(500); } else { Serial.println(" - No Set"); } } //set all register pins to LOW void clearRegisters(){ for(int i = 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) void writeRegisters(){ digitalWrite(RCLK_Pin, LOW); for(int i = numOfRegisterPins - 1; i >= 0; i--){ digitalWrite(SRCLK_Pin, LOW); int val = registers[i]; digitalWrite(SER_Pin, val); digitalWrite(SRCLK_Pin, HIGH); } digitalWrite(RCLK_Pin, HIGH); } //set an individual pin HIGH or LOW void setRegisterPin(int index, int value){ registers[index] = value; } void writebargraph(int set, int led){ if(set == 0){ for(int i = 0; i <= 9; i = i + 1){ if(i <= led){ setRegisterPin(i, HIGH); writeRegisters(); } else if(i > led){ setRegisterPin(i, LOW); writeRegisters(); } } } if(set == 1){ for(int k = 10; k <= 29; k = k + 1){ if(k <= 10 + led){ setRegisterPin(k, HIGH); writeRegisters(); } else if(k > 10 + led){ setRegisterPin(k, LOW); writeRegisters(); } } } } void setlevel(){ int level0 = 0; int level1 = 0; int level2 = 0; int level3 = 0; int level4 = 0; int level5 = 0; int level6 = 0; int level7 = 0; int level8 = 0; int level9 = 0; int level10 = 0; int level11 = 0; int level12 = 0; int level13 = 0; int level14 = 0; int level15 = 0; int level16 = 0; int level17 = 0; int level18 = 0; int level19 = 0; int level20 = 0; int level21 = 0; int level22 = 0; int level23 = 0; int level24 = 0; int level25 = 0; int level26 = 0; int level27 = 0; int level28 = 0; int level29 = 0; int positions[30] = { level0, level1, level2, level3, level4, level5, level6, level7, level8, level9, level10, level11, level12, level13, level14, level15, level16, level17, level18, level19, level20, level21, level22, level23, level24, level25, level26, level27, level28, level29}; digitalWrite(setlevelMode1_led, LOW); digitalWrite(setlevelMode2_led, LOW); boolean seeking = true; while(seeking == true){ for(int i = 0; i <= 29; i = i + 1){ writebargraph(1,map(analogRead(0),20,600,0,19)); positions[i] = map(analogRead(0),20,600,0,19); Serial.print(positions[i]); Serial.print(","); delay(10); } if (positions[0] == positions[0] && positions[1] == positions[0] && positions[2] == positions[0] && positions[3] == positions[0] && positions[4] == positions[0] && positions[5] == positions[0] && positions[6] == positions[0] && positions[7] == positions[0] && positions[8] == positions[0] && positions[9] == positions[0] && positions[10] == positions[0] && positions[11] == positions[0] && positions[12] == positions[0] && positions[13] == positions[0] && positions[14] == positions[0] && positions[15] == positions[0] ){ Serial.print(" - Level Set"); digitalWrite(setlevelMode1_led, HIGH); seeking = false; } else { Serial.print(" - No Set"); digitalWrite(setlevelMode1_led, LOW); } Serial.println(""); } } |
Here are some photos of the board if you want to try and work out the schematic:
Do you have any idea what’s the minimum distance that this sensor would be able to detect? I’m considering using it to measure water level, but I would like to get the sensor as close as possible to the fluid.
according to the datasheet it’s 4cm