Neuling bräuchte einen kleinen Rat!

Hallo zusammen,
mein Name ist Nils und ich komme aus dem schönen NRW.

Ich bin ein absoluter Neuling!
Habt ihr vielleicht ein paar Tipps für den Anfang?

Gerade habe ich folgendes Projekt angefangen: intellar.ca - Animated eye OLED
leider habe ich das Problem das ich die Animation nicht ans laufen bekomme da ein Auslöser von Nöten ist den ich aber raus haben möchte... leider raffe ich das mit dem loop nicht ganz bzw. weiss ich nicht was ich austauschen/einsetzen muss.
vielleicht hat einer von euch einen kleinen Tipp für mich.

Würde mich über jede Unterstützung freuen!

Evtl. erst mal klein anfangen mit den Beispielen in der IDE, um die Grundzusammenhänge zu erlernen. Dann kannst Du komplexere Sketche auch besser verstehen.

Gruß Tommy

Es würde effektiver sein, wenn Du den Code einfach posten würdest.
Was auch immer das für eine Seite ist, aber die hat hier soviele Blocker ausgelöst, da komm ich nicht mit zu rande....

Programm
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>


#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library. 
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL), ...
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

//Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &SPI);
  //uint8_t w, uint8_t h, SPIClass *spi_ptr,
    //                               int8_t dc_pin, int8_t rst_pin, int8_t cs_pin,
      //                             uint32_t bitrate)

//reference state
int ref_eye_height = 40;
int ref_eye_width = 40;
int ref_space_between_eye = 10;
int ref_corner_radius = 10;
//current state of the eyes
int left_eye_height = ref_eye_height;
int left_eye_width = ref_eye_width;
int left_eye_x = 32;
int left_eye_y = 32;
int right_eye_x = 32+ref_eye_width+ref_space_between_eye;
int right_eye_y = 32;
int right_eye_height = ref_eye_height;
int right_eye_width = ref_eye_width;




void draw_eyes(bool update=true)
{
    display.clearDisplay();        
    //draw from center
    int x = int(left_eye_x-left_eye_width/2);
    int y = int(left_eye_y-left_eye_height/2);
    display.fillRoundRect(x,y,left_eye_width,left_eye_height,ref_corner_radius,SSD1306_WHITE);
    x = int(right_eye_x-right_eye_width/2);
    y = int(right_eye_y-right_eye_height/2);
    display.fillRoundRect(x,y,right_eye_width,right_eye_height,ref_corner_radius,SSD1306_WHITE);    
    if(update)
    {
      display.display();
    }
    
}


void center_eyes(bool update=true)
{
  //move eyes to the center of the display, defined by SCREEN_WIDTH, SCREEN_HEIGHT
  left_eye_height = ref_eye_height;
  left_eye_width = ref_eye_width;
  right_eye_height = ref_eye_height;
  right_eye_width = ref_eye_width;
  
  left_eye_x = SCREEN_WIDTH/2-ref_eye_width/2-ref_space_between_eye/2;
  left_eye_y = SCREEN_HEIGHT/2;
  right_eye_x = SCREEN_WIDTH/2+ref_eye_width/2+ref_space_between_eye/2;
  right_eye_y = SCREEN_HEIGHT/2;
  
  draw_eyes(update);
}

void blink(int speed=12)
{
  draw_eyes();
  
  
  for(int i=0;i<3;i++)
  {
    left_eye_height = left_eye_height-speed;
    right_eye_height = right_eye_height-speed;    
    draw_eyes();
    delay(1);
  }
  for(int i=0;i<3;i++)
  {
    left_eye_height = left_eye_height+speed;
    right_eye_height = right_eye_height+speed;
    
    draw_eyes();
    delay(1);
  }
}


void sleep()
{
  left_eye_height = 2;
  right_eye_height = 2;
  draw_eyes(true);  
}
void wakeup()
{
  
  sleep();
  
  for(int h=0; h <= ref_eye_height; h+=2)
  {
    left_eye_height = h;
    right_eye_height = h;
    draw_eyes(true);
  }
  

}


void happy_eye()
{
  center_eyes(false);
  //draw inverted triangle over eye lower part
  int offset = ref_eye_height/2;
  for(int i=0;i<10;i++)
  {
    display.fillTriangle(left_eye_x-left_eye_width/2-1, left_eye_y+offset, left_eye_x+left_eye_width/2+1, left_eye_y+5+offset, left_eye_x-left_eye_width/2-1,left_eye_y+left_eye_height+offset,SSD1306_BLACK);
    //display.fillRect(left_eye_x-left_eye_width/2-1, left_eye_y+5, left_eye_width+1, 20,SSD1306_BLACK);

    display.fillTriangle(right_eye_x+right_eye_width/2+1, right_eye_y+offset, right_eye_x-left_eye_width/2-1, right_eye_y+5+offset, right_eye_x+right_eye_width/2+1,right_eye_y+right_eye_height+offset,SSD1306_BLACK);
    //display.fillRect(right_eye_x-right_eye_width/2-1, right_eye_y+5, right_eye_width+1, 20,SSD1306_BLACK);  
    offset -= 2;
    display.display();
    delay(1);
  }
  
  
  display.display();
  delay(1000);
}

