void loop() { delay(250); toggleLED(); if (isButtonPressed()) { if (servo1.attached()) detach(); else attach(); } if (!servo1.attached()) return; int32 average = averageAnalogReads(250); int16 angle1 = (int16)map(average, 0, 4095, MIN_ANGLE1, MAX_ANGLE1); int16 angle2 = (int16)map(average, 0, 4095, MIN_ANGLE2, MAX_ANGLE2); print_buf("pot reading = %d, angle 1 = %d, angle 2 = %d.", average, angle1, angle2); servo1.write(angle1); servo2.write(angle2); int16 read1 = servo1.read(); int16 read2 = servo2.read(); print_buf("write/read angle 1: %d/%d, angle 2: %d/%d", angle1, read1, angle2, read2); ASSERT(abs(angle1 - read1) <= 1); ASSERT(abs(angle2 - read2) <= 1); print_buf("pulse width 1: %d, pulse width 2: %d", servo1.readMicroseconds(), servo2.readMicroseconds()); Serial2.println("\n--------------------------\n"); }
void loop() { float vol_value = analogRead(VOLPIN); char str[5]; int val; if(isDebug == 0 && isServoDone == 0){ //for button buttonState = digitalRead(buttonPin); if (buttonState == HIGH) { //ボタンが押されたらサーボ起動、移動後にFlahsAirの初期化 (TODO 先に初期化するとサーボが動かないバグのため暫定) Serial.println("Btn High!!!!yeah"); //時計周りに 0 度の方向へ、そして戻した後に、FAを起動 moveServoShaft(0, 1); moveServoShaft(90, 1); delay(1000); isDebug = 1; // Initialize SD card. Serial.print(F("\nInitializing SD card...")); if (card.init(SPI_HALF_SPEED, chipSelectPin)) { Serial.print(F("OK")); } else { Serial.print(F("NG")); abort(); } memset(buffer, 0, 0x200); }else { //nothing } int d = Serial.read(); if( d == 'a' ){ sv.attach(SERVO_PIN); } else if( d == 'd' ){ sv.detach(); } else if( d == 'r' ){ Serial.println(sv.read()); Serial.println(sv.readMicroseconds()); } else if( '0' <= d && d <= '9' ){ //時計周りに 0 度の方向へそして戻してFA移動 moveServoShaft(0, 10); moveServoShaft(90, 10); delay(1000); //サーボ終了 isServoDone = 1; // Initialize SD card. Serial.print(F("\nInitializing SD card...")); if (card.init(SPI_HALF_SPEED, chipSelectPin)) { Serial.print(F("OK")); } else { Serial.print(F("NG")); abort(); } memset(buffer, 0, 0x200); } //} }else{ if (card.readExtMemory(1, 1, 0x1000, 0x200, buffer)) { str[0] = buffer[0]; str[1] = buffer[1]; str[2] = buffer[2]; str[3] = buffer[3]; str[4] = 0; val = atoi(str); //0バイトの場合初期化 if(currentByte != 0 && val == 0){ strip.clear(); strip.show(); Serial.println("back to start"); currentByte = 0; } if(val == 0 || val == currentByte){ //do nothing... //Serial.println("get 1..."); }else{ if (vol_value < 800){ //Serial.println("byte changed..."); currentByte = val; showLed3AndHelloServo(val); Serial.println(val); }else{ strip.clear(); strip.show(); } } } } delay(450); }
void loop() { // create local variables to hold a local copies of the channel inputs // these are declared static so that thier values will be retained // between calls to loop. static uint16_t unThrottleIn; static uint16_t unSteeringIn; static uint16_t unAuxIn; // local copy of update flags static uint8_t bUpdateFlags; // check shared update flags to see if any channels have a new signal if(bUpdateFlagsShared) { noInterrupts(); // turn interrupts off quickly while we take local copies of the shared variables // take a local copy of which channels were updated in case we need to use this in the rest of loop bUpdateFlags = bUpdateFlagsShared; // in the current code, the shared values are always populated // so we could copy them without testing the flags // however in the future this could change, so lets // only copy when the flags tell us we can. if(bUpdateFlags & THROTTLE_FLAG) { unThrottleIn = unThrottleInShared; } if(bUpdateFlags & STEERING_FLAG) { unSteeringIn = unSteeringInShared; } if(bUpdateFlags & AUX_FLAG) { unAuxIn = unAuxInShared; } // clear shared copy of updated flags as we have already taken the updates // we still have a local copy if we need to use it in bUpdateFlags bUpdateFlagsShared = 0; interrupts(); // we have local copies of the inputs, so now we can turn interrupts back on // as soon as interrupts are back on, we can no longer use the shared copies, the interrupt // service routines own these and could update them at any time. During the update, the // shared copies may contain junk. Luckily we have our local copies to work with :-) } // do any processing from here onwards // only use the local values unAuxIn, unThrottleIn and unSteeringIn, the shared // variables unAuxInShared, unThrottleInShared, unSteeringInShared are always owned by // the interrupt routines and should not be used in loop // the following code provides simple pass through // this is a good initial test, the Arduino will pass through // receiver input as if the Arduino is not there. // This should be used to confirm the circuit and power // before attempting any custom processing in a project. // we are checking to see if the channel value has changed, this is indicated // by the flags. For the simple pass through we don't really need this check, // but for a more complex project where a new signal requires significant processing // this allows us to only calculate new values when we have new inputs, rather than // on every cycle. if(bUpdateFlags & THROTTLE_FLAG) { if(servoThrottle.readMicroseconds() != unThrottleIn) { servoThrottle.writeMicroseconds(unThrottleIn); } } if(bUpdateFlags & STEERING_FLAG) { if(servoSteering.readMicroseconds() != unSteeringIn) { servoSteering.writeMicroseconds(unSteeringIn); } } if(bUpdateFlags & AUX_FLAG) { if(servoAux.readMicroseconds() != unAuxIn) { servoAux.writeMicroseconds(unAuxIn); } } bUpdateFlags = 0; }
void loop() { myservo.attach(pin); // attaches the servo on pin to the servo object myservo2.attach(pin2); myservo2.write(0); Log("ServoIndex: %d\n", myservo2.read()); Log("ServoIndex in Microseconds: %d\n", myservo2.readMicroseconds()); delay(delayAmount); myservo2.write(180); Log("ServoIndex: %d\n", myservo2.read()); Log("ServoIndex in Microseconds: %d\n", myservo2.readMicroseconds()); /* Tested to work on 9/26 at 4:41pm */ myservo.write(-90); Log("ServoIndex: %d\n", myservo.read()); Log("ServoIndex in Microseconds: %d\n", myservo.readMicroseconds()); delay(delayAmount); myservo.write(0); Log("ServoIndex: %d\n", myservo.read()); Log("ServoIndex in Microseconds: %d\n", myservo.readMicroseconds()); delay(delayAmount); myservo.write(180); Log("ServoIndex: %d\n", myservo.read()); Log("ServoIndex in Microseconds: %d\n", myservo.readMicroseconds()); delay(delayAmount); myservo.write(200); Log("ServoIndex: %d\n", myservo.read()); Log("ServoIndex in Microseconds: %d\n", myservo.readMicroseconds()); delay(delayAmount); /*Tested to work on 9/26 at 4:44pm */ myservo.writeMicroseconds(544); Log("ServoIndex: %d\n", myservo.read()); Log("ServoIndex in Microseconds: %d\n", myservo.readMicroseconds()); delay(delayAmount); myservo.writeMicroseconds(4000); Log("ServoIndex: %d\n", myservo.read()); Log("ServoIndex in Microseconds: %d\n", myservo.readMicroseconds()); delay(delayAmount); Log("ServoIndex: %d\n", myservo.read()); Log("ServoIndex in Microseconds: %d\n", myservo.readMicroseconds()); if (myservo.attached()) { Log("Servo is attached\n"); Log("Servo is detaching\n"); myservo.detach(); if (!myservo.attached()) { Log("Servo is detached\n"); } } else { Log("Servo is not attached\n"); } //for (pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees //{ // in steps of 1 degree // myservo.write(pos); // tell servo to go to position in variable 'pos' // delay(15); // waits 15ms for the servo to reach the position //} //for (pos = 180; pos >= 1; pos -= 1) // goes from 180 degrees to 0 degrees //{ // myservo.write(pos); // tell servo to go to position in variable 'pos' // delay(15); // waits 15ms for the servo to reach the position //} }