나는 운동을 위해 Raspberry Pi와 함께 작업하고 있습니다. 나는 브레드 보드에 다음과 같이 일하고 있어요Arduino-uno 프로젝트
:
- 5의 LED
- 이 스위치는 내 다음 코드는 다음과 같이 작동하도록되어
다음 다음으로
첫 번째 버튼은 다음 LED를 선택하고 두 번째 버튼은 선택한 LED를 켜거나 끌 수 있습니다.
//const variables
const int leds[] = {3, 5, 6, 9, 11};
const int buttons[] = {12, 13};
//variables that will change
int buttonState[] = {false, false};
int lastButtonState[] = {false, false};
int values[] = {false, false, false, false};
void setup() {
//init LEDs
for(int i = 0; i < sizeof(leds); i++){
pinMode(leds[i], OUTPUT);
}
//init buttons
for(int i = 0; i < sizeof(buttons); i++){
pinMode(buttons[i], INPUT);
}
}
void loop() {
//fade when game starts
fade();
//start game
start();
//output of game
output();
}
void output(){
bool t1 = !values[0];
bool t2 = t1 && values[1];
bool t3 = values[2] || values[3];
bool Q = !(t2 || t3);
if(!Q){
digitalWrite(leds[4], true);
}else{
digitalWrite(leds[4], false);
}
}
void start(){
//total of leds
int j = 0;
//check if user is not at 5th led
while(j < 4){
//loop through buttons
for(int i = 0; i < 2; i++){
// Read button
buttonState[i] = digitalRead(buttons[i]);
// check button state
if (buttonState[i] != lastButtonState[i]) {
// if the state has changed
if (buttonState[i] == HIGH) {
//check if button 1
if(i == 0){
//select next LED
j++;
}
//else button 2
else{
// if the current state of the 2nd button is HIGH
while(i == 1){
//if current value of led is false, put it true
if(values[j] == false){
//put led on
digitalWrite(leds[j], true);
values[j] = true;
delay(50);
}else{
//put led off
digitalWrite(leds[j], false);
delay(50);
values[j] = false;
}
//go back to button 1?
i = 0;
}
}
//go back to button 1?
i = 0;
}
}
// save the current state as the last state,
// for next time through the loop
lastButtonState[i] = buttonState[i];
// wait a little
delay(50);
}
}
}
void fade(){
//put every led on half-on
for(int i = 0; i < sizeof(leds); i++){
analogWrite(leds[i], 100);
}
}
경고없이 빌드되지 않습니다. 어떤 문제라도 언급하지 않는 이유는 무엇입니까? 'pinMode (leds, OUTPUT);'문은 틀린 것이어야하고, 결국은 루프의 포인트 인 (그리고 입력과 동일한)'pinMode (leds [i], OUTPUT);'이어야합니다. – unwind
확인했을 때 오류없이 실행되었지만 지금 업데이트했습니다. 그래서 그것은 더 이상 문제가 아닙니다. 현재 두 번째 버튼은 LED를 켜기/끄기로 설정해야 LED가 꺼지고 켜지지 않습니다. – DiceOfDoom
귀하의 질문이 순수한 소프트웨어에 관한 한, 주제에 관한 것입니다. 그러나 기록을 위해 소프트웨어 또는 하드웨어와 관련된 질문은 http://electronics.stackexchange.com/ (코드 외에도 몇 가지 회로도를 원할 것입니다)에서 질문해야합니다. 특히 Ardunio에 대한 질문은 http://arduino.stackexchange.com/에서 확인하십시오. http://raspberrypi.stackexchange.com/도 있습니다. – Lundin