Here's one way to write a Python function that returns a list of prime numbers up to a given maximum value:

Copy code
def get_primes(maximum):
    primes = []
    for num in range(2, maximum + 1):
        for prime in primes:
            if num % prime == 0:
                break
        else:
            primes.append(num)
    return primes

print(get_primes(20))
This function uses a for loop to iterate through the numbers from 2 to the given maximum value. For each number, it checks if it is divisible by any of the prime numbers that have been found so far. If it is not divisible by any of them, it is added to the list of primes. At the end, the list of primes is returned.

You can call the function with a maximum value to get a list of all prime numbers up to that value. In the example above, the output would be [2, 3, 5, 7, 11, 13, 17, 19].

Posted by 살레시오
,

Q) 아두이노의 5번핀에 LED가 연결되어 있을 경우 1초 간격으로 깜박이는 프로그램을 작성해줘.
A)

void setup() {
  pinMode(5, OUTPUT); // 5번 핀을 출력으로 설정
}

void loop() {
  digitalWrite(5, HIGH); // 5번 핀에 전압 인가
  delay(1000); // 1초 대기
  digitalWrite(5, LOW); // 5번 핀에 전압 끄기
  delay(1000); // 1초 대기
}
위 코드를 사용하면 1초 간격으로 LED가 깜박입니다.

 

 

Q) 아두이노의 I2C에 LCD가 연결된 경우 "Hello world"를 표시하는 프로그램을 작성해줘.
A)

#include <Wire.h> 
#include <LiquidCrystal_I2C.h> 

LiquidCrystal_I2C lcd(0x27,16,2); 

void setup() {
  lcd.init();
  lcd.backlight();
  lcd.clear();
  lcd.print("Hello world!");
}

void loop() {
}
위 코드를 사용하면 I2C를 이용하여 연결된 LCD에 "Hello world"가 표시됩니다.
LiquidCrystal_I2C.h 라이브러리를 추가해야 하며, 사용하는 LCD의 I2C 주소를 확인해야 할 수 있습니다.

 


Q) 아두이노로 DC모터를 제어하는 프로그램을 작성해줘.
A)

#include <Adafruit_MotorShield.h> 
#include "utility/Adafruit_MS_PWMServoDriver.h"

Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);

void setup() {
  AFMS.begin();
  myMotor->setSpeed(255); // set the speed to 255
  myMotor->run(FORWARD); // run the motor forward
  myMotor->run(RELEASE); // stop the motor
}

void loop() {
    myMotor->run(FORWARD); // run the motor forward
    delay(1000); // wait for 1 second
    myMotor->run(BACKWARD); // run the motor backward
    delay(1000); // wait for 1 second
    myMotor->run(RELEASE); // stop the motor
}
위 코드를 사용하면, Adafruit Motor Shield 라이브러리를 사용하여 1번 모터를 제어하며, 1초마다 방향을 바꾸어 DC모터를 제어 할 수 있습니다.

모터의 방향을 제어하기 위해서는 아래 구문 사용 가능
myMotor->run(FORWARD); // run the motor forward
myMotor->run(BACKWARD); // run the motor backward
myMotor->run(RELEASE); // stop the motor

모터 속도를 제어하기 위해서는 아래 구문 사용 가능
myMotor->setSpeed(255); // set the speed to 255

(모터 속도는 0~255 사이의 값을 사용 가능)

라이브러리는 아두이노 IDE에서 Adafruit Motor Shield V2 for Arduino library를 설치해야 합니다.

Posted by 살레시오
,

윤동주의 서시 "죽는 날까지 하늘을 우러러 한 점 부끄럼이 없기를, 잎새에 이는 바람에도 나는 괴로워했다. 별을 노래하는 마음으로 모든 죽어 가는 것을 사랑해야지 그리고 나한테 주어진 길을 걸어가야겠다. 오늘 밤에도 별이 바람에 스치운다."를 영어로 번역해줘.

"Until the day I die, I will gaze at the sky

Wishing for not a single speck of shame

Even when the wind blows through the leaves

I have felt pain.

With a heart that sings of stars

I must love all that dies

And the path that is given to me

I must walk it."

Please note that the above is my interpretation of the poem, and there might be variations in the translation of some phrases or expressions depending on the context and the translator's perspective.

 

Posted by 살레시오
,