void saccade(int direction_x, int direction_y)
{
  //quick movement of the eye, no size change. stay at position after movement, will not move back,  call again with opposite direction
  //direction == -1 :  move left
  //direction == 1 :  move right
  
  int direction_x_movement_amplitude = 8;
  int direction_y_movement_amplitude = 6;
  int blink_amplitude = 8;

  for(int i=0;i<1;i++)
  {
    left_eye_x+=direction_x_movement_amplitude*direction_x;
    right_eye_x+=direction_x_movement_amplitude*direction_x;    
    left_eye_y+=direction_y_movement_amplitude*direction_y;
    right_eye_y+=direction_y_movement_amplitude*direction_y;    
    
    right_eye_height-=blink_amplitude;
    left_eye_height-=blink_amplitude;
    draw_eyes();
    delay(1);
  }
  
  for(int i=0;i<1;i++)
  {
    left_eye_x+=direction_x_movement_amplitude*direction_x;
    right_eye_x+=direction_x_movement_amplitude*direction_x;    
    left_eye_y+=direction_y_movement_amplitude*direction_y;
    right_eye_y+=direction_y_movement_amplitude*direction_y;

    right_eye_height+=blink_amplitude;
    left_eye_height+=blink_amplitude;

    draw_eyes();
    delay(1);
  }
  
}

void move_right_big_eye()
{
  move_big_eye(1);
}
void move_left_big_eye()
{
  move_big_eye(-1);
}
void move_big_eye(int direction)
{
  //direction == -1 :  move left
  //direction == 1 :  move right

  int direction_oversize = 1;
  int direction_movement_amplitude = 2;
  int blink_amplitude = 5;

  for(int i=0;i<3;i++)
  {
    left_eye_x+=direction_movement_amplitude*direction;
    right_eye_x+=direction_movement_amplitude*direction;    
    right_eye_height-=blink_amplitude;
    left_eye_height-=blink_amplitude;
    if(direction>0)
    {
      right_eye_height+=direction_oversize;
      right_eye_width+=direction_oversize;
    }else
    {
      left_eye_height+=direction_oversize;
      left_eye_width+=direction_oversize;
    }

    draw_eyes();
    delay(1);
  }
  for(int i=0;i<3;i++)
  {
    left_eye_x+=direction_movement_amplitude*direction;
    right_eye_x+=direction_movement_amplitude*direction;
    right_eye_height+=blink_amplitude;
    left_eye_height+=blink_amplitude;
    if(direction>0)
    {
      right_eye_height+=direction_oversize;
      right_eye_width+=direction_oversize;
    }else
    {
      left_eye_height+=direction_oversize;
      left_eye_width+=direction_oversize;
    }
    draw_eyes();
    delay(1);
  }

  delay(1000);

  for(int i=0;i<3;i++)
  {
    left_eye_x-=direction_movement_amplitude*direction;
    right_eye_x-=direction_movement_amplitude*direction;    
    right_eye_height-=blink_amplitude;
    left_eye_height-=blink_amplitude;
    if(direction>0)
    {
      right_eye_height-=direction_oversize;
      right_eye_width-=direction_oversize;
    }else
    {
      left_eye_height-=direction_oversize;
      left_eye_width-=direction_oversize;
    }
    draw_eyes();
    delay(1);
  }
  for(int i=0;i<3;i++)
  {
    left_eye_x-=direction_movement_amplitude*direction;
    right_eye_x-=direction_movement_amplitude*direction;    
    right_eye_height+=blink_amplitude;
    left_eye_height+=blink_amplitude;
    if(direction>0)
    {
      right_eye_height-=direction_oversize;
      right_eye_width-=direction_oversize;
    }else
    {
      left_eye_height-=direction_oversize;
      left_eye_width-=direction_oversize;
    }
    draw_eyes();
    delay(1);
  }


  center_eyes();


}

void setup() {
  // put your setup code here, to run once:
  
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
  
  
  //for usb communication  
  Serial.begin(115200);
  
  

  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  
  // Clear the buffer
  display.clearDisplay();
  
  display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
  display.println(F("Intellar.ca"));
  display.display();
  delay(300);
  sleep();
  

  // Draw a single pixel in white
  //display.drawPixel(10, 10, SSD1306_WHITE);
  
  
}

void loop() {
  // put your main code here, to run repeatedly:


  
  //send A0 - A5  for animation 0 to 5
  if(Serial.available()) {
    String data = Serial.readString();
    data.trim();
    char cmd = data[0];
    
    
    
    if(cmd == 'A')
    {

      String arg = data.substring(1,data.length());
      int anim = arg.toInt();

      Serial.print(cmd);
      Serial.print(arg);
      
      

      switch(anim)
      {
        case 0:
          wakeup();
          break;
        case 1:
          center_eyes(true);
          break;
        case 2:
          move_right_big_eye();
          break;
        case 3:
          move_left_big_eye();
          break;
        case 4:      
          blink(10);
          break;
        case 5:
          blink(20);
          break;
        case 6:
          happy_eye();      
          break;
        case 7:
          sleep();
          break;
        case 8:
          int dir_x = random(-1, 2);
          int dir_y = random(-1, 2);
          saccade(dir_x,dir_y);
          delay(300);
          saccade(-dir_x,-dir_y);
          delay(300);
          break;
      }
    }
  }
}

