Sparki Code Sharing Post

Hi Folks,
Seems (by the lack of new content on the forums) that people are starting to get the hang of programming Sparki. Good news :slight_smile:

Could I be so bold as to suggest using this topic to share some of your wonderful code?

I propose only posting to this forum if your code works fully. If there are issues with it, then there are loads of other posts, or a new one that can be utilised.

I am far from an expert, so I for one would benefit hugely from this sharing exercise.

Proposal:
Posts should follow the example I put up in a minute. I’m not dying to dictate…I just think it should be kept uniform.

Regards,
Trevor.

Description of Code:
Displays current & max readings for x, y & z Accelerometer readings.

Sparki Components Utilised:
Accelerometer
Remote
Wheels
LCD

[code]/*******************************************
IR Remote

Sparki has a sensor that lets it receives
commands from the included remote control.
Try moving it around!
********************************************/
#include <Sparki.h> // include the sparki library
float x_min = 1023;
float x_max = 0;
float y_min = 1023;
float y_max = 0;
float z_min = 1023;
float z_max = 0;

void setup()
{
}

// /------^-----
// | |
// | 69 70 71 |
// | 68 64 67 |
// | 7 21 9 |
// | 22 25 13 |
// | 12 24 94 |
// | 8 28 90 |
// | 66 82 74 |
// ____________/

void loop()
{
int code = sparki.readIR();

//if(code != -1){
//sparki.print("Received code: ");
//sparki.println(code);
sparki.clearLCD();

//acelerometer:
/*
float x = sparki.accelX(); // measure the accelerometer x-axis
float y = sparki.accelY(); // measure the accelerometer y-axis
float z = sparki.accelZ() + 9.81; // measure the accelerometer z-axis. The +9.81 is to offset reading gravity.
*/

//magnetometer:
float x = sparki.magX(); // measure the magnetometer x-axis
float y = sparki.magY(); // measure the magnetometer y-axis
float z = sparki.magZ(); // measure the magnetometer z-axis.

if (x < x_min) x_min = x;
if (x > x_max) x_max = x;
if (y < y_min) y_min = y;
if (y > y_max) y_max = y;
if (z < z_min) z_min = z;
if (z > z_max) z_max = z;

// write the measurements to the screen
sparki.print(“Accel X: “);
sparki.println(x, 2);
sparki.print(“MinX:”);
sparki.print(x_min, 1);
sparki.print(” MaxX:”);
sparki.println(x_max, 2);
sparki.println("");

sparki.print(“Accel y: “);
sparki.println(y, 2);
sparki.print(“MinY:”);
sparki.print(y_min, 1);
sparki.print(” MaxY:”);
sparki.println(y_max, 2);
sparki.println("");

sparki.print(“Accel z: “);
sparki.println(z, 2);
sparki.print(“MinZ:”);
sparki.print(z_min, 1);
sparki.print(” MaxZ:”);
sparki.print(z_max, 2);

sparki.updateLCD(); // display all of the information written to the screen
delay(100);
//}

switch(code){

// Movement buttons
case 22: sparki.clearLCD();
delay(1000); break;

case 70: sparki.moveForward(); break;
case 21: sparki.moveBackward(); break;
case 71:
case 67: sparki.moveRight(); break;
case 69:
case 68: sparki.moveLeft(); break;
case 64: sparki.moveStop();
sparki.gripperStop();
break;

// Gripper Buttons
case 9: sparki.gripperOpen(); break;
case 7: sparki.gripperClose(); break;

// buzzer
case 13: sparki.beep(); break;

// Servo Buttons
case 12: sparki.servo(SERVO_LEFT); break;
case 24: sparki.servo(SERVO_CENTER); break;
case 94: sparki.servo(SERVO_RIGHT); break;

// RGB LED
case 90: sparki.RGB(RGB_OFF); break;
case 66: sparki.RGB(RGB_RED); break;
case 82: sparki.RGB(RGB_GREEN); break;
case 74: sparki.RGB(RGB_BLUE); break;

default:
break;

}

}
[/code]

I don’t think a single Forum Topic will work. First of all it discourages replies; or if there are questions and replies posted they will make it hard to find individual new sketches over time.

Either the forum Admin will have to create a new Forum Major Group (e.g. “User Software Contributions”) or as an alternative I would suggest that a common Topic “subject” format be used for new user contributed sketches and library functions. The latter method would also let people do a forum search to locate all such posts (e.g. search “[NEW Sketch]”).

Proposed Standard Subject Format:

