gpt4 book ai didi

c++ - 在编码中分配 Helm 机很困难

转载 作者:行者123 更新时间:2023-11-30 19:37:26 27 4
gpt4 key购买 nike

所以我几天前刚刚得到了 arudino,用于一个特定的项目,其中有四个伺服器,点击我的 3ds 上的八个按钮。我对编码完全陌生,需要帮助在我得到的编码中分配我的伺服系统和光电管。每次我尝试给出伺服系统所在位置的引脚编号时,我都会在其他地方收到错误。我已经尝试让它工作好几天了,所以任何建议都会很可爱!

#include <Servo.h>

// Define the slots (says digital ~PWM) on the Arduino where various parts of the machine will go
/* Diyode CodeShield Constants */
#define SERVO1 2 ); Servo attach ;2
#define SERVO2 3 ); Servo attach ;-3
#define SERVO3 4 ); Servo attach ;4
#define SERVO4 5 ); Servo attach ; -5
#define PIEZO 6 ); attach ; -6
#define BUTTON 9 ); attach 9

// Define a couple constants of length of time in milliseconds
#define CHECK_SCREEN_DELAY 150
#define WALKING_BUTTONPRESS_DURATION 3300

// Define a couple cases, don't mess with these
#define STARTING 1
#define WALKING 2
#define WATCHING 3
#define FOUND 4

//servo1 presses left and up
Servo servo1;
//servo2 presses right and down
;Servo servo2;
//servo3 presses b and y
Servo servo3;
//servo4 presses a and x
Servo servo4;

// Defines the starting positions of the servos.
// These will need to be adjusted during setup.
int servo1Flat = 108;
int servo1Deflect = 18;
int servo2Flat = 95;
int servo2Deflect = 20;
int servo3Flat = 83;
int servo3Deflect = 15;
int servo4Flat = 65;
int servo4Deflect = 15;
String name = "A";

//Define the photoreceptor.
int photocellPin = 1; // the cell and 10K pulldown are connected to a1
int photocellReading; // the analog reading from the sensor divider

//Define counts so the program knows when to collect, hatch, or deposit eggs.
unsigned int mode = 1;
unsigned int egg_count = 0;
unsigned int clutch_count = 0;
unsigned int box_count = 0;
unsigned int bike_count = 0;

//This is the level of light that the program uses to make decisions on whether to collect
//an egg or to hatch one.
unsigned char dark_threshold = 400;

//Setting up and writing in th servos. Don't mess with these.
void setup() {
Serial.begin (115200);
Serial.print("Start Unit ");
Serial.println(name);

servo1.attach(SERVO1);
servo1.write(servo1Flat);

servo2.attach(SERVO2);
servo2.write(servo2Flat);

servo3.attach(SERVO3);
servo3.write(servo3Flat);

servo4.attach(SERVO4);
servo4.write(servo4Flat);
}
void loop() {

switch (mode) {
case STARTING:
watchButton();
break;
case WALKING:
doWalking();
break;
case WATCHING:
break;
case FOUND:
watchButton();
break;
}
}
void watchButton() {
if (digitalRead(BUTTON) == HIGH) {
mode = WALKING;
}
}

int is_between(long val, long lower, long upper)
{
return (val > lower && val < upper);
}

