What's wrong with my servo?

My servo needs to be controlled with MIT app inventor, so I wrote some code but it doesn't work. I also have a spectrometer and soil moisture sensor, so is the code from that messing it up? I ran the servo code separately and it worked with my app, only when I combined the code did it stop working. I'm kind of new to Arduino, so my code isn't great. Any tips would be greatly appreciated!! (This is on a Nano by the way.)

#include <Servo.h>
#include <Adafruit_AS7341.h>
Adafruit_AS7341 as7341;

Servo ser;
int poser = 90;
int servo;
char Incoming_Value = 0;
int moisturePin = A0;
int moistureValue = 0;


void setup() {
  Serial.begin(9600);

  as7341.enableLED(true);

  ser.attach(D7);

  while (!Serial) {
    delay(1);
  }
  if (!as7341.begin()) {
    Serial.println("Could not find AS7341");
    while (1) {
      delay(10);
    }
  }
  as7341.setATIME(100);
  as7341.setASTEP(999);
  as7341.setGain(AS7341_GAIN_256X);
}

void loop() {
  moistureValue = analogRead(moisturePin);
  as7341.enableLED(true);

  while (Serial.available())
  {
    servo = Serial.read();
    poser = servo;
    ser.write(poser);
    delay(15);
  }

  if (!as7341.readAllChannels()) {
    Serial.println("Error reading all channels!");
    return;
  }

  Serial.println(moistureValue);
  Serial.println("|");
  specValue();
  Serial.println("|");
  Serial.println();
  delay(1000);
}

void specValue() {

  Serial.print("blue 445nm: ");
  Serial.print(as7341.getChannel(AS7341_CHANNEL_445nm_F2));
  Serial.print("red 630nm: ");
  Serial.print(as7341.getChannel(AS7341_CHANNEL_630nm_F7));
  Serial.print("yellow 590nm: ");
  Serial.print(as7341.getChannel(AS7341_CHANNEL_590nm_F6));
}
ser.attach(D7);

Does this compile ?

What is this supposed to do? The result of Serial.read() is the value of a single ASCII character.

Please take a look at the Serial Input Basics tutorial, posted elsewhere on the forum.

You may have a power issue. Power your project with a good, stiff external power supply and not batteries or USB power. The clue is that the code works with any one device but not all, The code doesn't just "break", HARDWARE breaks. You run low on mA you get weirdness as devices go in an out of limit and the power level moves as devices demand more or less current.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.