Skip to content

Commit

Permalink
Made code following exact format
Browse files Browse the repository at this point in the history
Code of conduct:

if
{
...
}
else 
{
....
}

/*-----------
* comment
------------*/
  • Loading branch information
karinakozarova authored Mar 27, 2017
1 parent a78e52a commit 1759cc2
Showing 1 changed file with 69 additions and 36 deletions.
105 changes: 69 additions & 36 deletions start.ino
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ int pos = 0;
void joystick_position();

void setup() {
/*-------------------------------------------------------------
* Serial.begin sets the data rate in bits per second (baud)
* for serial data transmission.115200 is the fastest.
-------------------------------------------------------------*/
Serial.begin(115200);

//setting uo the pins
//setting up the pins
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
pinMode(bPin, INPUT);
Expand All @@ -45,53 +49,66 @@ void setup() {
//attaching the servo
myservo.attach(6);

Serial.println("Program started");
Serial.println("Program started!");
}

void loop() {
joystick_position();
}

void joystick_position(){
//control the joystick
/* ---------------------------------------------------------------
* Depending on the joystick position the pen is being moved.
* Also, you can change the degree of the pen via a pottentiometer
* and the servo_move_up and servo_move_down functions
----------------------------------------------------------------*/

//x - left or right // y - forward or backward //b - diagonal
xValue= analogRead(xPin);
yValue = analogRead(yPin);
bValue = analogRead(bPin);
Serial.print(bValue);

//it moves up and down
//chenge the pottentiometer of the arduino and it changes the angle of the pen
sValue = map(bValue,0,1023,85,10);
//change the pottentiometer of the arduino and it changes the angle of the pen
sValue = map(bValue,0,1023,85,10);

myservo.write(sValue);
// move the pen according to the x and y cordinates
/*-------------------------------------------------------
* In the following if-else constructions,
* the constants are cordinates.
* The max is 1023 to both x and y values.
* The middle is with cordinates (512;512).
* If we use it, the joystick will be too precise
* and the error percentage will up.
* That's why we use 525 as middle.
* When yDir and xDir are 1,we move the pen diagonally
-------------------------------------------------------*/

if((xValue>490 && xValue <525 ) && (yValue>490 && yValue<525))
{
stepStop();
}
else if((xValue>490 && xValue <525 )){

else if((xValue>490 && xValue <525 ))
{
if(yValue > 525)
{
//push the y value and the pen moves to the right
//push the y value => pen moves to the right
yValue = map(yValue,525,1023,20,1);
yDir = 1;
}
else
{
//push the y value and it the pen to the left
//push the y value => pen to the left
yValue = map(yValue,490,0,20,1);
yDir = 0;

}

digitalWrite(STEP_MOTOR_PLATE, LOW);
stepMeHigh(STEP_MOTOR_PEN,yDir,yValue);

}
else if((yValue>490 && yValue <525 ))
{

}
else if((yValue>490 && yValue <525 ))
{
if(xValue > 525)
{
//push the y value and it moves the plate forward => the pen moves backward
Expand All @@ -107,11 +124,16 @@ void joystick_position(){
digitalWrite(STEP_MOTOR_PEN, LOW);
stepMeHigh(STEP_MOTOR_PLATE,xDir,xValue);

}
else
{
//push the y value and it moves the position of the plate backward => the pen moves forward
if(xValue > 525)
}
else
{
/*------------------------------------------------
* By changing the y value( by moving the joystick),
* we move the position of the plate backward
* ===> the pen moves forward.
------------------------------------------------*/

if(xValue > 525) //525 - not pushed joystick
{
xValue = map(xValue,525,1023,20,1);
xDir = 1;
Expand All @@ -122,7 +144,8 @@ void joystick_position(){
xDir = 0;
}

if(yValue > 525){
if(yValue > 525) //525 - not pushed joystick
{
yValue = map(yValue,525,1023,20,1);
yDir = 1;
}
Expand All @@ -135,44 +158,54 @@ void joystick_position(){
stepMeHigh(STEP_MOTOR_PLATE,xDir,xValue);
stepMeHigh(STEP_MOTOR_PEN,yDir,yValue);
}
}
}

void servo_move_down(){

//pen goes down
//change the angle of the pen from 90 to 0
for (pos = 90; pos >= 0; pos -= 1) {
myservo.write(pos);
/*----------------------------------------
* Via the servo and the pottentiometer,
* we move the pen downward
----------------------------------------*/

for (pos = 90; pos >= 0; pos -= 1) //change the angle of the pen from 90 to 0
{
myservo.write(pos); //command to rotate the servo to the specified angle
}

}

void servo_move_up(){

//pen goes up
//change the angle of the pen from 0 to 90
for(angle = 0; angle < 90; angle += 1)
/*----------------------------------------
* Via the servo and the pottentiometer,
* we move the pen upward
----------------------------------------*/
for(angle = 0; angle < 90; angle += 1) //change the angle of the pen from 0 to 90
{
servo_test.write(angle);
servo_test.write(angle); //command to rotate the servo to the specified angle
}

}

void stepMeHigh(int motor, int dir, int del) {

//1 - motor pen, 2 - motor plate // 0 - left, 1 - right //
/*-----------------------------------------------
* Function parametres:
* motor : 1 - motor pen, 2 - motor plate
* dir : 0 - left, 1 - right
-----------------------------------------------*/

digitalWrite(DIR_MOTOR_PLATE, dir);
digitalWrite(DIR_MOTOR_PEN, dir);
digitalWrite(motor, HIGH);
delay(del);
digitalWrite(motor, LOW);

}
}

void stepStop(){

//nulls the step
digitalWrite(STEP_MOTOR_PEN, LOW);
digitalWrite(STEP_MOTOR_PLATE, LOW);

}

0 comments on commit 1759cc2

Please sign in to comment.