Siehst Du den Text und danach die schlafenden Augen?

das wäre der code

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO: A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO: 2(SDA), 3(SCL), ...
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

//Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &SPI);
//uint8_t w, uint8_t h, SPIClass *spi_ptr,
// int8_t dc_pin, int8_t rst_pin, int8_t cs_pin,
// uint32_t bitrate)

//reference state
int ref_eye_height = 40;
int ref_eye_width = 40;
int ref_space_between_eye = 10;
int ref_corner_radius = 10;
//current state of the eyes
int left_eye_height = ref_eye_height;
int left_eye_width = ref_eye_width;
int left_eye_x = 32;
int left_eye_y = 32;
int right_eye_x = 32+ref_eye_width+ref_space_between_eye;
int right_eye_y = 32;
int right_eye_height = ref_eye_height;
int right_eye_width = ref_eye_width;

void draw_eyes(bool update=true)
{
display.clearDisplay();
//draw from center
int x = int(left_eye_x-left_eye_width/2);
int y = int(left_eye_y-left_eye_height/2);
display.fillRoundRect(x,y,left_eye_width,left_eye_height,ref_corner_radius,SSD1306_WHITE);
x = int(right_eye_x-right_eye_width/2);
y = int(right_eye_y-right_eye_height/2);
display.fillRoundRect(x,y,right_eye_width,right_eye_height,ref_corner_radius,SSD1306_WHITE);
if(update)
{
display.display();
}

}

void center_eyes(bool update=true)
{
//move eyes to the center of the display, defined by SCREEN_WIDTH, SCREEN_HEIGHT
left_eye_height = ref_eye_height;
left_eye_width = ref_eye_width;
right_eye_height = ref_eye_height;
right_eye_width = ref_eye_width;

left_eye_x = SCREEN_WIDTH/2-ref_eye_width/2-ref_space_between_eye/2;
left_eye_y = SCREEN_HEIGHT/2;
right_eye_x = SCREEN_WIDTH/2+ref_eye_width/2+ref_space_between_eye/2;
right_eye_y = SCREEN_HEIGHT/2;

draw_eyes(update);
}

void blink(int speed=12)
{
draw_eyes();

for(int i=0;i<3;i++)
{
left_eye_height = left_eye_height-speed;
right_eye_height = right_eye_height-speed;
draw_eyes();
delay(1);
}
for(int i=0;i<3;i++)
{
left_eye_height = left_eye_height+speed;
right_eye_height = right_eye_height+speed;

draw_eyes();
delay(1);

}
}

void sleep()
{
left_eye_height = 2;
right_eye_height = 2;
draw_eyes(true);
}
void wakeup()
{

sleep();

for(int h=0; h <= ref_eye_height; h+=2)
{
left_eye_height = h;
right_eye_height = h;
draw_eyes(true);
}

}

void happy_eye()
{
center_eyes(false);
//draw inverted triangle over eye lower part
int offset = ref_eye_height/2;
for(int i=0;i<10;i++)
{
display.fillTriangle(left_eye_x-left_eye_width/2-1, left_eye_y+offset, left_eye_x+left_eye_width/2+1, left_eye_y+5+offset, left_eye_x-left_eye_width/2-1,left_eye_y+left_eye_height+offset,SSD1306_BLACK);
//display.fillRect(left_eye_x-left_eye_width/2-1, left_eye_y+5, left_eye_width+1, 20,SSD1306_BLACK);

display.fillTriangle(right_eye_x+right_eye_width/2+1, right_eye_y+offset, right_eye_x-left_eye_width/2-1, right_eye_y+5+offset, right_eye_x+right_eye_width/2+1,right_eye_y+right_eye_height+offset,SSD1306_BLACK);
//display.fillRect(right_eye_x-right_eye_width/2-1, right_eye_y+5, right_eye_width+1, 20,SSD1306_BLACK);  
offset -= 2;
display.display();
delay(1);

}

display.display();
delay(1000);
}

void saccade(int direction_x, int direction_y)
{
//quick movement of the eye, no size change. stay at position after movement, will not move back, call again with opposite direction
//direction == -1 : move left
//direction == 1 : move right

int direction_x_movement_amplitude = 8;
int direction_y_movement_amplitude = 6;
int blink_amplitude = 8;

for(int i=0;i<1;i++)
{
left_eye_x+=direction_x_movement_amplitudedirection_x;
right_eye_x+=direction_x_movement_amplitude
direction_x;
left_eye_y+=direction_y_movement_amplitudedirection_y;
right_eye_y+=direction_y_movement_amplitude
direction_y;

right_eye_height-=blink_amplitude;
left_eye_height-=blink_amplitude;
draw_eyes();
delay(1);

}

for(int i=0;i<1;i++)
{
left_eye_x+=direction_x_movement_amplitudedirection_x;
right_eye_x+=direction_x_movement_amplitude
direction_x;
left_eye_y+=direction_y_movement_amplitudedirection_y;
right_eye_y+=direction_y_movement_amplitude
direction_y;

right_eye_height+=blink_amplitude;
left_eye_height+=blink_amplitude;

draw_eyes();
delay(1);

}

}

