在JavaScript中,我可能会遍历一组包含数据的对象,如下所示:
const components = [
{
id: 1,
pin: "A0",
name: "light"
},
{
id: 2,
pin: "A1",
name: "sound"
},
{
id: 1,
pin: "A0",
name: "heat"
},
]
for (const component of components) {
const value = analogRead(component.pin);
console.log(`${component.name}:value`)
}
我经常需要在Arduino上使用类似的代码,但是我不确定该如何处理。
注意:我不希望将其准确翻译为C ++;我想知道使用Arduino时实现此目的的标准模式。
答案 0 :(得分:1)
您可以使用public void ConfigureServices(IServiceCollection services)
{
// ... other services added here ...
// One JWTAuthenticationStateProvider for each connection on server side.
// A singleton for clientside.
services.AddScoped<AuthenticationStateProvider,
JWTAuthenticationStateProvider>();
}
。为此,您需要声明一个首先描述您的对象类型的结构。
C structure
输出:
struct component
{
int id;
char pin[10];
char name[50];
};
component components[] = {
{
1,
"A0",
"light"},
{
2,
"A1",
"sound"},
{
1,
"A0",
"heat"}
};
int main ()
{
int len = sizeof(components)/sizeof(components[0]);
for (int i=0 ; i<len ; i++)
{
printf("{ id: %d , pin: \"%s\" , name: \"%s\" }\n",components[i].id, components[i].pin, components[i].name);
}
return 0;
}
答案 1 :(得分:0)
如果您使用的是现代C ++(11或更高版本,我了解arduino支持),并且您的数据存储在数组中,则只需执行以下操作即可:
int values[5] = { 16, 2, 77, 40, 12071 }
for(auto const& value: values) {
// Do stuff
}