// // samd11c14a_hallsensor.ino // // SAMD11C and HALL SOT23 Sensor // // Joanne Leong // // This work may be reproduced, modified, distributed, // performed, and displayed for any purpose. Copyright is // retained and must be preserved. The work is provided // as is; no warranty is provided, and users accept all // liability. //Joanne's Board Details const int SENSOR_PIN = 14; //PA04 --> external hall sensor board const int LED = 5; //PA05 --> integrated led //Sensor variables int raw_val = 0; int smoothed_val=0; //Store analog and digital values bool is_tared = false; int offset = 0; //how much to offset raw sensor signal after tare //Smoothing const int numReadings = 10; int readings[numReadings]; //readings from the analog input int readIndex = 0; //index of hte current reading int total = 0; // running total int average = 0; // the average void setup() { pinMode(LED, OUTPUT); //initialize the LED pins as outputs: pinMode(SENSOR_PIN, INPUT); //initialize the analog pin as input: //initialize serial communication with the computer Serial.begin(9600); //initialize all the readings to 0 for (int thisReading = 0; thisReading < numReadings; thisReading++){ readings[thisReading] = 0; } } void loop() { raw_val = analogRead(SENSOR_PIN); smoothed_val = smoothing(tare(raw_val)); if(smoothed_val > -3 && smoothed_val< 3){smoothed_val = 0;} //filter out fine noise Serial.println(smoothed_val); //make led light up if field value is large enough if(abs(smoothed_val) > 50){ digitalWrite(LED,HIGH); }else{ digitalWrite(LED,LOW); } } int tare(int val){ if(!is_tared){ offset = val; is_tared = true; } return val-offset; } int smoothing(int val){ total = total - readings[readIndex]; //subtract the last reading readings[readIndex] = val; total = total + readings[readIndex]; // add the reading to the total readIndex = readIndex + 1; //advance to the next position in the array if (readIndex >= numReadings){ //reset index when it exceeds number of readings readIndex =0; } average = total / numReadings; //calculate the average delay(1); return average; } //void blink(){ // Serial.println("LED ON"); // digitalWrite(Integrated_Led,HIGH); // delay(1000); // digitalWrite(Integrated_Led,LOW); // delay(1000); //}