6 years, 8 months ago.  This question has been closed. Reason: Unclear question

How to control servo motor position using microbit's accelerometer?

I am trying to program it such that when I tilt the microbit, the servo position follows it.

This is my code for the transmitter microbit.

/* The MIT License (MIT)

Copyright (c) 2016 British Broadcasting Corporation. This software is provided by Lancaster University by arrangement with the BBC.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

  • /
  1. include "MicroBit.h"
  2. include <string>

MicroBit uBit; Serial pc(USBTX, USBRX); Scales the given value that is in the -1024 to 1024 range int a value between 0 and 4. int pixel_from_g(int value) { int x = 0;

if (value > -750) x++; if (value > -250) x++; if (value > 250) x++; if (value > 750) x++;

return x; }

int main() { Initialise the micro:bit runtime. uBit.init(); uBit.radio.enable(); Periodically read the accelerometer x and y values, and plot a scaled version of this ont the display. while(1) { int acc_x = uBit.accelerometer.getX(); int acc_y = uBit.accelerometer.getY();

int x = pixel_from_g(acc_x); int y = pixel_from_g(acc_y); uBit.display.image.clear(); uBit.display.image.setPixelValue(x, y, 255);

char buf1[10];

ManagedString s1(acc_x); ManagedString s2(acc_y); ManagedString s1("123"); ManagedString s2("45"); ManagedString s = s1 + "," + s2; std::sprintf (buf1, "%s",acc_x ); uBit.radio.datagram.send(s);

pc.printf("tx\r\n"); uBit.sleep(1000); } }

And this is my code for the receiver microbit.

/* The MIT License (MIT)

Copyright (c) 2016 British Broadcasting Corporation. This software is provided by Lancaster University by arrangement with the BBC.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

  • /
  1. include "MicroBit.h"
  2. include <stdlib.h>
  3. include <stdio.h>
  4. include <string.h>
  5. include <string>

using namespace std; Serial pc(USBTX, USBRX);

MicroBit uBit;

Scales the given value that is in the -1024 to 1024 range int a value between 0 and 4.

int pixel_from_g(int value) { int x = 0;

if (value > -750) x++; if (value > -250) x++; if (value > 250) x++; if (value > 750) x++;

return x; }

void onData(MicroBitEvent) { ManagedString b = uBit.radio.datagram.recv(); int num = atoi(b.toCharArray()); char seps[] = ","; char *token1; char *token2;

token1 = strtok( (char*)b.toCharArray(), seps ); pc.printf("t1 = %s", token1); token2 = strtok( NULL, seps ); pc.printf("t2 = %s", token2);

int acc_x = atoi(token1); int acc_y = atoi(token2); int x = pixel_from_g(acc_x); int y = pixel_from_g(acc_y); uBit.display.image.clear(); uBit.display.image.setPixelValue(x, y, 255); }

int main() { Initialise the micro:bit runtime. uBit.init(); uBit.messageBus.listen(MICROBIT_ID_RADIO, MICROBIT_RADIO_EVT_DATAGRAM, onData); uBit.radio.enable();

char str1[] = "123"; int num = atoi(str1); pc.printf("rx\r\n");

while(1) {

int x = pixel_from_g(acc_x); int y = pixel_from_g(acc_y); uBit.display.image.setPixelValue(x, y, 255);

uBit.sleep(1000); } }