void move_right_big_eye()
{
move_big_eye(1);
}
void move_left_big_eye()
{
move_big_eye(-1);
}
void move_big_eye(int direction)
{
//direction == -1 : move left
//direction == 1 : move right

int direction_oversize = 1;
int direction_movement_amplitude = 2;
int blink_amplitude = 5;

for(int i=0;i<3;i++)
{
left_eye_x+=direction_movement_amplitudedirection;
right_eye_x+=direction_movement_amplitude
direction;
right_eye_height-=blink_amplitude;
left_eye_height-=blink_amplitude;
if(direction>0)
{
right_eye_height+=direction_oversize;
right_eye_width+=direction_oversize;
}else
{
left_eye_height+=direction_oversize;
left_eye_width+=direction_oversize;
}

draw_eyes();
delay(1);

}
for(int i=0;i<3;i++)
{
left_eye_x+=direction_movement_amplitudedirection;
right_eye_x+=direction_movement_amplitude
direction;
right_eye_height+=blink_amplitude;
left_eye_height+=blink_amplitude;
if(direction>0)
{
right_eye_height+=direction_oversize;
right_eye_width+=direction_oversize;
}else
{
left_eye_height+=direction_oversize;
left_eye_width+=direction_oversize;
}
draw_eyes();
delay(1);
}

delay(1000);

for(int i=0;i<3;i++)
{
left_eye_x-=direction_movement_amplitudedirection;
right_eye_x-=direction_movement_amplitude
direction;
right_eye_height-=blink_amplitude;
left_eye_height-=blink_amplitude;
if(direction>0)
{
right_eye_height-=direction_oversize;
right_eye_width-=direction_oversize;
}else
{
left_eye_height-=direction_oversize;
left_eye_width-=direction_oversize;
}
draw_eyes();
delay(1);
}
for(int i=0;i<3;i++)
{
left_eye_x-=direction_movement_amplitudedirection;
right_eye_x-=direction_movement_amplitude
direction;
right_eye_height+=blink_amplitude;
left_eye_height+=blink_amplitude;
if(direction>0)
{
right_eye_height-=direction_oversize;
right_eye_width-=direction_oversize;
}else
{
left_eye_height-=direction_oversize;
left_eye_width-=direction_oversize;
}
draw_eyes();
delay(1);
}

center_eyes();

}

void setup() {
// put your setup code here, to run once:

// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);

//for usb communication
Serial.begin(115200);

// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.

// Clear the buffer
display.clearDisplay();

display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0,0); // Start at top-left corner
display.println(F("Intellar.ca"));
display.display();
delay(300);
sleep();

// Draw a single pixel in white
//display.drawPixel(10, 10, SSD1306_WHITE);

}

void loop() {
// put your main code here, to run repeatedly:

//send A0 - A5 for animation 0 to 5
if(Serial.available()) {
String data = Serial.readString();
data.trim();
char cmd = data[0];

if(cmd == 'A')
{

  String arg = data.substring(1,data.length());
  int anim = arg.toInt();

  Serial.print(cmd);
  Serial.print(arg);
  
  

  switch(anim)
  {
    case 0:
      wakeup();
      break;
    case 1:
      center_eyes(true);
      break;
    case 2:
      move_right_big_eye();
      break;
    case 3:
      move_left_big_eye();
      break;
    case 4:      
      blink(10);
      break;
    case 5:
      blink(20);
      break;
    case 6:
      happy_eye();      
      break;
    case 7:
      sleep();
      break;
    case 8:
      int dir_x = random(-1, 2);
      int dir_y = random(-1, 2);
      saccade(dir_x,dir_y);
      delay(300);
      saccade(-dir_x,-dir_y);
      delay(300);
      break;
  }
}

}
}

ja genau.

Setze Deinen Code bitte in Codetags. So kann den keiner lesen.
Wie das geht, steht hier.

Gruß Tommy

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>


#define SCREEN_WIDTH 128  // OLED display width, in pixels
#define SCREEN_HEIGHT 64  // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL), ...
#define OLED_RESET -1        // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C  ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

//Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &SPI);
//uint8_t w, uint8_t h, SPIClass *spi_ptr,
//                               int8_t dc_pin, int8_t rst_pin, int8_t cs_pin,
//                             uint32_t bitrate)

//reference state
int ref_eye_height = 40;
int ref_eye_width = 40;
int ref_space_between_eye = 10;
int ref_corner_radius = 10;
//current state of the eyes
int left_eye_height = ref_eye_height;
int left_eye_width = ref_eye_width;
int left_eye_x = 32;
int left_eye_y = 32;
int right_eye_x = 32 + ref_eye_width + ref_space_between_eye;
int right_eye_y = 32;
int right_eye_height = ref_eye_height;
int right_eye_width = ref_eye_width;




void draw_eyes(bool update = true) {
  display.clearDisplay();
  //draw from center
  int x = int(left_eye_x - left_eye_width / 2);
  int y = int(left_eye_y - left_eye_height / 2);
  display.fillRoundRect(x, y, left_eye_width, left_eye_height, ref_corner_radius, SSD1306_WHITE);
  x = int(right_eye_x - right_eye_width / 2);
  y = int(right_eye_y - right_eye_height / 2);
  display.fillRoundRect(x, y, right_eye_width, right_eye_height, ref_corner_radius, SSD1306_WHITE);
  if (update) {
    display.display();
  }
}


