RoboCatz.com

Robot Art (updated 2017)

#pragma config(Motor,  motorA,   frontArm,     tmotorNXT, PIDControl, encoder)
#pragma config(Motor,  motorB,   leftMotor,    tmotorEV3_Large, PIDControl, encoder)
#pragma config(Motor,  motorC,   rightMotor,   tmotorEV3_Large, PIDControl, encoder)
#pragma config(Motor,  motorD,   backArm,      tmotorEV3_Medium, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
/*-----------------------------------
  Useful Pragmas
 #pragma config(Sensor, S1,     touchSensor1,   sensorEV3_Touch)
 #pragma config(Sensor, S2,     colorSensor,    sensorEV3_Color)
 #pragma config(Sensor, S3,     ultrasonicSensor, sensorEV3_Gyro)
------------------------------------*/

#define print writeDebugStreamLine
#define clear clearDebugStream
#define setLED setLEDColor

#define drawCircle(CenterX, CenterY, Radius) drawEllipse(CenterX-Radius, CenterY+Radius, CenterX+Radius, CenterY-Radius)
#define eraseCircle(CenterX, CenterY, Radius) eraseEllipse(CenterX-Radius, CenterY+Radius, CenterX+Radius, CenterY-Radius)
#define fillCircle(CenterX, CenterY, Radius) fillEllipse(CenterX-Radius, CenterY+Radius, CenterX+Radius, CenterY-Radius)

#define drawRectangle(Left, Bottom, Width, Height) drawRect(Left, Bottom+Height, Left+Width, Bottom)
#define eraseRectangle(Left, Bottom, Width, Height) eraseRect(Left, Bottom+Height, Left+Width, Bottom)
#define fillRectangle(Left, Bottom, Width, Height) fillRect(Left, Bottom+Height, Left+Width, Bottom)

#define clearDisplay eraseDisplay

#define sine(Degrees) sinDegrees(Degrees)
#define cosine(Degrees) cosDegrees(Degrees)

#define random(Limit) abs(rand()%Limit)

//####################################################
// Graphics Library Structures
typedef struct coord_struct { int x; int y; } coordinate;

void display(int lineNum, string *sgb) {    // Display a message on a certain line
	nxtDisplayTextLine(lineNum, "%s", sgb);
}
////////////////////////////////////////////////////////////////////////////////
//
// Menu System Primitives
//
////////////////////////////////////////////////////////////////////////////////
#define MAX_MENU_ITEMS  20
typedef enum ProgramMenuItem { RED_FUNCTION, ORANGE_FUNCTION, YELLOW_FUNCTION, GREEN_FUNCTION, BLUE_FUNCTION, INDIGO_FUNCTION, VIOLET_FUNCTION, ULTRAVIOLET_FUNCTION };
string menu_prompts[MAX_MENU_ITEMS];
int menu[MAX_MENU_ITEMS];
int lastMenuItem = 0;
int scrollTopMenuItem = 0;
int cursorAtMenuItem = 0;
int selectedItem = 0;

int addMenuItem(string *promptStr, ProgramMenuItem retVal)
{
  menu_prompts[lastMenuItem++] = *promptStr;
  return retVal;
}
void showMenu()
{
  display(1, "-- Missions --");
  int cdLine = 0;
  for(cdLine = 0; cdLine < 6; cdLine++)
  {
    displayBigTextLine((cdLine+2)*2, " %s", menu_prompts[cdLine+scrollTopMenuItem]);
  }
}

