Assignment:
Also for the week of output I decided to focus on what could be useful for the final project. On the board of last week I had already assumed the connectors for a servo motor. Unfortunately, however, one of the connectors on the card has broken (that of the Hall sensor) so at this moment I can not connect inputs and outputs to program them at the same time.
After two tiring weeks I managed to draw and mill a working board (for now). you can see all the process on my Input page and you can download the png file of board here:
Before starting I tried to better understand how a servo motor works and how it moves. I had already read part of the datasheet while designing the board to understand the pinout. I have not really chosen which servo motor I will use in the final project but it seems that there is not much difference (I hope).
I started by testing the servo motor with an example code to understand how it works.
#includeServo myservo; //servo control int pos = 0; //posizione iniziale void setup(){ myservo.attach(10); //(Digital pin 10) Serial.begin(19200); // 19200 is the rate of communication Serial.println("Haupei"); // some output for debug purposes. } void loop() { for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } } }
I started from the arduino example code to move a servo with a potentiometer.
With arduino everything worked, with my board did not work right away but at the end the sevo motor moves when the magnet approaches the sensor.
For the moment it is a good result, for the final project, however, the servant must move at a certain angle and then remain in position until a different order, there will be to write that part of the code.
/* Controlling a servo position using a hall sensor */ #includeServo myservo; // create servo object to control a servo int potpin = A0; // analog pin used to connect the hallsensorboard int val; // variable to read the value from the analog pin void setup() { myservo.attach(10); // attaches the servo on pin 10 to the servo object } void loop() { val = analogRead(potpin); // reads the value of the hallsensor val = map(val, 0, 1023, 0, 179); // scale it (tobereviewed!) to use it with the servo (value between 0 and 180) myservo.write(val); // sets the servo position according to the scaled value (for now it's ok) delay(15); // waits for the servo to get there }
I wrote another part of the code for my final project (it isn't the final version!) , with the help Francesco of I still have to test it, but you can download it below.
//code for slave: for open and closed drawers with a master board #include//for i2c #include #define drawer_pin 5 //digital input pin #define servo_pin 10 //pwm servo pin #define lock_pin A0 //analog input pin hall sensor #define address_i2c 8 //address bus #define lockthreshold 20 //hall sensor analogue reading threshold #define closepos 20 //servo position closing (block inserted) #define openpos 160 //posizione servo opening (block not inserted) Servo moveLock; // create servo object to control a servo char todo = 'a'; void setup() { Wire.begin(address_i2c); // join i2c bus with address #8 Wire.onReceive(receiveEvent); // register event Wire.onRequest (requestEvent); Serial.begin(115200); // start serial for output Serial.println("Start..."); pinMode (drawer_pin, INPUT); //drawer pin (switch digital input) moveLock.attach(servo_pin); // attaches the servo on pin 10 to the servo object if (drawer() && isLock()) { //initial check to see if it is closed closed(); //move servo motor } else { opened(); //move servo motor todo = 'a'; } } void loop() { switch (todo) { case 'a': //drawer is open and must be closed if (drawer ()) { if (isLock ()) { closed(); todo = '0'; // nothing } } break; case 'b': //The drawer has just opened because the master has told him to do it. and must not close up if ( isLock() == false ) { //do it if the block has been removed todo = 'a'; //make sure that you check whether it should close or not } break; default: break; } delay(100); } //he does it when something is sent to him from the master void receiveEvent(int howMany) { String cmd = ""; //buffer while (Wire.available()) { //if there is at least one byte to read he performs. - 0 lockthreshold); //the more I'm getting closer, the more it increases } //turn servo motor to close void closed () { moveLock.write(closepos); Serial.println("closed"); //write on the serial when it's closing, useful for verification } //turn servo motor to open void opened() { moveLock.write(openpos); Serial.println("opened"); //write on the serial when it's opening, useful for verification } //It's open? if is open: true, if is closed: false bool isOpen () { return (moveLock.read() == openpos);
outputweek fabacademy_hallsensor_move_servo motor from lauracip on Vimeo.