void center_eyes(bool update = true) {
  //move eyes to the center of the display, defined by SCREEN_WIDTH, SCREEN_HEIGHT
  left_eye_height = ref_eye_height;
  left_eye_width = ref_eye_width;
  right_eye_height = ref_eye_height;
  right_eye_width = ref_eye_width;

  left_eye_x = SCREEN_WIDTH / 2 - ref_eye_width / 2 - ref_space_between_eye / 2;
  left_eye_y = SCREEN_HEIGHT / 2;
  right_eye_x = SCREEN_WIDTH / 2 + ref_eye_width / 2 + ref_space_between_eye / 2;
  right_eye_y = SCREEN_HEIGHT / 2;

  draw_eyes(update);
}

void blink(int speed = 12) {
  draw_eyes();


  for (int i = 0; i < 3; i++) {
    left_eye_height = left_eye_height - speed;
    right_eye_height = right_eye_height - speed;
    draw_eyes();
    delay(1);
  }
  for (int i = 0; i < 3; i++) {
    left_eye_height = left_eye_height + speed;
    right_eye_height = right_eye_height + speed;

    draw_eyes();
    delay(1);
  }
}


void sleep() {
  left_eye_height = 2;
  right_eye_height = 2;
  draw_eyes(true);
}
void wakeup() {

  sleep();

  for (int h = 0; h <= ref_eye_height; h += 2) {
    left_eye_height = h;
    right_eye_height = h;
    draw_eyes(true);
  }
}


void happy_eye() {
  center_eyes(false);
  //draw inverted triangle over eye lower part
  int offset = ref_eye_height / 2;
  for (int i = 0; i < 10; i++) {
    display.fillTriangle(left_eye_x - left_eye_width / 2 - 1, left_eye_y + offset, left_eye_x + left_eye_width / 2 + 1, left_eye_y + 5 + offset, left_eye_x - left_eye_width / 2 - 1, left_eye_y + left_eye_height + offset, SSD1306_BLACK);
    //display.fillRect(left_eye_x-left_eye_width/2-1, left_eye_y+5, left_eye_width+1, 20,SSD1306_BLACK);

    display.fillTriangle(right_eye_x + right_eye_width / 2 + 1, right_eye_y + offset, right_eye_x - left_eye_width / 2 - 1, right_eye_y + 5 + offset, right_eye_x + right_eye_width / 2 + 1, right_eye_y + right_eye_height + offset, SSD1306_BLACK);
    //display.fillRect(right_eye_x-right_eye_width/2-1, right_eye_y+5, right_eye_width+1, 20,SSD1306_BLACK);
    offset -= 2;
    display.display();
    delay(1);
  }


  display.display();
  delay(1000);
}

void saccade(int direction_x, int direction_y) {
  //quick movement of the eye, no size change. stay at position after movement, will not move back,  call again with opposite direction
  //direction == -1 :  move left
  //direction == 1 :  move right

  int direction_x_movement_amplitude = 8;
  int direction_y_movement_amplitude = 6;
  int blink_amplitude = 8;

  for (int i = 0; i < 1; i++) {
    left_eye_x += direction_x_movement_amplitude * direction_x;
    right_eye_x += direction_x_movement_amplitude * direction_x;
    left_eye_y += direction_y_movement_amplitude * direction_y;
    right_eye_y += direction_y_movement_amplitude * direction_y;

    right_eye_height -= blink_amplitude;
    left_eye_height -= blink_amplitude;
    draw_eyes();
    delay(1);
  }

  for (int i = 0; i < 1; i++) {
    left_eye_x += direction_x_movement_amplitude * direction_x;
    right_eye_x += direction_x_movement_amplitude * direction_x;
    left_eye_y += direction_y_movement_amplitude * direction_y;
    right_eye_y += direction_y_movement_amplitude * direction_y;

    right_eye_height += blink_amplitude;
    left_eye_height += blink_amplitude;

    draw_eyes();
    delay(1);
  }
}

