Photoresistor controlled servo not responding

I have an animation that plays when the value read by the photoresistor on A0, drops below a threshold. Im using a continuous rotation "servo" ( 0=anticlockwise, 90=stop, 180=clockwise). The gif plays fine and the photoresistor values are coming in just fine too but the servo just wont respond. I checked the power supply and the connections. Im running standard firmata on an arduino mega. The motor works just fine with other code. Is there something basic that I'm missing here?

here is the code

Animation animation1;
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
void setup() {
  size(640, 360);
  background(255, 204, 0);
  frameRate(24);
  animation1 = new Animation("BL ", 38);

  println(Arduino.list());
  arduino = new Arduino(this, Arduino.list()[0], 57600);
   for (int i = 0; i <= 52; i++)
    arduino.pinMode(i, Arduino.INPUT);
     arduino.pinMode(8, Arduino.SERVO);
}

void draw() { 
int pres=arduino.analogRead(0);
  println(pres);


  if (pres<140) {
    arduino.servoWrite(8, 90);
    delay(500);
    animation1.display(0, 0);
  } 
  else {
    background(0);
    arduino.servoWrite(8,180);
    delay(500);
  }


}




class Animation {
  PImage[] images;
  int imageCount;
  int frame;

  Animation(String imagePrefix, int count) {
    imageCount = count;
    images = new PImage[imageCount];

    for (int i = 0; i < imageCount-1; i++) {
      String filename = imagePrefix + nf(i) + ".jpg";
      images[i] = loadImage(filename);
    }
  }

  void display(float xpos, float ypos) {
    frame = (frame+1) % imageCount;
    if(frame<31)
    image(images[frame], xpos, ypos);
else
frame=0;
}

  int getWidth() {
    return images[0].width;
  }
}

Answers

  • @pratheek_ir

    I will suggest creating a parallel sketch where you interact with the servo by itself.

    Try something like the code below. Please notice it is not tested. If you don't get the servo running, you should also post in the arduino community if you haven't done so.

    Kf

    //INSTUCTIONS: Srvo starts with no motion. 
    //Click events trigger the servo to advanced 
    //through the following states in order: 0 90 180 90
    // and then back to 0 to start the cycle again. 
    import processing.serial.*;
    import cc.arduino.*;
    Arduino arduino;
    
    final int SERVOPIN=8;
    
    
    //Three states: 0 do nothing, positive and negative
    int[] states={0, 90, 180, 90};
    int cstate=states[0];  //current state
    boolean triggerNow=false;
    
    
    
    void setup() {
      size(640, 360);
      textAlign(CENTER,CENTER);
      textSize(28);
      fill(255);
      frameRate(24);
    
      println(Arduino.list());
      arduino = new Arduino(this, Arduino.list()[0], 57600);
    
      arduino.pinMode(SERVOPIN, Arduino.SERVO);
    }
    
    void draw() { 
    
      background(0);
      text("Current state: " + states[cstate],width/2,height/2);
    
      if (triggerNow==true) {
        arduino.servoWrite(SERVOPIN, states[cstate]);
        delay(20);
        triggerNow=false;
      }
    }
    
    
    void mouseReleased() {
    
      //Increases states and contrain values between array boundaries
    
      cstate=(cstate+1)%states.length;  
      triggerNow=true;
    }
    
Sign In or Register to comment.