Skip to content

Instantly share code, notes, and snippets.

@micromeeeter
Created January 21, 2018 11:49
Show Gist options
  • Save micromeeeter/b574f69aa3551b681d8b511e75358ae3 to your computer and use it in GitHub Desktop.
Save micromeeeter/b574f69aa3551b681d8b511e75358ae3 to your computer and use it in GitHub Desktop.
SFC2017年度秋学期 電子工作 最終課題 Wild Computing / 自然の中で機能するシステム-「世界の繁栄のために」
#include <Servo.h>
//motor A connected between A01 and A02
//motor B connected between B01 and B02
int STBY = 9; //standby
//Motor A
const int PWMA = 3; //Speed control
const int AIN1 = 4; //Direction
const int AIN2 = 5; //Direction
//Motor B
const int PWMB = 6; //Speed control
const int BIN1 = 7; //Direction
const int BIN2 = 8; //Direction
//surbo
Servo myServo;
const int SERPIN = 11;
int theta;
void setup(){
pinMode(STBY, OUTPUT);
pinMode(PWMA, OUTPUT);
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(PWMB, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(BIN2, OUTPUT);
myServo.attach(SERPIN);
}
void loop(){
for(int i = 0; i < 16; i++){
int mode = int(random(0, 5));
int sp = 64 * int(random(1, 3));
moveMode(mode, sp);
delay(500);
}
stop();
delay(1000);
for(theta = 90; theta >= 50; theta--){
myServo.write(theta);
delay(25);
}
for(theta = 50; theta <= 90; theta++){
myServo.write(theta);
delay(25);
}
delay(1000);
}
void move(int motor, int speed, int direction){
//Move specific motor at speed and direction
//motor: 0 for B 1 for A
//speed: 0 is off, and 255 is full speed
//direction: 0 clockwise, 1 counter-clockwise
digitalWrite(STBY, HIGH); //disable standby
boolean inPin1 = LOW;
boolean inPin2 = HIGH;
if(direction == 1){
inPin1 = HIGH;
inPin2 = LOW;
}
if(motor == 1){
digitalWrite(AIN1, inPin1);
digitalWrite(AIN2, inPin2);
analogWrite(PWMA, speed);
}else{
digitalWrite(BIN1, inPin1);
digitalWrite(BIN2, inPin2);
analogWrite(PWMB, speed);
}
}
void moveMode(int mode, int sp){
switch(mode){
case 0:
move(1, sp, 1);
move(2, sp, 0);
break;
case 1:
move(1, sp * 0.5, 1);
move(2, sp, 0);
break;
case 2:
move(1, sp, 0);
move(2, sp, 0);
break;
case 3:
move(1, sp, 0);
move(2, sp * 0.5, 1);
break;
case 4:
move(1, sp, 0);
move(2, sp, 1);
break;
}
}
void stop(){
//enable standby
digitalWrite(STBY, LOW);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment