Атомная электронная анимация с ws2812 rgb с использованием Arduino - PullRequest
0 голосов
/ 14 января 2020

Пытаюсь сделать анимацию электрона атома в качестве учебного пособия. Для учителя химии было непросто заставить учащихся представить себе маленькие электроны, движущиеся вокруг ядра. У меня есть простой способ использовать светодиод для представления электронов, в то время как ядра, в которых не движутся протоны и нейтроны, отображаются на ЖК-дисплее, а также в единицах массы (MU). Я сделал код для водорода, это просто, потому что его светодиодное освещение вокруг кольца RGB. Вот код.

#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define PIN 3
#define BRIGHTNESS 30
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      8

//////////////////////////////////////////////Define variables for LCD1602//////////////////////////////////////////

#define I2C_ADDR          0x27       //Define I2C Address where the PCF8574A is
#define BACKLIGHT_PIN      3
#define En_pin             2
#define Rw_pin             1
#define Rs_pin             0
#define D4_pin             4
#define D5_pin             5
#define D6_pin             6
#define D7_pin             7

////////////////////////////////////////////////////Initialize the LCD1602///////////////////////////////////////////
LiquidCrystal_I2C      lcd(I2C_ADDR, En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
//                BS  E  D4 D5  D6 D7
//LiquidCrystal_I2C lcd(7, 8, 9, 10, 11, 12);
void printDetail(uint8_t type, int value);

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRBW + NEO_KHZ800);

//int delayval = 250; // delay for half a second
void setup()
{
  Serial.begin(115200);

  Serial.println();
  pixels.setBrightness(BRIGHTNESS);
  pixels.begin(); // This initializes the NeoPixel library.
lcd.begin (16,2); //Define the LCD as 16 column by 2 rows 
 lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); //Switch on the backlight
 lcd.setBacklight(HIGH);

}

void loop()
{
 lcd.setCursor(2,0);
  lcd.print("Hydrogen Atom");
  lcd.setCursor(1,1);
  lcd.print("P=1,N=0, MU =1");

  for(int i=0; i<=8; i++)
  {
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(0,255,0)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(100);
    pixels.clear();
   //delay(delayval); // Delay for a period of time (in milliseconds).
  }

 lcd.clear();
}

Я также сделал для гелия, как указано ниже

#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
//#define PIN_A
//#define PIN_B
#define PIN 3
#define SDA A4
#define SDL A5
#define PIN 3
//#define PIN_A 8
#define BRIGHTNESS 30
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      12

//////////////////////////////////////////////Define variables for LCD1602//////////////////////////////////////////

#define I2C_ADDR          0x27       //Define I2C Address where the PCF8574A is
#define BACKLIGHT_PIN      3
#define En_pin             2
#define Rw_pin             1
#define Rs_pin             0
#define D4_pin             4
#define D5_pin             5
#define D6_pin             6
#define D7_pin             7

////////////////////////////////////////////////////Initialize the LCD1602///////////////////////////////////////////
LiquidCrystal_I2C      lcd(I2C_ADDR, En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
//                BS  E  D4 D5  D6 D7
//LiquidCrystal_I2C lcd(7, 8, 9, 10, 11, 12);
void printDetail(uint8_t type, int value);

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRBW + NEO_KHZ800);

//int delayval = 250; // delay for half a second
void setup()
{
  Serial.begin(115200);

  Serial.println();
  pixels.setBrightness(BRIGHTNESS);
  pixels.begin(); // This initializes the NeoPixel library.
lcd.begin (16,2); //Define the LCD as 16 column by 2 rows 
 lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); //Switch on the backlight
 lcd.setBacklight(HIGH);

}

void loop()
{
Serial.println("Lithium Atom");
 lcd.setCursor(2,0);
  lcd.print("Helium Atom");
  lcd.setCursor(1,1);
  lcd.print("P=2,N=2 MU=4");

  for(int i=0; i<5; i++)
  {
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(255,0,0)); // Moderately bright green color.
    pixels.setPixelColor(5+i, pixels.Color(0,255,0));
    pixels.show(); // This sends the updated pixel color to the hardware.
   delay(200);
   pixels.clear();
   //delay(delayval); // Delay for a period of time (in milliseconds).
  }

 // lcd.clear();
}