void move_right_big_eye() {
  move_big_eye(1);
}
void move_left_big_eye() {
  move_big_eye(-1);
}
void move_big_eye(int direction) {
  //direction == -1 :  move left
  //direction == 1 :  move right

  int direction_oversize = 1;
  int direction_movement_amplitude = 2;
  int blink_amplitude = 5;

  for (int i = 0; i < 3; i++) {
    left_eye_x += direction_movement_amplitude * direction;
    right_eye_x += direction_movement_amplitude * direction;
    right_eye_height -= blink_amplitude;
    left_eye_height -= blink_amplitude;
    if (direction > 0) {
      right_eye_height += direction_oversize;
      right_eye_width += direction_oversize;
    } else {
      left_eye_height += direction_oversize;
      left_eye_width += direction_oversize;
    }

    draw_eyes();
    delay(1);
  }
  for (int i = 0; i < 3; i++) {
    left_eye_x += direction_movement_amplitude * direction;
    right_eye_x += direction_movement_amplitude * direction;
    right_eye_height += blink_amplitude;
    left_eye_height += blink_amplitude;
    if (direction > 0) {
      right_eye_height += direction_oversize;
      right_eye_width += direction_oversize;
    } else {
      left_eye_height += direction_oversize;
      left_eye_width += direction_oversize;
    }
    draw_eyes();
    delay(1);
  }

  delay(1000);

  for (int i = 0; i < 3; i++) {
    left_eye_x -= direction_movement_amplitude * direction;
    right_eye_x -= direction_movement_amplitude * direction;
    right_eye_height -= blink_amplitude;
    left_eye_height -= blink_amplitude;
    if (direction > 0) {
      right_eye_height -= direction_oversize;
      right_eye_width -= direction_oversize;
    } else {
      left_eye_height -= direction_oversize;
      left_eye_width -= direction_oversize;
    }
    draw_eyes();
    delay(1);
  }
  for (int i = 0; i < 3; i++) {
    left_eye_x -= direction_movement_amplitude * direction;
    right_eye_x -= direction_movement_amplitude * direction;
    right_eye_height += blink_amplitude;
    left_eye_height += blink_amplitude;
    if (direction > 0) {
      right_eye_height -= direction_oversize;
      right_eye_width -= direction_oversize;
    } else {
      left_eye_height -= direction_oversize;
      left_eye_width -= direction_oversize;
    }
    draw_eyes();
    delay(1);
  }


  center_eyes();
}

void setup() {
  // put your setup code here, to run once:

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);


  //for usb communication
  Serial.begin(115200);



  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.

  // Clear the buffer
  display.clearDisplay();

  display.setTextSize(1);               // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);  // Draw white text
  display.setCursor(0, 0);              // Start at top-left corner
  display.println(F("Intellar.ca"));
  display.display();
  delay(300);
  sleep();


  // Draw a single pixel in white
  //display.drawPixel(10, 10, SSD1306_WHITE);
}

void loop() {
  // put your main code here, to run repeatedly:



  //send A0 - A5  for animation 0 to 5
  if (Serial.available()) {
    String data = Serial.readString();
    data.trim();
    char cmd = data[0];



    if (cmd == 'A') {

      String arg = data.substring(1, data.length());
      int anim = arg.toInt();

      Serial.print(cmd);
      Serial.print(arg);



      switch (anim) {
        case 0:
          wakeup();
          break;
        case 1:
          center_eyes(true);
          break;
        case 2:
          move_right_big_eye();
          break;
        case 3:
          move_left_big_eye();
          break;
        case 4:
          blink(10);
          break;
        case 5:
          blink(20);
          break;
        case 6:
          happy_eye();
          break;
        case 7:
          sleep();
          break;
        case 8:
          int dir_x = random(-1, 2);
          int dir_y = random(-1, 2);
          saccade(dir_x, dir_y);
          delay(300);
          saccade(-dir_x, -dir_y);
          delay(300);
          break;
      }
    }
  }
}

Der Auslöser ist in dem Fall die Anforderung via SerialMonitor und der Eingabe einer Steuersequenz.

Leider ist der Code blockierend, sodass auf die Schnelle keine dauerhafte Lösung möglich ist.
Aber um alle Zustände abzubilden ginge es ganz einfach diese nacheinander zu durchlaufen.
Damit die, die nicht blockieren auch sichtbar werden muss irgendwie auch eine nichtblockierende Pause rein.

Hab ich mal gemacht.

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>


#define SCREEN_WIDTH 128  // OLED display width, in pixels
#define SCREEN_HEIGHT 64  // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL), ...
#define OLED_RESET -1        // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C  ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

//Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &SPI);
//uint8_t w, uint8_t h, SPIClass *spi_ptr,
//                               int8_t dc_pin, int8_t rst_pin, int8_t cs_pin,
//                             uint32_t bitrate)

//reference state
int ref_eye_height = 40;
int ref_eye_width = 40;
int ref_space_between_eye = 10;
int ref_corner_radius = 10;
//current state of the eyes
int left_eye_height = ref_eye_height;
int left_eye_width = ref_eye_width;
int left_eye_x = 32;
int left_eye_y = 32;
int right_eye_x = 32 + ref_eye_width + ref_space_between_eye;
int right_eye_y = 32;
int right_eye_height = ref_eye_height;
int right_eye_width = ref_eye_width;




void draw_eyes(bool update = true)
{
  display.clearDisplay();
  //draw from center
  int x = int(left_eye_x - left_eye_width / 2);
  int y = int(left_eye_y - left_eye_height / 2);
  display.fillRoundRect(x, y, left_eye_width, left_eye_height, ref_corner_radius, SSD1306_WHITE);
  x = int(right_eye_x - right_eye_width / 2);
  y = int(right_eye_y - right_eye_height / 2);
  display.fillRoundRect(x, y, right_eye_width, right_eye_height, ref_corner_radius, SSD1306_WHITE);
  if (update)
  {
    display.display();
  }
}


