/*Program Written and Edited by Cheng Lin and Elijah Sensenig *Last edited 5/5/2016 (fully working at the time) **Note threshold values may change and may need to be edited in the future *Takes an analog signal in (from Hall Effect sensor) and outputs a PWM signal *dependent on which Hall Effect sensor sent an input and the direction of the magnetic *flux. *If loops could and should be condensed using if/else statements. *Loops were not condensed due to a time constraint. */ /*Beginning of declarations and main settings*/ #include int32_t frequency = 35; //Frequency in Hz for analog input static double freq = 35; //frequency in Hz for digital output static double x = 2; //used for duty cycle (2 = 50%, 1 = 100% etc.) volatile double us; //variable used for the frequency to microsecond conversion const int analogPin0 = 0; //inputs from hall effect sensors const int analogPin1 = 1; const int analogPin2 = 2; const int analogPin3 = 3; /*Beginning of setup loop*/ void setup() { us = (1/(freq*x))*pow(10,6); /*the x value sets up duty cycle (x=2 => 50%, x=3 => 33.33%, etc.)*/ pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); pinMode(5, OUTPUT); Serial.begin(9600); //Serial.print(freq, DEC); //printing freq variable to ensure its correct //Serial.print("\t"); //Serial.print(us, DEC); //printing us variable to ensure the conversion worked } /*Beginning of main loop*/ void loop() { Serial.print("\t"); //Serial.print(analogRead(analogPin0)); //printing current analog pin reading value int val0 = analogRead(analogPin0); //setting variable to the current analog pin value //Serial.print("\t"); //Serial.print(analogRead(analogPin1)); //printing current analog pin reading value int val1 = analogRead(analogPin1); //setting variable to the current analog pin value //Serial.print("\t"); //Serial.print(analogRead(analogPin2)); //printing current analog pin reading value int val2 = analogRead(analogPin2); //setting variable to the current analog pin value //Serial.print("\t"); //Serial.print(analogRead(analogPin3)); //printing current analog pin reading value int val3 = analogRead(analogPin3); //setting variable to the current analog pin value //Serial.print("\t"); delay(50); /*two if loops below are so the magnet can face either direction * currently they are opposite PWMs * this was done just to note a difference while testing and isn't actually needed*/ //folling loops are for input 0 if(val0 < 495) { while(val0 < 495) { digitalWrite(2, HIGH); delayMicroseconds(us); digitalWrite(2, LOW); //Serial.print("\t"); //Serial.print(analogRead(analogPin0)); val0 = analogRead(analogPin0); delay(50); } } if(val0 > 509) { while(val0 > 509) { digitalWrite(2, LOW); delayMicroseconds(us); digitalWrite(2, HIGH); //Serial.print("\t"); //Serial.print(analogRead(analogPin0)); val0 = analogRead(analogPin0); delay(50); } digitalWrite(2, LOW); //setting output back to low after loop } //following loops are for input 1 if(val1 < 485) { while(val1 < 485) { digitalWrite(3, HIGH); delayMicroseconds(us); digitalWrite(3, LOW); //Serial.print("\t"); //Serial.print(analogRead(analogPin1)); val1 = analogRead(analogPin1); delay(50); } } if(val1 > 492) { while(val1 > 492) { digitalWrite(3, LOW); delayMicroseconds(us); digitalWrite(3, HIGH); //Serial.print("\t"); //Serial.print(analogRead(analogPin1)); val1 = analogRead(analogPin1); delay(50); } digitalWrite(3, LOW); //setting output back to low after loop } //folling loops are for input 2 if(val2 < 495) { while(val2 < 495) { digitalWrite(4, HIGH); delayMicroseconds(us); digitalWrite(4, LOW); //Serial.print("\t"); //Serial.print(analogRead(analogPin2)); val2 = analogRead(analogPin2); delay(50); } } if(val2 > 509) { while(val2 > 509) { digitalWrite(4, LOW); delayMicroseconds(us); digitalWrite(4, HIGH); //Serial.print("\t"); //Serial.print(analogRead(analogPin2)); val2 = analogRead(analogPin2); delay(50); } digitalWrite(4, LOW); //setting output back to low after loop } //following loops are for input 3 if(val3 < 489) { while(val3 < 489) { digitalWrite(5, HIGH); delayMicroseconds(us); digitalWrite(5, LOW); //Serial.print("\t"); //Serial.print(analogRead(analogPin3)); val3 = analogRead(analogPin3); delay(50); } } if(val3 > 500) { while(val3 > 500) { digitalWrite(5, LOW); delayMicroseconds(us); digitalWrite(5, HIGH); //Serial.print("\t"); //Serial.print(analogRead(analogPin3)); val3 = analogRead(analogPin3); delay(50); } digitalWrite(5, LOW); //setting output back to low after loop } }