Sie sind auf Seite 1von 4

#include <Stepper.

h>
#include <Servo.h>
#include <LiquidCrystal.h>
// Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(13,12,6, 5, 4, 3); // Create an lcd object and assign the pins
(Rs,E,D4,D5,D6,D7)
// create a stepper object on pins 4, 5, 6 and 7
stepper stepper(STEPS, 14, 15, 16, 17); // pins for stepper motor
stepper.step(200);
// number of steps
int color_count[7] = {0 0 0 0 0 0 0}
brown empty

//red

orange yellow green

blue

volatile int button_pause = 0; //turns 1 when interrupt has been triggered


volatile int button_unpause = 0;
int servoPin = 11;
servo servo_motor;

// create servo called

stepper.setSpeed(60);

//stepper motor speed

int initial_angle = 15;


int final_angle = 0;

//angle for servomotor

int count = 0;

Servo_Motor

//counter for null color readings

void setup() {
// set servo pin as output:
servo_motor.attach(servoPin);
// attaches the servo to pin 11
servo_motor.write(initial_angle);
// centering
delay(1000)
lcd.begin(16, 2); // Set the display to 16 columns and 2 rows
pause("Push button to", "start");
ing in order
}

//pause for user to have everyth

void loop()
{
if (button_pause = 1) {
pause("pause", " ");
n program
}

//checks is interrupt button has been pressed


//if button is pressed again resumes mai

attachInterrupt(0,button_pause,rising) //pin2
int color_angle = scan_color(); //determines color and return angle correspondin
g to color
lcd_counter(color_angle);
//displays how many m&m of given color

cup_servo(color_angle); //rotates servo to corresponding color


spin_stepper();
//rotate top disk 45
test(color_angle);
//after 3 empty color readings pauses program
}
void cup_servo (color_angle)
//Rotates servo to color angle
{
final_angle = color_angle;
int direction = final_angle
initial_angle / abs(final_angle-initial_angl
e);
//direction: 1 clockwise, -1 anticlockwise, 0 none
int angle = 0;
switch direction
{
Case '1':
for(angle = initial_angle; angle < final_angle; angle +=
1) {
servo_motor.write(angle);
delay(20);
break;
}
Case '-1':
for(angle = initial_angle; angle > final_angle; angle -=
1) {
servo_motor.write(angle);
delay(20);
break;
}
Case '0':
break;
}
inicial_angle = final_angle

//new inital angle

Void spin_stepper(); {
stepper.step(45);
delay(500)
}

//time for m&m to drop into cup

Void isr_pause() {
button_pause = 1
}

//interrupt routine

Void isr_unpause() {
button_unpause = 1;
}

//interrupt routine

void Pause(String led_write1, String led_write2) {

//writes to lcd then pau

ses. wait for button to resume.


lcd.clear(); // Clear the display
lcd.begin(0,0)
lcd.print(led_write1);
lcd.begin(0,1)
lcd.print(led_write2);
detachInterrupt(0,isr_pause) //pin2
while(button_unpause = 0) {
attachInterrupt(0,isr_unpause,rising) //pin2
delay(1000)
//time for detecting unpause int
errupt
}
button_pause = 0;
button_unpause = 0;
detachInterrupt(0,isr_unpause) //pin2
attachInterrupt(0,isr_pause,rising) //pin2
}
void lcd_counter(int color) {
int center = 0
switch color
{
Case '0':
color_count[0] = color_count[0] +1;
lcd.clear(); // Clear the display
lcd.begin(5,0)
lcd.print("**RED**");
lcd.begin(7,1)
lcd.print(color_count[0]);
break;
Case '30':
color_count[1] = color_count[1] +1;
lcd.clear(); // Clear the display
lcd.begin(4,0)
lcd.print("**ORANGE**");
lcd.begin(8,1)
lcd.print(color_count[1]);
break;
Case '60':
color_count[2] = color_count[2] +1;
lcd.clear(); // Clear the display
lcd.begin(4,0)
lcd.print("**YELLOW**");
lcd.begin(8,1)
lcd.print(color_count[2]);
break;
Case '90':
color_count[3] = color_count[3] +1;
lcd.clear(); // Clear the display
lcd.begin(5,0)
lcd.print("**GREEN**");
lcd.begin(8,1)
lcd.print(color_count[3]);
break;

Case '120':
color_count[4] = color_count[4] +1;
lcd.clear(); // Clear the display
lcd.begin(6,0)
lcd.print("**BLUE**");
lcd.begin(8,1)
lcd.print(color_count[4]);
break;
Case '180':
color_count[5] = color_count[5] +1;
lcd.clear(); // Clear the display
lcd.begin(5,0)
lcd.print("**BROWN**");
lcd.begin(8,1)
lcd.print(color_count[5]);
break;
Case '15':
color_count[6] = color_count[6] +1;
lcd.clear(); // Clear the display
lcd.begin(5,0)
lcd.print("**EMPTY**");
lcd.begin(8,1)
lcd.print(color_count[6]);
break;
}

}
void test (int empty_reading)
{
if (int empty_reading = 15)
{
count = count++;
if (count = 3)
{
count = 0;
pause("pause"
}
}
else
{
count = 0;
}
}

" ");

Das könnte Ihnen auch gefallen