void center_eyes(bool update = true)
{
  //move eyes to the center of the display, defined by SCREEN_WIDTH, SCREEN_HEIGHT
  left_eye_height = ref_eye_height;
  left_eye_width = ref_eye_width;
  right_eye_height = ref_eye_height;
  right_eye_width = ref_eye_width;
  left_eye_x = SCREEN_WIDTH / 2 - ref_eye_width / 2 - ref_space_between_eye / 2;
  left_eye_y = SCREEN_HEIGHT / 2;
  right_eye_x = SCREEN_WIDTH / 2 + ref_eye_width / 2 + ref_space_between_eye / 2;
  right_eye_y = SCREEN_HEIGHT / 2;
  draw_eyes(update);
}

void blink(int speed = 12)
{
  draw_eyes();
  for (int i = 0; i < 3; i++)
  {
    left_eye_height = left_eye_height - speed;
    right_eye_height = right_eye_height - speed;
    draw_eyes();
    delay(1);
  }
  for (int i = 0; i < 3; i++)
  {
    left_eye_height = left_eye_height + speed;
    right_eye_height = right_eye_height + speed;
    draw_eyes();
    delay(1);
  }
}


void sleep()
{
  left_eye_height = 2;
  right_eye_height = 2;
  draw_eyes(true);
}
void wakeup()
{
  sleep();
  for (int h = 0; h <= ref_eye_height; h += 2)
  {
    left_eye_height = h;
    right_eye_height = h;
    draw_eyes(true);
  }
}


void happy_eye()
{
  center_eyes(false);
  //draw inverted triangle over eye lower part
  int offset = ref_eye_height / 2;
  for (int i = 0; i < 10; i++)
  {
    display.fillTriangle(left_eye_x - left_eye_width / 2 - 1, left_eye_y + offset, left_eye_x + left_eye_width / 2 + 1, left_eye_y + 5 + offset, left_eye_x - left_eye_width / 2 - 1, left_eye_y + left_eye_height + offset, SSD1306_BLACK);
    //display.fillRect(left_eye_x-left_eye_width/2-1, left_eye_y+5, left_eye_width+1, 20,SSD1306_BLACK);
    display.fillTriangle(right_eye_x + right_eye_width / 2 + 1, right_eye_y + offset, right_eye_x - left_eye_width / 2 - 1, right_eye_y + 5 + offset, right_eye_x + right_eye_width / 2 + 1, right_eye_y + right_eye_height + offset, SSD1306_BLACK);
    //display.fillRect(right_eye_x-right_eye_width/2-1, right_eye_y+5, right_eye_width+1, 20,SSD1306_BLACK);
    offset -= 2;
    display.display();
    delay(1);
  }
  display.display();
  delay(1000);
}

void saccade(int direction_x, int direction_y)
{
  //quick movement of the eye, no size change. stay at position after movement, will not move back,  call again with opposite direction
  //direction == -1 :  move left
  //direction == 1 :  move right
  int direction_x_movement_amplitude = 8;
  int direction_y_movement_amplitude = 6;
  int blink_amplitude = 8;
  for (int i = 0; i < 1; i++)
  {
    left_eye_x += direction_x_movement_amplitude * direction_x;
    right_eye_x += direction_x_movement_amplitude * direction_x;
    left_eye_y += direction_y_movement_amplitude * direction_y;
    right_eye_y += direction_y_movement_amplitude * direction_y;
    right_eye_height -= blink_amplitude;
    left_eye_height -= blink_amplitude;
    draw_eyes();
    delay(1);
  }
  for (int i = 0; i < 1; i++)
  {
    left_eye_x += direction_x_movement_amplitude * direction_x;
    right_eye_x += direction_x_movement_amplitude * direction_x;
    left_eye_y += direction_y_movement_amplitude * direction_y;
    right_eye_y += direction_y_movement_amplitude * direction_y;
    right_eye_height += blink_amplitude;
    left_eye_height += blink_amplitude;
    draw_eyes();
    delay(1);
  }
}

void move_right_big_eye()
{
  move_big_eye(1);
}
void move_left_big_eye()
{
  move_big_eye(-1);
}
void move_big_eye(int direction)
{
  //direction == -1 :  move left
  //direction == 1 :  move right
  int direction_oversize = 1;
  int direction_movement_amplitude = 2;
  int blink_amplitude = 5;
  for (int i = 0; i < 3; i++)
  {
    left_eye_x += direction_movement_amplitude * direction;
    right_eye_x += direction_movement_amplitude * direction;
    right_eye_height -= blink_amplitude;
    left_eye_height -= blink_amplitude;
    if (direction > 0)
    {
      right_eye_height += direction_oversize;
      right_eye_width += direction_oversize;
    }
    else
    {
      left_eye_height += direction_oversize;
      left_eye_width += direction_oversize;
    }
    draw_eyes();
    delay(1);
  }
  for (int i = 0; i < 3; i++)
  {
    left_eye_x += direction_movement_amplitude * direction;
    right_eye_x += direction_movement_amplitude * direction;
    right_eye_height += blink_amplitude;
    left_eye_height += blink_amplitude;
    if (direction > 0)
    {
      right_eye_height += direction_oversize;
      right_eye_width += direction_oversize;
    }
    else
    {
      left_eye_height += direction_oversize;
      left_eye_width += direction_oversize;
    }
    draw_eyes();
    delay(1);
  }
  delay(1000);
  for (int i = 0; i < 3; i++)
  {
    left_eye_x -= direction_movement_amplitude * direction;
    right_eye_x -= direction_movement_amplitude * direction;
    right_eye_height -= blink_amplitude;
    left_eye_height -= blink_amplitude;
    if (direction > 0)
    {
      right_eye_height -= direction_oversize;
      right_eye_width -= direction_oversize;
    }
    else
    {
      left_eye_height -= direction_oversize;
      left_eye_width -= direction_oversize;
    }
    draw_eyes();
    delay(1);
  }
  for (int i = 0; i < 3; i++)
  {
    left_eye_x -= direction_movement_amplitude * direction;
    right_eye_x -= direction_movement_amplitude * direction;
    right_eye_height += blink_amplitude;
    left_eye_height += blink_amplitude;
    if (direction > 0)
    {
      right_eye_height -= direction_oversize;
      right_eye_width -= direction_oversize;
    }
    else
    {
      left_eye_height -= direction_oversize;
      left_eye_width -= direction_oversize;
    }
    draw_eyes();
    delay(1);
  }
  center_eyes();
}

