2

I am trying to run a program which works as follows:

An LDR is integrated in a box. A block is placed on top. This box is able to be rotated by a servo. The program is supposed to make the box turn until the block slides and lifts off from the LDR.

At that point the servo angle is stored and used to calculate the friction coefficient. Finally the answer to this calculation is printed on an lcd and the program repeats by first returning the box to the original state. The entire programs runs on Arduino and an Arduino Lotus. Why doesn't the servo rotate?

//servo 
#include <Servo.h> 

Servo myservo; 
int previous_angle = 0; 
int angle = 0; 
    
//lightsensor 
int lightsensor = 0;     
int LIGHT = 800;     
int DARK = 940; 
              
//LED screen     
#include <Adafruit_GFX.h>     
#include <Adafruit_SSD1306.h> 
    
#define OLED_RESET 4     
Adafruit_SSD1306 display(OLED_RESET); 
              
void setup() {    
  Serial.begin(9600);     
  myservo.attach(6); 
            
  pinMode(lightsensor, INPUT);               

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); 
    
  delay(100);                 
} 
              
void loop() { 
  int i = previous_angle;     
  int lightStatus = analogRead(lightsensor); 
    
  if (lightStatus >= DARK) {      
    delay(300); 

    while (int i = previous_angle) {
      i<=90;
      i = i+1;
      myservo.write(i);     
      delay(45); 
    };      
  } else if (lightStatus < DARK) {    
    for (int i = previous_angle; i>0;) {     
      delay(300);     
      int angle = tan(i);         
      display.clearDisplay();     
      display.setTextSize(1.5);     
      display.setTextColor(WHITE);     
      display.setCursor(0,0);     
      display.println(angle);     
      display.display();     
      delay(300);  
              
      myservo.write(0);     
      delay(45);     
    };     
  }     
}
2
  • 1
    In the future, please format your source code using the {} on this site. As it was it was very difficult to follow your code. Also, please include a schematic of how your motor is connected. – jwh20 Jan 11 at 13:38
  • 1
    Question: Have you been able to make your motor run with just a simple program? It's often helpful to start out simple and add things one step at a time. – jwh20 Jan 11 at 13:40
2

For starters: this bit is a strange mixture of for and while syntax:

while (int i = previous_angle) {
  i <= 90; 
  i = i + 1; 
  myservo.write(i);
  delay(45); 
};  

It will compile, but probably not do what you want it to do. It should probably be:

for (int i=previous_angle; i<=90; i=i+1) {
  myservo.write(i);
  delay(45); 
}

Also, previous_angle is never changed in your code after initialisation; it is always 0.

That also means that the for loop below will not do what you want (and that loop also doesn't increment i anywhere):

for (int i = previous_angle; i>0;)

There may be more; make the suggested changes first and see what happens.

3
  • If I change it I get an error when verifying the code 'expected ')' before ';' token' – user71605 Jan 11 at 14:00
  • I blame coffee underflow 8-). Answer edited. – ocrdu Jan 11 at 14:00
  • Changing to a "for" does resolve the error, I will check whether the code also properly as needed thankyou – user71605 Jan 11 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.