Author Topic: random delay and speed  (Read 712 times)

mr27t

  • Newbie
  • *
  • Posts: 1
random delay and speed
« on: September 05, 2018, 11:33:46 am »
Hello, I am new to code and am using an Arduino Uno  basic starter kit (I'm at project 8 now). I'am attempting to create a cat toy using a digispark attiny85 (digispark 16.5). I found a code online for an Arduino Uno that works awesome with my Arduino Uno, but it will not compile for my Digispark. I have been playing with it for a while and this is where I am at with it so far.


void setup() {
    //P0, P1, and P4 are capable of hardware PWM (analogWrite).
    pinMode(1, OUTPUT);
    pinMode(4, OUTPUT);
   
}

void loop() {
 
 
    analogWrite(1,180);
    delay(1000);
    analogWrite(1,90);
    delay(1000);
    analogWrite(4,180);
    delay(1000);
    analogWrite(4,90);
    delay(1000);
}


The following is what I found online for my Arduino Uno board,,, How do I make it work on my Digispark board?


// Cat Wand - randomly actuates a pair of servos.
//x_servo is on pin 6 and y_servo is on pin 9.
//Randomly chooses a new position for the servos.
//Finds a new position from the old position.

 
#include <Servo.h>
float min_x = 5;
float max_x = 75;
float min_y = 5;
float max_y = 180;
float minimal_movement = 5;
int min_freeze = 5;
int max_freeze = 50;
int random_delay;
float x_position = min_x + (max_x - min_x)/6;
float x_old_position = x_position;
float y_position = min_y + (max_y - min_y)/10;
float y_old_position = y_position;
float x_new_position;
float x_speed;
float y_new_position;
float y_speed;
int movement_time;
Servo x_servo; 
Servo y_servo;
int pos = 0;
void setup() {
  x_servo.attach(6);
  x_servo.write(x_position);//Raises and lowers Wand.
  y_servo.attach(9);
  y_servo.write(y_position);//Moves Wand left and right.     
}
void loop() {
  movement_time = random(5,30);
  random_delay = random(min_freeze, max_freeze);
  x_new_position = random(min_x+minimal_movement, max_x-minimal_movement);
  y_new_position = random(min_y+minimal_movement, max_y-minimal_movement);
    if( (x_new_position > x_old_position) && (abs(x_new_position - x_old_position) < 5 )) {
    x_new_position = x_new_position + minimal_movement;
  }  else if ( (x_new_position < x_old_position) && (abs(x_new_position - x_old_position) < 5 )) {
    x_new_position = x_new_position - minimal_movement;
  }
    if( (y_new_position > y_old_position) && (abs(y_new_position - y_old_position) < 5 )) {
    y_new_position = y_new_position + minimal_movement;
  }  else if ( (y_new_position < y_old_position) && (abs(y_new_position - y_old_position) < 5 )) {
    y_new_position = y_new_position - minimal_movement;
  }
  x_speed = (x_new_position - x_old_position)/movement_time;
  y_speed = (y_new_position - y_old_position)/movement_time; 
  for (pos = 0; pos < movement_time; pos += 1) {
      x_position = x_position + x_speed;
      x_servo.write(x_position);
      y_position = y_position + y_speed;
      y_servo.write(y_position);                   
    delay(15);
  }
  x_old_position = x_new_position;
  y_old_position = y_new_position;
  delay(random_delay);

}