void setup()
{
  // put your setup code here, to run once:
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
  //for usb communication
  Serial.begin(115200);
  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  // Clear the buffer
  display.clearDisplay();
  display.setTextSize(1);               // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);  // Draw white text
  display.setCursor(0, 0);              // Start at top-left corner
  display.println(F("Intellar.ca"));
  display.display();
  delay(300);
  sleep();
  // Draw a single pixel in white
  //display.drawPixel(10, 10, SSD1306_WHITE);
}

uint32_t lastmillis;
byte anim;
void loop()
{
  if (millis() - lastmillis > 20000)
  {
    anim++;
    lastmillis = millis();
  }
  // put your main code here, to run repeatedly:
  //send A0 - A5  for animation 0 to 5
  switch (anim)
  {
    case 0:
      wakeup();
      break;
    case 1:
      center_eyes(true);
      break;
    case 2:
      move_right_big_eye();
      break;
    case 3:
      move_left_big_eye();
      break;
    case 4:
      blink(10);
      break;
    case 5:
      blink(20);
      break;
    case 6:
      happy_eye();
      break;
    case 7:
      sleep();
      break;
    case 8:
      int dir_x = random(-1, 2);
      int dir_y = random(-1, 2);
      saccade(dir_x, dir_y);
      delay(300);
      saccade(-dir_x, -dir_y);
      delay(300);
      break;
    default:
      anim = 0;
      break;
  }
}

(gelöscht, hat sich überschnitten)

super vielen dank :slight_smile: der auto hat noch folgendes.

import serial

  def sendAnimToDisplay(display:serial, index) : ""
                                                 "
                        case 0 :wakeup();
break;
case 1:
  center_eyes(true);
  break;
case 2:
  move_right_big_eye();
  break;
case 3:
  move_left_big_eye();
  break;
case 4:
  blink(10);
  break;
case 5:
  blink(20);
  break;
case 6:
  happy_eye();
  break;
case 7:
  sleep();
  break;
  ""
  "
    cmd = 'A' + str(int(index)) cmdByte = bytes(cmd, 'utf-8')
                                            print("writing ", cmd, cmdByte)
                                              display.write(cmdByte)```




import display_control import serial import time

arduino = serial.Serial("COM3", 115200, timeout = .1) time.sleep(0.5)

                                        for i in range(8) :display_control.sendAnimToDisplay(arduino, i) time.sleep(2)

keine Ahnung, was das wird.
gehe mal in Deinen Post mit dem bleistift unten und markier den ganzen Code und dann klickst Du im editor oben auf <code> und speicherst das ab - sonst ist das nicht zu lesen (und zu verstehen)

da ist beim posten as schief gegangen. Poste das nochmal einzeln.

import serial

  def sendAnimToDisplay(display:serial, index) : ""
                                                 "
                        case 0 :wakeup();
break;
case 1:
  center_eyes(true);
  break;
case 2:
  move_right_big_eye();
  break;
case 3:
  move_left_big_eye();
  break;
case 4:
  blink(10);
  break;
case 5:
  blink(20);
  break;
case 6:
  happy_eye();
  break;
case 7:
  sleep();
  break;
  ""
  "
    cmd = 'A' + str(int(index)) cmdByte = bytes(cmd, 'utf-8')
                                            print("writing ", cmd, cmdByte)
                                              display.write(cmdByte)
import display_control import serial import time

  arduino = serial.Serial("COM3", 115200, timeout = .1) time.sleep(0.5)

                                            for i in range(8) :display_control.sendAnimToDisplay(arduino, i) time.sleep(2)

Ich versteh nicht, was das ist.
Da ich das Ausgangsmaterial auch nicht habe, kann ich Dir nciht weiter helfen.

Der erste Code wartet auf eine Eingabe - die hab ich Dir geändert, weil Du die nicht möchtest.
Mehr kann ich nicht tun, da ich weder die Aufgabe noch das Ergebnis kenne.

Vielen lieben dank für deine Hilfe :wink:
Ich wünsche dir noch einen schönen Abend.

Es funktioniert Jetzt wie beschrieben! Vielen vielen lieben dank!

Na siehste. :wink:
Dann mach nen Haken dran, dass es gelöst ist.

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