Моя проблема, в которой мне нужна помощь, связана с литием, и поэтому я могу перебирать другие атомы вверх - к калию.

Я хочу получить помощь, используя подход Миллиса, потому что задержка не приводит к плавному светодиоду. литиевый код с задержкой

#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define PIN_A 3
#define PIN_B 4
//#define PIN 7
//#define PIN 8
#define BRIGHTNESS 30
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS_A      3
#define NUMPIXELS_B      4



//////////////////////////////////////////////Define variables for LCD1602//////////////////////////////////////////

#define I2C_ADDR          0x27       //Define I2C Address where the PCF8574A is
#define BACKLIGHT_PIN      3
#define En_pin             2
#define Rw_pin             1
#define Rs_pin             0
#define D4_pin             4
#define D5_pin             5
#define D6_pin             6
#define D7_pin             7

////////////////////////////////////////////////////Initialize the LCD1602///////////////////////////////////////////
LiquidCrystal_I2C      lcd(I2C_ADDR, En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
//                BS  E  D4 D5  D6 D7
//LiquidCrystal_I2C lcd(7, 8, 9, 10, 11, 12);
void printDetail(uint32_t type, int value);

Adafruit_NeoPixel pixels_A = Adafruit_NeoPixel(NUMPIXELS_A, PIN_A, NEO_GRBW + NEO_KHZ800);
Adafruit_NeoPixel pixels_B = Adafruit_NeoPixel(NUMPIXELS_B, PIN_B, NEO_GRBW + NEO_KHZ800); 
//int delayval = 250; // delay for half a second
unsigned long interval = 100;
unsigned long previousMillis  = 0; 
//const unsigned long pixel_B = 0;

//unsigned long interval pixel_B = 100;
void setup()
{
  //Serial.begin(115200);

 // Serial.println();
 pixels_A.setBrightness(BRIGHTNESS);
  pixels_A.begin(); // This initializes the NeoPixel library.
 pixels_B.setBrightness(BRIGHTNESS);
  pixels_B.begin(); // This initializes the NeoPixel library. 
lcd.begin (16,2); //Define the LCD as 16 column by 2 rows 
 lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); //Switch on the backlight
 lcd.setBacklight(HIGH);

}

void loop()
{
if ((unsigned long)(millis() - previousMillis) >= interval) {
  previousMillis = millis();

 int i,j;
 lcd.setCursor(2,0);
  lcd.print("Lithium Atom");
  lcd.setCursor(1,1);
  lcd.print("P=3 N=4 MU=?");

    }

 for (unsigned int i=0; i<4; i++) for (unsigned int j=0; j<12; j++)
  {

  pixels_A.setPixelColor(i, pixels_A.Color(255,0,0)); // Moderately bright green color.
    pixels_A.setPixelColor(4+i,pixels_A.Color(0,255,0));

   // pixels_B.setPixelColor(j, pixels_B.Color(0,0,255(50))); // Moderately bright green color.
   //  pixels_A.show(); // This sends the updated pixel color to the hardware.
   }

  // delay(10);
 //   pixels_A.clear();
 // pixels_B.setPixelColor(j, pixels_B.Color(0,0,255)); // Moderately bright green color.
// delay(50);
// pixels_B.show(); // This sends the updated pixel color to the hardware.
// pixels_B.clear();



   //for (int j=0; j<12; j++)
   // { pixels_B.setPixelColor(j, pixels_B.Color(0,0,255)); // Moderately bright green color.
   // pixels_B.show(); // This sends the updated pixel color to the hardware.

   // delay(100);
    //pixels_B.clear();
   //delay(delayval); // Delay for a period of time (in milliseconds).
  }

 lcd.clear();

}

Атомы имеют энергетические уровни, в этом случае я использую кольца для представления энергетических уровней. Например, первое кольцо с восемью светодиодами, второе с 12 светодиодами и т. Д.

плата Mega 2650, ЖК-дисплей и светодиодные кольца ws2812

Я также хотел бы использовать кнопка pu sh, так что она может изменить элементы нет. от водорода до калия каждый раз, когда вы pu sh это. Таким образом, второе кольцо надето на другой контакт, так же как и третье кольцо

...