Arduino에서 배열에 문제가 있습니다 (기본적으로 "GeneralInput"유형의 "Inputs"라고 함). 액세스 할 수있는 요소가 무엇이든 관계없이 코드에서 항상 마지막 요소를 반환합니다. 그 배열의. 다음은 코드의 일부 :배열은 마지막 요소 만 계속 반환합니다. [C/Arduino]
//...include statements
//other initializations
GeneralInput *Inputs[19];
void setup()
{
//...
//...
InitializeInputs();
}
void InitializeInputs()
{
//type 0 = pedal switch; 1 = volume pedal
//type 2 = potentiometer; 3= switch;
//pedal switches
Inputs[0] = &GeneralInput(0,0,true,false,NULL,10);
Inputs[1] = &GeneralInput(1,0,true,false,NULL,9);
Inputs[2] = &GeneralInput(2,0,true,false,NULL,6);
Inputs[3] = &GeneralInput(3,0,true,false,NULL,5);
//volume pedal
Inputs[4] = &GeneralInput(4,1,false,false,NULL,A2);
//potentiometer
Inputs[5] = &GeneralInput(5,2,false,true,mux2,5);
Inputs[6] = &GeneralInput(6,2,false,true,mux2,6);
Inputs[7] = &GeneralInput(7,2,false,true,mux2,7);
Inputs[8] = &GeneralInput(8,2,false,true,mux2,8);
Inputs[9] = &GeneralInput(9,2,false,true,mux2,9);
Inputs[10] = &GeneralInput(10,2,false,true,mux2,10);
Inputs[11] = &GeneralInput(11,2,false,true,mux2,11);
//switch
Inputs[12] = &GeneralInput(12,3,true,true,mux2,15);
Inputs[13] = &GeneralInput(13,3,true,true,mux2,14);
Inputs[14] = &GeneralInput(14,3,true,true,mux2,13);
Inputs[15] = &GeneralInput(15,3,true,true,mux2,12);
//joystick
Inputs[16] = &GeneralInput(16,3,true,true,mux1,2); //switch
Inputs[17] = &GeneralInput(17,2,false,true,mux1,1); //x axis
Inputs[18] = &GeneralInput(18,2,false,true,mux1,3); //y axis
}
void loop()
{
int length=0;
//cycle through different inputs
int startIndex=0,endIndex=0;
//temp arrays
byte toSendTmp[30];
for(int i=0;i<30;i++)
toSendTmp[i]=0;
//...
//..
int packetIndex=0;
for(int i=startIndex;i<endIndex;i++)
{
//if the input is updated,fill the array with the new data
/*
* When i try to have access to the i-element i always get
* the last one instead.
*/
if(Inputs[i]->Update())
{
toSendTmp[(packetIndex*3)] = Inputs[i]->GetID();
toSendTmp[(packetIndex*3)+1] = Inputs[i]->GetType();
toSendTmp[(packetIndex*3)+2] = Inputs[i]->GetValue();
packetIndex++;
}
}
//....
//...
}
을 그리고 여기에 필요한 경우 GeneralInput.h 및 GeneralInput.cp P 코드입니다. 참고 : 배열이 항상 마지막 항목을 반환하는지 또는 배열의 모든 슬롯이 동일한 개체 (마지막으로 생성 된)에 대한 포인터로 채워지는지 여부를 알 수 없습니다.
내가 뭘 잘못하고 있는지에 대한 아이디어가 있습니까?
미리 감사드립니다. 귀하의 &GeneralInput
나는 이것이 컴파일하는 것에 놀랐습니다. 임시 오브젝트의 주소는 가져올 수 없습니다. –
Arduino에서 'new'를 사용할 수 없으므로 고맙습니다. 맞습니다. 문제를 살펴 보겠습니다. – MTKJacob
@JosephMansfield C에서 무엇에 관한 주소 (예 :'char ** foo = &"bar";')가 "취할 수"있습니다. 그러나 로컬 변수와 같이 일반적으로 처리되지 않는 무언가의 주소를 취하는 것이 유용 할 수있는 상황은 제한적입니다. –