[NEW Sketch] Sketch_Name “sketch short description”

[NEW Function] Function_Name “function short description”

****** Message Content ******

Description of Sketch

Displays current & max readings for x, y & z Accelerometer readings.

Sparki Components Utilized

Accelerometer; Remote; Wheels; LCD; xxxxxx

Sparki.h and Sparki.cpp Version Dependencies

Sparki.h 20140218 (Magnetometer angle offset)

Notes

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.

/*******************************************
  IR Remote
  
  Sparki has a sensor that lets it receives
  commands from the included remote control.
  Try moving it around!
********************************************/
#include <Sparki.h> // include the sparki library

Trevor: Your code confused me a bit. You commented the accelerometer measurements and used the magnetometer instead. But the screen displays indicate “Accel X, Accel Y, and Accel Z”. They should have been “Mag X, Mag Y, Mag Z” when the values are displayed.

This sketch draws equilateral polygons. You control the number of sides by changing the value of “int sides” and the length of the sides by changing the value of “int length” . Use the recommended EXPO VIS-A-VIS PEN but wrap 2 wraps of scotch magic tape at the shoulder of the pen, You have to adjust the pen for depth this way but it holds the pen tight and you will get better polygons.

Name:GEO_POLYGON
Uses:Pen Holder and the Wheels, LCD, Buzzer, Magnetometer, and the LED.

                       [code]

                        #include <Sparki.h> // include the sparki library

//DRAWS (PLOTS) EQUALATERAL POLYGON OF WHAT EVER SIZE AND
//NUMBER OF SIDES YOU SPECIFY BY CHANGING VALUES OF “SIDES”
//AND LENGTH OF SIDES BELOW

void setup(){
sparki.servo(SERVO_CENTER); // center the servo
}
int sides = 20; // number of sides to polygram
int length = 2; // distance to travel change as desired
float multi = 1.055; // correction for sparki angles
float a = 360/sides; // angle to turn
void loop() {
sparki.clearLCD(); // wipe the screen

float x = sparki.magX(); // measure the Magnetometer x-axis
float y = sparki.magY(); // measure the Magnetometer y-axis
float z = sparki.magZ(); // measure the Magnetometer z-axis

// write the measurements to the screen
sparki.print("Mag X: ");
sparki.println(x);

sparki.print("Mag Y: ");
sparki.println(y);

sparki.print("Mag Z: ");
sparki.println(z);
sparki.print("LENGTH OF SIDES: ");
sparki.println(length);
sparki.print("ANGLE TURNED: ");
sparki.println(a*multi);

        sparki.RGB(100,0,0); // turn the light red
        sparki.beep(); // beep!
        sparki.moveForward(length); //length of side
        sparki.RGB(0,0,100); // turn the light blue
        sparki.moveRight(a*multi); // rotate right

sparki.updateLCD(); // display all of the information written to the screen

delay(1000); // wait 1 seconds
}
[/code]

HAVE FUN WITH THIS

What happened to the Code Sharing? Did the newness wear off or we just driving Sparki around with the remote? Come and throw this old dog a bone.

Here’s the forum link for a maze following program I started: [NEW Sketch] Maze_Walk1 – Navigate Through a Maze

I posted it as a new topic so it stands out and can be updated over time, which is what I suggested above.

Hi,
Could I suggest an organization of the forum like this:
a - issues / solutions / code samples by sensor, building a knowledge base and a library of tricks for each sensor
b - project based topics like for instance the “find and pickup post” of craig where you could state a challenge and invite solutions.
In case of the “find and pick” a project description/challenge could be:

// assume 5 objects, mine are pine 1 x 1 1/2 x 4", smooth square rectangles
// big enough to recognize light/smooth enough to push/drag
// aasume a playing field, line or coordinates/distance driven, the latter is hard because
// of possible slipping wheels.
//
// start loop:
// center the pinging
// rotate on axis doing pings for 360 degrees.
// record object number, angle and range if you ping something in range
// stop scanning after going around 360.
// select closest object
// turn to it
// get somewhat closer
// try to get the angle and distance right
// go the distance as square as possible
// grab the object
// determine a new place for it and put it there,
// the place could be predetermined places storage places or random new ones within the playing field.
// go to a new point either random within the playing field and start over at loop, or
// find the next object based on known information.
//
// Q: record the track, time elapsed ??
//
//====================

Parts of solutions of these projects could be split off or duplicated in the sensor areas and vice versa.

c: whatever other splits could made…

Feel free the comment…
Dirk