void showCursor(int menuItemNum) {
	int putOnLine = (menuItemNum - scrollTopMenuItem) + 2;
	displayBigTextLine(putOnLine*2, ">%s", menu_prompts[menuItemNum]);
}
void adjustMenuItem(const bool bIncrement) {
  if(bIncrement)
  {
    cursorAtMenuItem = ++cursorAtMenuItem % lastMenuItem;
    scrollTopMenuItem = (cursorAtMenuItem >= scrollTopMenuItem+6) ? ++scrollTopMenuItem: ((cursorAtMenuItem==0)? 0: scrollTopMenuItem);
  }
  else
  {
    cursorAtMenuItem = --cursorAtMenuItem % lastMenuItem;
    cursorAtMenuItem = (cursorAtMenuItem < 0) ? (lastMenuItem-1): cursorAtMenuItem;
    scrollTopMenuItem = (cursorAtMenuItem < scrollTopMenuItem) ? ((scrollTopMenuItem>0)?--scrollTopMenuItem : 0) : ((cursorAtMenuItem >= scrollTopMenuItem+6) ? cursorAtMenuItem-5: scrollTopMenuItem);
  }
  showMenu();
  showCursor(cursorAtMenuItem);
  playSoundFile("Click");
  wait(.2);
}
int startShowingTheMenu(int menuStartsAt)
{
  cursorAtMenuItem = menuStartsAt;
  showCursor(cursorAtMenuItem);
	setLEDColor(ledOrange);
  playSoundFile("Ready");
  wait(1);
	setLEDColor(ledOff);
  while(!getButtonPress(buttonEnter)) { // Wait Here
  	if (getButtonPress(buttonUp)) adjustMenuItem(false);
  	if (getButtonPress(buttonDown)) adjustMenuItem(true);
    wait(0.1);
  }
  setLEDColor(ledRed);
  playSoundFile("Confirm");
  wait(1);
  setLEDColor(ledGreen);
  return cursorAtMenuItem;
}
int startMenu() {
  return startShowingTheMenu(0);
}
int startMenu(int menuStartsAt) {
  return startShowingTheMenu(menuStartsAt - 1);  // This is a zero offset parameter
}
/*---------------------
RED_FUNCTION
This function draws four objects on the screen.
The coordinates are based on a Cartesian system with the origin at the lower left corner of the display
---------------------*/
void redFunction() {
	int height=25;
	int width=25;
	int radius=25;
	clearDisplay();
  drawRectangle(10,10,width,height);
  wait(1);
  drawRectangle(130,10,width,height);
  wait(1);
  drawRectangle(10,75,width,height);
  wait(1);
  drawCircle(130,75,radius);
  wait(3);
  waitForEnterButton();
}
/*---------------------
ORANGE_FUNCTION
This function draws four objects on the screen.
The sizes of the objects are randomly determined using the random() function.
Press the Left-Right buttons to re-size the objects.
---------------------*/
void orangeFunction() {
	clearDisplay();
	displayBigTextLine(1,"Left=Random");
	displayBigTextLine(3,"Right=Random");
	displayBigTextLine(6,"Enter = Exit");
  wait(3);
  clearDisplay();
  int height=random(50);
  int width=random(50);
  int radius=random(25);
  drawRectangle(10,10,width,height);
  drawRectangle(130,10,width,height);
  drawRectangle(10,75,width,height);
  drawCircle(130,75,radius);
  while(!getButtonPress(buttonEnter)) { // Wait Here
  	if (getButtonPress(buttonLeft) || getButtonPress(buttonRight)) {
		eraseRectangle(10,10,width,height);
		eraseRectangle(130,10,width,height);
		eraseRectangle(10,75,width,height);
		eraseCircle(130,75,radius);
		height=random(100);
		width=random(100);
		radius=random(50);
		drawRectangle(10,10,width,height);
		drawRectangle(130,10,width,height);
		drawRectangle(10,75,width,height);
		drawCircle(130,75,radius);
		playSoundFile("Click");
		wait(.2);
	}
	wait(0.1);
  }
  clearDisplay();
}
/*---------------------
YELLOW_FUNCTION
This function draws four objects on the screen.
The sizes of the objects are randomly determined using the random() function.
Press and hold the Enter Key to exit.
---------------------*/
void yellowFunction() {
  clearDisplay();
  int height=random(50);
  int width=random(50);
  int radius=random(25);
  drawRectangle(10,10,width,height);
  drawRectangle(130,10,width,height);
  drawRectangle(10,75,width,height);
  drawCircle(130,75,radius);
  while(!getButtonPress(buttonEnter)) { // Wait Here
	eraseRectangle(10,10,width,height);
	eraseRectangle(130,10,width,height);
	eraseRectangle(10,75,width,height);
	eraseCircle(130,75,radius);
	height=random(100);
	width=random(100);
	radius=random(50);
	drawRectangle(10,10,width,height);
	drawRectangle(130,10,width,height);
	drawRectangle(10,75,width,height);
	drawCircle(130,75,radius);
	wait(.75);
  }
  clearDisplay();
}
/*---------------------
GREEN_FUNCTION
This function draws a FILLED circle at random positions on the screen.
Press and hold the Enter Key to exit.
Notice how the previous circle needs to be erased.
---------------------*/
void greenFunction() {
  clearDisplay();
  int radius=10;
  int xCoordinate=random(140);
  int yCoordinate=random(100);
  drawCircle(xCoordinate,yCoordinate,radius);
  while(!getButtonPress(buttonEnter)) { // Wait Here
	eraseCircle(xCoordinate,yCoordinate,radius);
	xCoordinate=random(140)+15;
	yCoordinate=random(100)+15;
	fillCircle(xCoordinate,yCoordinate,radius);
	wait(.5);
  }
  clearDisplay();
}
/*---------------------
BLUE_FUNCTION
This function draws open circles at random positions on the screen.
Press and hold the Enter Key to exit.
Notice how the previous circle is not completely erased.
Only the part where the new circle is drawn is erased.
Can you change this to show rectangles instead of circles?
---------------------*/
void blueFunction() {
  clearDisplay();
  int radius=30;
  while(!getButtonPress(buttonEnter)) { // Wait Here
	int xCoordinate=random(140)+15;
	int yCoordinate=random(100)+15;
	eraseCircle(xCoordinate,yCoordinate,radius);
	drawCircle(xCoordinate,yCoordinate,radius);
	wait(.2);
  }
  clearDisplay();
}
/*---------------------
INDIGO_FUNCTION
This function draws open circles in a larger circle centered on the screen.
Press and hold the Enter Key to exit.
Notice how the previous circle is not erased.
Can you change this to erase only where the current circle is being drawn circles?
---------------------*/
void indigoFunction() {
  clearDisplay();
  int i=0;
  int numCircles = 30;
  int radius = 50;
  while(!getButtonPress(buttonEnter)) { // Wait Here
	float remainder = i % numCircles;
	float fraction = remainder / numCircles;
	int degree = fraction * 360;
	int xOffset = cosine(degree) * radius;
	int yOffset = sine(degree) * radius;
	int xCoordinate = 80 + xOffset;
	int yCoordinate = 50 + yOffset;
	drawCircle(xCoordinate, yCoordinate, 20);
	i++;
	wait(.2);
  }
  clearDisplay();
}
/*---------------------
VIOLET_FUNCTION
This function draws lines in a pattern on the screen.
Press and hold the Enter Key to exit.
Use of the FOR loop.
---------------------*/
void violetFunction() {
  clearDisplay();
  coordinate verticalAxis[20];
  coordinate horizontalAxis[20];
  int numberPointsPerLine = 6;
  int distanceBetweenPoints = (int) (125 / numberPointsPerLine);
  for(int i=0; i<numberPointsPerLine; i++) {
	verticalAxis[i].y = i * distanceBetweenPoints;
	horizontalAxis[i].x = i * distanceBetweenPoints;
	}
  while(!getButtonPress(buttonEnter)) { // Wait Here
	int xCoordinate=random(100)+40;
	int yCoordinate=random(50)+30;
    // Each xCoordinate will now be paired with each verticalAxis.y value to form a new point
		// Lines from those points will drawn to each horizontalAxis.x value
	for(int i=0; i<numberPointsPerLine; i++) {
		verticalAxis[i].y = i * distanceBetweenPoints;
  		for(int j=0; j<numberPointsPerLine; j++) {
			drawLine(xCoordinate, verticalAxis[i].y, horizontalAxis[j].x, yCoordinate);
		}
	}
	wait(2);
	clearDisplay();
  }
}
/*-----------------------

Your function can go here

----------------------*/
void ultravioletFunction() {
	clearDisplay();
  // Write your function here...
  //
  //
  //
  clearDisplay();
}
task main()
{
  menu[lastMenuItem] = addMenuItem("Proj1-Variables", RED_FUNCTION);
  menu[lastMenuItem] = addMenuItem("Proj2-Random", ORANGE_FUNCTION);
  menu[lastMenuItem] = addMenuItem("Proj3-Wait", YELLOW_FUNCTION);
  menu[lastMenuItem] = addMenuItem("Proj4-Fill Circ", GREEN_FUNCTION);
  menu[lastMenuItem] = addMenuItem("Proj5-Don't Clr", BLUE_FUNCTION);
  menu[lastMenuItem] = addMenuItem("Proj6-Increment", INDIGO_FUNCTION);
  menu[lastMenuItem] = addMenuItem("Proj7-For Loops", VIOLET_FUNCTION);
  menu[lastMenuItem] = addMenuItem("Proj8-Your Graph", ULTRAVIOLET_FUNCTION);

  while(true) {
	  showMenu();  // The menu is shown
	  selectedItem = startMenu();  // An item is selected

	  switch (menu[selectedItem])  // Now switch based on the menu value of the selected item
	  {
	  case RED_FUNCTION: redFunction(); break;
	  case ORANGE_FUNCTION: orangeFunction(); break;
	  case YELLOW_FUNCTION: yellowFunction(); break;
	  case GREEN_FUNCTION: greenFunction(); break;
	  case BLUE_FUNCTION: blueFunction(); break;
	  case INDIGO_FUNCTION: indigoFunction(); break;
	  case VIOLET_FUNCTION: violetFunction(); break;
	  case ULTRAVIOLET_FUNCTION: ultravioletFunction(); break;
	  }
  }
}