jmunsch

multiplexer arduino code

May 17th, 2014
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.35 KB | None | 0 0
  1. int write_pins[] = { 0,1,2,3,4,5,6 };
  2. long max_min_array[16][2];
  3.  
  4.  
  5. int max_min(int pin,int value){
  6.   //-------------------------------------------
  7.   // set to initial reads and then move min_max
  8.   // if value > min
  9.   // if value < max
  10.   //-------------------------------------------
  11.   //Serial.println("inside max_min");
  12.   //Serial.println(pin);
  13.   int MIN = max_min_array[pin][0];
  14.   int MAX = max_min_array[pin][1];
  15.  
  16.   if ( ( MIN && MAX ) == 0 ){
  17.     max_min_array[pin][0] = value;
  18.     max_min_array[pin][1] = value;
  19.   return 0;
  20.   }
  21.   // min
  22.   if ( MIN > value ){
  23.     max_min_array[pin][0] = value;
  24.     return 1;
  25.   }
  26.   // max
  27.   if ( MAX < value ){
  28.     max_min_array[pin][1] = value;
  29.     return 2;
  30.   }
  31.   return 3;
  32. }
  33.  
  34. int mux_pin_select(int mux_pin){
  35.   //-------------------------------------------
  36.   //Serial.println("inside mux_pin_select");
  37.   // sets select pins based on pin (pin = decimal)
  38.   // write_pins 0,1 = empty
  39.   // write_pins 2 = Disable
  40.   // write_pins 3,4,5,6 = S0,S1,S2,S3
  41.   //-------------------------------------------
  42.   int select_pin_map[] = { 3,4,5,6 };
  43.   //Serial.println(mux_pin);
  44.   for ( int b = 0; b < 4;b++){
  45.  
  46.     int bitset = bitRead(mux_pin, b);
  47.     //Serial.print(bitset);
  48.     digitalWrite(select_pin_map[b], bitset);
  49.   }
  50.   //Serial.println("");
  51.   return 0;
  52. }
  53.  
  54. int multiplex_read(){
  55.   //-------------------------------------------
  56.   // For mux_pin select pin "mux_pin_select()", read A0, and
  57.   // store "max_min(mux_pin,sensorValue)"
  58.   //-------------------------------------------
  59.   //Serial.println("inside multiplex read");
  60.   //Serial.println("##### select pins #####");
  61.   for (int mux_pin = 0; mux_pin <= 15; mux_pin++){
  62.     mux_pin_select(mux_pin);
  63.     long sensorValue = analogRead(A0);
  64.     for (int i = 0; i < 5; i++){
  65.       sensorValue = analogRead(A0) + sensorValue;
  66.     }
  67.     sensorValue = sensorValue/5;
  68.     int result = max_min(mux_pin,sensorValue);
  69. /*    
  70.     Serial.print("sensorValue: ");
  71.     Serial.print(sensorValue);
  72.     Serial.print(" max_min_return: ");
  73.     Serial.print(result);
  74.     if (result == 0){
  75.       Serial.println(" Initial Value Added");
  76.     }
  77.     if (result == 1){
  78.       Serial.println(" Minimum Value Added");
  79.     }
  80.     if (result == 2){
  81.       Serial.println(" Maximum Value Added");
  82.     }
  83.     if (result == 3){
  84.       Serial.println(" No Value Added");
  85.     }
  86. */    
  87.   }
  88.   return 0;
  89. }
  90.  
  91.  
  92. void setup(){
  93.   // clear max_min_array and multiplex_read_array
  94.   for ( int i = 0; i <= 15; i++){
  95.     max_min_array[i][0] = 0;
  96.     max_min_array[i][1] = 0;
  97.   }
  98.   // initialize write pins
  99.   for ( int i = 0; i <= 7; i++){  
  100.     pinMode(write_pins[i], OUTPUT);
  101.   }
  102.   Serial.begin(115200);
  103.   Serial.println("\nInitializing Serial.\nInput Alphanumeric and Press Enter/Send.");
  104. }
  105.  
  106. void loop(){
  107.   if ( Serial.available()){
  108.     Serial.println("\nStarting multiplex_read().");
  109.     while (true){
  110.       for ( int i = 0; i <= 250; i++){  // approx 4 seconds with the current code
  111.         multiplex_read();
  112.       }
  113.       Serial.println("### Min Max Array #########");
  114.       for (int i = 0; i <= 15; i++){
  115.         Serial.print("\n### Row/Mux: ");
  116.         Serial.println(i);
  117.         for (int j = 0; j <= 1; j++){
  118.         Serial.print(max_min_array[i][j]);
  119.         Serial.print(" ");
  120.         }
  121.       }
  122.     }
  123.   }
  124. }
  125.  
  126.  
  127. ##################
  128. output
  129. ###################
  130.  
  131. ### Min Max Array #########
  132.  
  133. ### Row: 0
  134. 365 375
  135. ### Row: 1
  136. 441 458
  137. ### Row: 2
  138. 231 248
  139. ### Row: 3
  140. 420 432
  141. ### Row: 4
  142. 335 369
  143. ### Row: 5
  144. 0 0
  145. ### Row: 6
  146. 0 0
  147. ### Row: 7
  148. 0 0
  149. ### Row: 8
  150. 0 0
  151. ### Row: 9
  152. 0 0
  153. ### Row: 10
  154. 0 0
  155. ### Row: 11
  156. 0 0
  157. ### Row: 12
  158. 0 0
  159. ### Row: 13
  160. 0 0
  161. ### Row: 14
  162. 0 0
  163. ### Row: 15
  164. 0 0 ### Min Max Array #########
  165.  
  166. ### Row: 0
  167. 365 1096
  168. ### Row: 1
  169. 441 460
  170. ### Row: 2
  171. 231 260
  172. ### Row: 3
  173. 418 432
  174. ### Row: 4
  175. 335 376
  176. ### Row: 5
  177. 0 0
  178. ### Row: 6
  179. 0 0
  180. ### Row: 7
  181. 0 0
  182. ### Row: 8
  183. 0 0
  184. ### Row: 9
  185. 0 0
  186. ### Row: 10
  187. 0 0
  188. ### Row: 11
  189. 0 0
  190. ### Row: 12
  191. 0 0
  192. ### Row: 13
  193. 0 0
  194. ### Row: 14
  195. 0 0
  196. ### Row: 15
  197. 0 0 ### Min Max Array #########
  198.  
  199. ### Row: 0
  200. 290 1096
  201. ### Row: 1
  202. 194 1078
  203. ### Row: 2
  204. 231 272
  205. ### Row: 3
  206. 409 432
  207. ### Row: 4
  208. 335 376
  209. ### Row: 5
  210. 0 0
  211. ### Row: 6
  212. 0 0
  213. ### Row: 7
  214. 0 0
  215. ### Row: 8
  216. 0 0
  217. ### Row: 9
  218. 0 0
  219. ### Row: 10
  220. 0 0
  221. ### Row: 11
  222. 0 0
  223. ### Row: 12
  224. 0 0
  225. ### Row: 13
  226. 0 0
  227. ### Row: 14
  228. 0 0
  229. ### Row: 15
  230. 0 0 ### Min Max Array #########
  231.  
  232. ### Row: 0
  233. 290 1096
  234. ### Row: 1
  235. 194 1078
  236. ### Row: 2
  237. 231 1043
  238. ### Row: 3
  239. 406 432
  240. ### Row: 4
  241. 335 379
  242. ### Row: 5
  243. 0 0
  244. ### Row: 6
  245. 0 0
  246. ### Row: 7
  247. 0 0
  248. ### Row: 8
  249. 0 0
  250. ### Row: 9
  251. 0 0
  252. ### Row: 10
  253. 0 0
  254. ### Row: 11
  255. 0 0
  256. ### Row: 12
  257. 0 0
  258. ### Row: 13
  259. 0 0
  260. ### Row: 14
  261. 0 0
  262. ### Row: 15
  263. 0 0 ### Min Max Array #########
  264.  
  265. ### Row: 0
  266. 290 1096
  267. ### Row: 1
  268. 194 1078
  269. ### Row: 2
  270. 109 1043
  271. ### Row: 3
  272. 356 1020
  273. ### Row: 4
  274. 335 379
  275. ### Row: 5
  276. 0 0
  277. ### Row: 6
  278. 0 0
  279. ### Row: 7
  280. 0 0
  281. ### Row: 8
  282. 0 0
  283. ### Row: 9
  284. 0 0
  285. ### Row: 10
  286. 0 0
  287. ### Row: 11
  288. 0 0
  289. ### Row: 12
  290. 0 0
  291. ### Row: 13
  292. 0 0
  293. ### Row: 14
  294. 0 0
  295. ### Row: 15
  296. 0 0 ### Min Max Array #########
  297.  
  298. ### Row: 0
  299. 290 1096
  300. ### Row: 1
  301. 194 1078
  302. ### Row: 2
  303. 109 1043
  304. ### Row: 3
  305. 317 1020
  306. ### Row: 4
  307. 286 1068
  308. ### Row: 5
  309. 0 0
  310. ### Row: 6
  311. 0 0
  312. ### Row: 7
  313. 0 0
  314. ### Row: 8
  315. 0 0
  316. ### Row: 9
  317. 0 0
  318. ### Row: 10
  319. 0 0
  320. ### Row: 11
  321. 0 0
  322. ### Row: 12
  323. 0 0
  324. ### Row: 13
  325. 0 0
  326. ### Row: 14
  327. 0 0
Advertisement
Add Comment
Please, Sign In to add comment