2017-12-27 54 views
0

저는 새로운 프로그래머이고 Arduino를 작성하는이 프로그램에 대해 몇 가지 질문을 가지고 있습니다. 여기 for 루프를 사용하는 Arduino 파형 발생기가 순환하지 않습니다.

내가 할 노력하고 무엇 :

  • 서로를 트리거 루프를위한 한 쌍을 사용하여 8 비트 삼각파 발생을 만듭니다.
  • 주파수를 관리하기 위해 웨이브 증가/감소 사이의 시간을 제어하십시오.
  • 8 비트 변수 threshold1을 웨이브의 진폭과 비교하고 리드 온시를 뒤집습니다.

나는이 문제로 실행하고 있습니다 : 나는 아래 가지고 내가 논리의 두 번째 절반을 주석 처리하지 않는 한 루프 모두에서

  1. 내 증가/감소 상태가 트리거되지 않습니다.

  2. 루프의 앞쪽 가장자리와 루프 트리거의 한쪽 끝이 한 번 트리거되고 다시 트리거되지 않고 중지됩니다. 그 이유를 이해하지 못하고 있습니다.

나는 나보다 많은 경험을 가진 몇 명의 친구에게 다가 가려고했지만 모두 엉망이었습니다. 필자는 직렬 모니터를 사용하여 파형 발생기에서 하나의 성공적인 사이클을 얻고, 플립 플롭을 작동시키는 디버그 작업을 시작했습니다 (아래에서 설명한대로 로직 조건의 두 번째 부분을 주석 처리합니다).

내가 배우려고하는 바는 for 루프가 전체 사이클 후에 의도 한대로 다시 트리거되도록하고 여러 조건 (2 개 이상)을 사용하여 증가/감소를 처리하는 가장 좋은 방법은 무엇이겠습니까? 비교)? (- 거기에 걸어 당신이 초보자 말했다 때문에 의미가 있습니다)

/* this program is my first attemp to design a dual window comparator within the arduino for use in a eurorack environment. 
/the basic premise is a 8bit triangle waveform generator and two 8bit variables. when the wave and variable match a 
/flip occurs, so 4 flips per period, on off on off, and when those occur (and therefore, the duty cycle of the pwn square 
/output) is controlled by the 2 threshold variables */ 

// constants won't change. Used here to set a pin number: 
const int ledPin = 13;// the number of the LED pin 

// Variables will change: 
int ledState = LOW;// ledState used to set the LED 
long stpLngth = 200; //triangle wave step length TODO make an analog read do this and make a scaler to make it exponential control ei v/Oct 

void setup() { 
    // set the digital pin as output: 
    pinMode(ledPin, OUTPUT); 

    // Time comparison for setting the wave frequency: 
    unsigned long currentMillis = 0; 
    unsigned long previousMillis = 0; 

    byte threshold1 = 0b01100000; //starting position 98/256 
    byte threshold2 = 0b10000000; //starting position 128/256 
    // I will add code to handle a second threshold once I get the for loop to work as expected 
} 

void loop() { 
    Serial.begin(9600); 
    unsigned long currentMillis = millis(); 
    unsigned long previousMillis = 0; 
    unsigned char wavePosition = 0; 
    byte threshold1 = 0b01100000; //starting position 98/256 
    byte threshold2 = 0b10000000; //starting position 128/256 
    long stepLength = 200; 
    //this variable is to compare to the difference between currentMillis and previousMillis, and control frequency. TODO write a tap tempo and a mapped analog control for this 
    /*This is the leading edge of my triangle wave, I want it to initiate a waveat 0 and increment if: 
    * a) it's less than 254 and 
    * b) (currentMillis - previousMillis) is equal or greater than stepLength*/ 

    //I've commented out the extra logic conditions I was trying to use to control the frequency below and now it increments, but I am still hoping to learn a way to control the time between steps: 
    for(wavePosition=0x00; wavePosition<0xff /*&& ((currentMillis - previousMillis) >= stepLength)*/; wavePosition++) { 
    previousMillis = currentMillis; 
    Serial.println("increment waveform"); 
    //this is for debugging it prints each time the wave decrements 
    if (threshold1 == wavePosition) { 
     //compares threshold1 to wavePosition flipflop led on match 
     if (ledState == LOW) { 
     // if the LED is off turn it on and vice-versa: 
     ledState = HIGH; 
     } else { 
     ledState = LOW; 
     } 
     Serial.println("Led flip 2"); 
     //this is for debugging it prints when the led changes state 
     digitalWrite(ledPin, ledState); 
    } 
    } 

    // falling edge of wave same logic conditions commented out, this one decrements but at the end of the waveform it doesn't restart the rising edge for loop it just hangs. 
    for(wavePosition=0xff; wavePosition>0x01/* && ((currentMillis - previousMillis >= stepLength)*/; wavePosition--) { 
    previousMillis = currentMillis; 
    Serial.println("decrement waveform"); 
    //this is for debugging it prints each time the wave decrements 
    if (threshold1 == wavePosition) { 
     //compares threshold1 to wavePosition flipflop led on match 
     if (ledState == LOW) { 
     // if the LED is off turn it on and vice-versa: 
     ledState = HIGH; 
     } else { 
     ledState = LOW; 
     } 
     Serial.println("Led flip 2"); 
     //this is for debugging it prints when the led changes state 
     digitalWrite(ledPin, ledState); 
    } 
    } 
} 
+0

'Serial.begin (9600);은 항상'setup()'함수에 있어야합니다. 'loop()'함수에서'Serial.begin (9600);을 사용하여 간단한 "Hello World"프로그램을 실행하더라도 실패 할 수 있습니다. – Nitred

+0

그게 문제를 해결했지만, 지금은 반복됩니다. 고마워. –

답변

2

이 코드는 버그가 많이 포함되어

여기 내 코드입니다. 내가 좋을 것 가장 큰 사항은 다음과 같습니다 초보자로서

  1. , setup()loop() 전에 모든 변수를 선언하고 나중에 다시 선언하지 않습니다.
  2. 모든 초기화 코드를 setup()으로 옮깁니다.
  3. loop()에 설치 코드가 없는지 확인하십시오.

아두 이노 스케치가 실행될 때, 아두 이노, 그냥 한 번 setup()를 호출 한 후 영원히 반복 loop()를 호출 것을 기억하십시오.

예를 들어, Nitred가 지적한대로 Serial.begin(9600)setup() 안에 있고 loop() 안에 있지 않아야합니다. 또한 threshold1threshold2이 설정되어 있고 setup()loop()에 모두 선언되어 있습니다. 대신 setup()loop() 앞에 선언하고 setup()으로 설정해야합니다.

코드를 수정하면 코드가 제대로 실행되지 않습니다. 코드에 여러 가지 버그가 포함되어 있기 때문에 다른 버그를 쉽게 찾을 수 있습니다. 예를 들어, loop()이 호출 될 때마다 loop()의 시작이 재설정되기 때문에 루프는 currentMillispreviousMillis과 관련하여 원하는 방식으로 작동하지 않습니다.

행운을 빈다. Brad

+0

훌륭한 조언! – dda