//Define the actions to take when you've hatched all your eggs,
//i.e. go to the PC, deposit the eggs, come back outside and ask for
//another egg.
void tapScreen() {
// Go east and get off bike
if(clutch_count < 6)
{
servo2.write(servo2Flat + servo2Deflect);
delay(4000);
servo2.write(servo2Flat);
delay(100);
servo3.write(servo3Flat - 5*servo3Deflect);
delay(500);
servo3.write(servo3Flat);
delay(600);
//Walk into daycare center and over to pc
servo1.write(servo1Flat + servo1Deflect);
delay(300);
servo1.write(servo1Flat);
delay(600);
servo1.write(servo1Flat + servo1Deflect);
delay(200);
servo1.write(servo1Flat);
delay(600);
servo1.write(servo1Flat + servo1Deflect);
delay(200);
servo1.write(servo1Flat);
delay(600);
servo1.write(servo1Flat + servo1Deflect);
delay(200);
servo1.write(servo1Flat);
delay(600);
servo1.write(servo1Flat + servo1Deflect);
delay(200);
servo1.write(servo1Flat);
delay(600);
servo1.write(servo1Flat - 2*servo1Deflect);
delay(3500);
servo1.write(servo1Flat);
delay(600);
servo2.write(servo2Flat + servo2Deflect);
delay(300);
servo2.write(servo2Flat);
delay(600);
servo2.write(servo2Flat + servo2Deflect);
delay(200);
servo2.write(servo2Flat);
delay(600);
servo1.write(servo1Flat - 2*servo1Deflect);
delay(200);
servo1.write(servo1Flat);
delay(600);
//Open PC, navigate to eggs
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(2200);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo2.write(servo2Flat - 4*servo2Deflect);
delay(300);
servo2.write(servo2Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(2300);
//Deposit 5 eggs
servo2.write(servo2Flat + servo2Deflect);
delay(300);
servo2.write(servo2Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo2.write(servo2Flat + servo2Deflect);
delay(200);
servo2.write(servo2Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo2.write(servo2Flat + servo2Deflect);
delay(200);
servo2.write(servo2Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo2.write(servo2Flat + servo2Deflect);
delay(200);
servo2.write(servo2Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo2.write(servo2Flat + servo2Deflect);
delay(200);
servo2.write(servo2Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
//Exit out of PC and walk back outside, get on bike, ask for egg
servo4.write(servo4Flat + 4*servo4Deflect);
delay(400);
servo4.write(servo4Flat);
delay(2500);
servo1.write(servo1Flat + servo1Deflect);
delay(300);
servo1.write(servo1Flat);
delay(600);
servo1.write(servo1Flat + servo1Deflect);
delay(250);
servo1.write(servo1Flat);
delay(600);
servo2.write(servo2Flat - 4*servo2Deflect);
delay(3000);
servo2.write(servo2Flat);
delay(1200);
servo2.write(servo2Flat - 4*servo2Deflect);
delay(300);
servo2.write(servo2Flat);
delay(300);
servo3.write(servo3Flat - 5*servo3Deflect);
delay(500);
servo3.write(servo3Flat);
delay(600);
servo2.write(servo2Flat + servo2Deflect);
delay(500);
servo2.write(servo2Flat);
delay(400);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(400);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(400);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(400);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(400);
clutch_count++;
}
//When the box is full, the program follows these commands, which are
//identical to the above except it changes the box and resets the clutch count.
else
{
servo2.write(servo2Flat + servo2Deflect);
delay(4000);
servo2.write(servo2Flat);
delay(100);
servo3.write(servo3Flat - 5*servo3Deflect);
delay(500);
servo3.write(servo3Flat);
delay(600);
//Walk into daycare center and over to pc
servo1.write(servo1Flat + servo1Deflect);
delay(300);
servo1.write(servo1Flat);
delay(600);
servo1.write(servo1Flat + servo1Deflect);
delay(200);
servo1.write(servo1Flat);
delay(600);
servo1.write(servo1Flat + servo1Deflect);
delay(200);
servo1.write(servo1Flat);
delay(600);
servo1.write(servo1Flat + servo1Deflect);
delay(200);
servo1.write(servo1Flat);
delay(600);
servo1.write(servo1Flat + servo1Deflect);
delay(200);
servo1.write(servo1Flat);
delay(600);
servo1.write(servo1Flat - 2*servo1Deflect);
delay(3500);
servo1.write(servo1Flat);
delay(600);
servo2.write(servo2Flat + servo2Deflect);
delay(300);
servo2.write(servo2Flat);
delay(600);
servo2.write(servo2Flat + servo2Deflect);
delay(200);
servo2.write(servo2Flat);
delay(600);
servo1.write(servo1Flat - 2*servo1Deflect);
delay(300);
servo1.write(servo1Flat);
delay(600);
//Open PC, navigate to eggs
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(2200);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo2.write(servo2Flat - 4*servo2Deflect);
delay(300);
servo2.write(servo2Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(2300);
//Change box, deposit 5 eggs
servo2.write(servo2Flat + servo2Deflect);
delay(300);
servo2.write(servo2Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(900);
servo2.write(servo2Flat + servo2Deflect);
delay(300);
servo2.write(servo2Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo2.write(servo2Flat + servo2Deflect);
delay(200);
servo2.write(servo2Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo2.write(servo2Flat + servo2Deflect);
delay(200);
servo2.write(servo2Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo2.write(servo2Flat + servo2Deflect);
delay(200);
servo2.write(servo2Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo2.write(servo2Flat + servo2Deflect);
delay(200);
servo2.write(servo2Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(700);
//Exit out of PC and walk back outside, get on bike, ask for egg
servo4.write(servo4Flat + 4*servo4Deflect);
delay(250);
servo4.write(servo4Flat);
delay(2500);
servo1.write(servo1Flat + servo1Deflect);
delay(300);
servo1.write(servo1Flat);
delay(600);
servo1.write(servo1Flat + servo1Deflect);
delay(250);
servo1.write(servo1Flat);
delay(600);
servo2.write(servo2Flat - 4*servo2Deflect);
delay(3000);
servo2.write(servo2Flat);
delay(1200);
servo2.write(servo2Flat - 4*servo2Deflect);
delay(300);
servo2.write(servo2Flat);
delay(300);
servo3.write(servo3Flat - 5*servo3Deflect);
delay(500);
servo3.write(servo3Flat);
delay(600);
servo2.write(servo2Flat + servo2Deflect);
delay(500);
servo2.write(servo2Flat);
delay(400);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(400);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(400);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(400);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(400);
clutch_count = 1;
box_count++;
Serial.print("***BOXES FILLED***: ");
Serial.println(box_count);
}
Serial.print("Clutches: ");
Serial.println(clutch_count);
bike_count = 0;
}

//Define the beep that will occur if the program messes up, which
//happens when the program rides back and forth on the bike 50 times without
//the screen darkening. The screen should always darken before riding back and
//forth 50 times due to an egg being accepted from the breeder or hatched.
void beep() {
analogWrite( PIEZO, 35);
delay(400);
digitalWrite(PIEZO, LOW);
}

void doWalking()
{
while(1)
{
photocellReading = analogRead(photocellPin);
//As I mentioned above the beep definition, when the program messes up,
//i.e. your character ends up in a place they shouldn't be and cannot
//collecting eggs, the program follows these commands. It will press "b" a bunch
//of times, then walk around a bit in case you are in the breeder's house,
//and then will use Talonflame to fly to in front of the Pokemon center. Then
//you will deposit any eggs/pokemon you might have (besides Talonflame), you will
// switch to a new box, and start over.
if(bike_count >= 50)
{
beep();
servo3.write(servo3Flat + servo3Deflect);
delay(400);
servo3.write(servo3Flat);
delay(600);
servo3.write(servo3Flat + servo3Deflect);
delay(400);
servo3.write(servo3Flat);
delay(600);
servo3.write(servo3Flat + servo3Deflect);
delay(400);
servo3.write(servo3Flat);
delay(600);
servo3.write(servo3Flat + servo3Deflect);
delay(400);
servo3.write(servo3Flat);
delay(600);
servo3.write(servo3Flat + servo3Deflect);
delay(400);
servo3.write(servo3Flat);
delay(600);
servo3.write(servo3Flat + servo3Deflect);
delay(400);
servo3.write(servo3Flat);
delay(600);
servo3.write(servo3Flat + servo3Deflect);
delay(400);
servo3.write(servo3Flat);
delay(600);
servo3.write(servo3Flat + servo3Deflect);
delay(400);
servo3.write(servo3Flat);
delay(600);
servo3.write(servo3Flat + servo3Deflect);
delay(400);
servo3.write(servo3Flat);
delay(600);
servo3.write(servo3Flat + servo3Deflect);
delay(400);
servo3.write(servo3Flat);
delay(600);
servo3.write(servo3Flat + servo3Deflect);
delay(400);
servo3.write(servo3Flat);
delay(600);
servo3.write(servo3Flat + servo3Deflect);
delay(400);
servo3.write(servo3Flat);
delay(600);
servo3.write(servo3Flat + servo3Deflect);
delay(400);
servo3.write(servo3Flat);
delay(600);
servo3.write(servo3Flat + servo3Deflect);
delay(400);
servo3.write(servo3Flat);
delay(600);
servo1.write(servo1Flat + servo1Deflect);
delay(250);
servo1.write(servo1Flat);
delay(600);
servo1.write(servo1Flat + servo1Deflect);
delay(250);
servo1.write(servo1Flat);
delay(600);
servo2.write(servo2Flat - 4*servo2Deflect);
delay(4000);
servo2.write(servo2Flat);
delay(200);
servo4.write(servo4Flat + 4*servo4Deflect);
delay(250);
servo4.write(servo4Flat);
delay(2500);
servo1.write(servo1Flat + servo1Deflect);
delay(250);
servo1.write(servo1Flat);
delay(600);
servo1.write(servo1Flat + servo1Deflect);
delay(250);
servo1.write(servo1Flat);
delay(600);
servo2.write(servo2Flat - 4*servo2Deflect);
delay(4000);
servo2.write(servo2Flat);
delay(200);
servo4.write(servo4Flat + 4*servo4Deflect);
delay(250);
servo4.write(servo4Flat);
delay(2500);
servo4.write(servo4Flat - servo4Deflect);
delay(250);
servo4.write(servo4Flat);
delay(2000);
servo4.write(servo4Flat + 4*servo4Deflect);
delay(250);
servo4.write(servo4Flat);
delay(5000);
servo4.write(servo4Flat + 4*servo4Deflect);
delay(250);
servo4.write(servo4Flat);
delay(2000);
servo4.write(servo4Flat - servo4Deflect);
delay(250);
servo4.write(servo4Flat);
delay(2000);
servo4.write(servo4Flat - servo4Deflect);
delay(250);
servo4.write(servo4Flat);
delay(1000);
servo2.write(servo2Flat - 4*servo2Deflect);
delay(300);
servo2.write(servo2Flat);
delay(1000);
servo4.write(servo4Flat - servo4Deflect);
delay(250);
servo4.write(servo4Flat);
delay(1000);
servo4.write(servo4Flat - servo4Deflect);
delay(250);
servo4.write(servo4Flat);
delay(1500);
servo4.write(servo4Flat - servo4Deflect);
delay(250);
servo4.write(servo4Flat);
delay(13000);
servo3.write(servo3Flat - 5*servo3Deflect);
delay(500);
servo3.write(servo3Flat);
delay(600);
clutch_count = 6;
beep();
tapScreen();
beep();
egg_count = 0;
bike_count = 0;

}

//If the screen is bright after asking for an egg and you have fewer than 5 eggs,
//the program bikes back and forth and asks for an egg.
else if(photocellReading > 350 && egg_count <5)
{
servo1.write(servo1Flat + servo1Deflect);
delay(WALKING_BUTTONPRESS_DURATION);
servo1.write(servo1Flat);
delay(50);
servo2.write(servo2Flat + servo2Deflect);
delay(3200);
servo2.write(servo2Flat);
delay(50);
servo1.write(servo1Flat + servo1Deflect);
delay(WALKING_BUTTONPRESS_DURATION);
servo1.write(servo1Flat);
delay(50);
servo2.write(servo2Flat + servo2Deflect);
delay(3500);
servo2.write(servo2Flat);
delay(100);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(400);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(400);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(400);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(400);
bike_count++;
}
//Deposits eggs if you've hatched them all.
else if(egg_count >= 10)
{
tapScreen();
egg_count = 0;
}

//Focuses on hatching eggs after you've collected 5 eggs.
else if(photocellReading > 350 && egg_count >=5)
{
servo1.write(servo1Flat + servo1Deflect);
delay(3300);
servo1.write(servo1Flat);
delay(50);
servo2.write(servo2Flat + servo2Deflect);
delay(3300);
servo2.write(servo2Flat);
delay(50);
bike_count++;
}

//Hatches an egg when the screen goes dark after you've collected 5 eggs.
else if(photocellReading < 350 && egg_count >=5)
{
servo3.write(servo3Flat + servo3Deflect);
delay(400);
servo3.write(servo3Flat);
delay(19000);
servo3.write(servo3Flat + servo3Deflect);
delay(400);
servo3.write(servo3Flat);
delay(3000);
egg_count++;
Serial.print("Eggs hatched: ");
Serial.println(egg_count - 5);
servo1.write(servo1Flat + servo1Deflect);
delay(800);
servo1.write(servo1Flat);
bike_count = 0;
}

//If you've collected fewer than five eggs and the screen is still dark after asking
//for an egg, the program will take the egg.
else if(photocellReading < 350 && egg_count < 5)
{
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(400);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(400);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(3000);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(400);
servo4.write(servo4Flat - servo4Deflect);
delay(200);
servo4.write(servo4Flat);
delay(400);
egg_count++;
Serial.print("Eggs: ");
Serial.println(egg_count);
bike_count = 0;
}

int curr = analogRead(photocellPin);
}
}

最佳答案

我不知道你想在这里实现什么目标:

#define SERVO1 2 ); Servo attach ;2
#define SERVO2 3 ); Servo attach ;-3
#define SERVO3 4 ); Servo attach ;4
#define SERVO4 5 ); Servo attach ; -5
#define PIEZO 6 ); attach ; -6
#define BUTTON 9 ); attach 9

但是你得到的是,在 SERVO1 的每个地方出现在代码中,则替换为 2 ); Servo attach ;2字符串。所以servo1.attach(SERVO1);变成servo1.attach(2 ); Servo attach ;2);这是无稽之谈。只需进行类似 #define SERVO1 2 的定义即可你应该没问题。

关于c++ - 在编码中分配 Helm 机很困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39866623/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com