8个随机继电器。在任何给定时间最多需要3个,我该怎样才能实现这个目标? [英] 8 random relays. Maximum of 3 required at any given time only how can I acheive this?
本文介绍了8个随机继电器。在任何给定时间最多需要3个,我该怎样才能实现这个目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个代码,用于随机控制8个继电器。该计划是为我的万圣节道具。我是编码的新手。我只希望以随机顺序在任何时间运行最多3个继电器。关于如何实现这个问题的任何想法。
非常感谢提前
#define time1 100
#define time2 3000
void setup()
{
pinMode( 2 ,OUTPUT); // relay 1
pinMode( 3 ,OUTPUT); // relay 2
pinMode( 4 ,OUTPUT); // relay 3
pinMode( 5 ,OUTPUT); // relay 4
pinMode( 6 ,OUTPUT); // relay 5
pinMode( 7 ,OUTPUT); // relay 6
pinMode( 8 ,OUTPUT); // relay 7
pinMode( 9 ,OUTPUT); // relay 8
delay( 50 < /跨度>); // 检查所有继电器在复位时是否处于非活动状态
digitalWrite( 2 跨度>,HIGH);
digitalWrite( 3 ,HIGH);
digitalWrite( 4 ,HIGH);
digitalWrite( 5 ,HIGH);
digitalWrite( 6 ,HIGH);
digitalWrite( 7 ,HIGH);
digitalWrite( 8 ,HIGH);
digitalWrite( 9 ,HIGH);
}
// - (结束设置)---
void loop()
{
digitalWrite(random( 2 , 10 ),LOW); // 设置
延迟的随机中继(random(time1,time2)); // 等待一段时间(1到30秒之间)
digitalWrite(random(< span class =code-digit> 2 , 10 ),HIGH); // 设置随机中继
延迟(random(time1,time2)); // 等待一段时间(1到30秒之间)
}
我的尝试:
尝试重新编写代码,但没有成功,程序冻结。
解决方案
这是C#代码,但应该很容易转换为Arduino的C:
// 生成20个值......
for ( int x = 0 ; x < 20 ; x ++)
{
// 保持字节值打开位。
byte value = 0 ;
// count保持最大位数
// 启用此时间,最多为maxBitsOnPerByte。
// 在您的情况下,maxBitsOnPerByte应为3。
// RNG是随机数类。 'Next'将返回
// 介于0和指定限制之间的整数,EXCLUSIVE。
// 在您的示例中,这意味着0,1,2和3。 / span>
int count = RNG.Next(maxBitsOnPerByte + 1 );
// 循环'计数'次,打开字节中的随机位。 / span>
// 注意,这个CAN多次转动相同的位!
for ( int y = 0 ; y < count; y ++)
{
// 取一个位(1)并将其左移一个
// 数字(从0到7)位。然后使用我们正在跟踪的现有值,或者此值
// 。
// 在C#中,OR运算的值是整数
// 必须回送到一个字节。
value =( byte )( value |(0x1 << RNG.Next( 8 )));
}
// 将值转换为字符串或1和0,
// 当然用0填充。
string binary = Convert.ToString( value , 2 ).PadLeft( 8 ,' 0');
Console.WriteLine(
Value:{binary} 跨度>);
}
作为一种简单的方法,您可以(随机)选择可以的继电器数量在给定时刻ON
(比如MAX_ON
,它只是rand()%4
)。
然后迭代MAX_ON
次,在每次迭代时选择一个随机中继(rand()%7
)。可能的中继重复不会使(可能)假设失效。
[更新]
类似(警告:未经测试)
for (;;)
{
int max_on = random( 4 );
int n;
for (n = 0 ; n< max_on; ++ n)
{
digitalWrite(random( 2 , 10 ),LOW);
}
延迟(随机( 1 , 31 )); // 等待一段时间(1到30秒之间)
}<小> [/更新] 小>
I have a code written below to control 8 relays in random. The program is for my Halloween props. I am new to coding. I only want to have the maximum of 3 relays operating at any one time in a random sequence. Any ideas on how I can achieve this problem.
Many Thanks in advance
#define time1 100
#define time2 3000
void setup()
{
pinMode(2, OUTPUT); //relay 1
pinMode(3, OUTPUT); //relay 2
pinMode(4, OUTPUT); //relay 3
pinMode(5, OUTPUT); //relay 4
pinMode(6, OUTPUT); //relay 5
pinMode(7, OUTPUT); //relay 6
pinMode(8, OUTPUT); //relay 7
pinMode(9, OUTPUT); //relay 8
delay(50); //Check that all relays are inactive at Reset
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
}
//--(end setup )---
void loop()
{
digitalWrite(random(2,10), LOW); // sets a random relay on
delay(random(time1,time2)); // wait for some time (between 1 and 30 seconds)
digitalWrite(random(2,10), HIGH); // sets a random relay off
delay(random(time1,time2)); // wait for some time (between 1 and 30 seconds)
}
What I have tried:
Tried re writing the code but was unsuccessful and the program froze.
解决方案
This is C# code, but should be easily converted to Arduino's C:
// Generate 20 values... for (int x = 0; x < 20; x++) { // Holds the byte value were turning bits on in. byte value = 0; // count holds the maximum number of bits were going to // enable this time around, up to maxBitsOnPerByte. // In your case, maxBitsOnPerByte should be 3. // RNG is the random number class. 'Next' will return // an integer between 0 and the specified limit, EXCLUSIVE. // In your example, this means 0, 1, 2, and 3. int count = RNG.Next(maxBitsOnPerByte + 1); // Loop 'count' times, turning on a random bit in the byte. // Note, this CAN turn the same bit on multiple times! for (int y = 0; y < count; y++) { // Take a single bit (1) and shift it left a random // number (from 0 to 7) bits. Then OR this value // with the existing value we're tracking. // In C#, the value of an OR operation is an integer // that must be cast back to a byte. value = (byte)(value | (0x1 << RNG.Next(8))); } // Convert the value to a string or 1's and 0's, // padded with 0's of course. string binary = Convert.ToString(value, 2).PadLeft(8, '0'); Console.WriteLine(
"Value: {binary}"); }
As a simple approach, you might (randomly) choose how many relays could beON
at a given instant (sayMAX_ON
, it's justrand()%4
).
Then iterateMAX_ON
times, picking at each iteration a random relay (rand()%7
). A possible relay repetition would not invalidate the (could) assumption.
[Update]
Something like (warning: not tested)
for (;;) { int max_on = random(4); int n; for (n=0; n<max_on; ++n) { digitalWrite(random(2,10), LOW); } delay(random(1,31)); // wait for some time (between 1 and 30 seconds) }[/Update]
这篇关于8个随机继电器。在任何给定时间最多需要3个,我该怎样才能实现这个目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文