For my project, I want to design this situation as a code in Arduino.
I want to make a code to describe that integer I will increase until 127 with a specific time (like 50ms). I call this specific time as duration time.
Meanwhile, integer J will start rising a few milliseconds (defined as specific onset time (SOT)) after integer I starts rising. Both of integers have same duration time. But there is a difference between starting time.
Integer I will increase from 0 to 127. When I =127, the time should be 50ms.
Integer J will start increasing from 0 to 127 after a few milliseconds (SOT, for example 40ms) after integer I starts rising.
For this situation, how to make the code?
This is the my sample code.
unsigned long previousMillis = 0;
const long interval = 100;
int i = 0;
int j = 0;
bool i_decreasing = false;
bool j_decreasing = false; //
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
unsigned long currentMills = millis();
if(!i_decreasing&&i<127){
Serial.println("I");
Serial.println(i);
if (currentMills-previousMillis>=interval){
previousMillis = currentMills;
j++;
Serial.println(j);
}
if (i== 127){
i_decreasing = true;
}
}
if(i_decreasing && i>0){
I--;
j++;
Serial.println(i);
Serial.println(j);
if (j==127){
j_decreasing = true;
}
}
if(j_decreasing && j>0){
j--;
Serial.println("j");
Serial.println(j);
}
// put your main code here, to run repeatedly:
}
We need some additional information.
Post your sketch, well formated, with well-tempered comments and in so called code tags and a real schematic to see how we can help.
It doesn't need to look like a curve function. It is just an example of what I want to describe.
Furthermore, after reaching the max value, it will go back to 0.
This figure is exactly what I want to describe.
It doesn't matter whether it is a curve or a straight line.
For the question if anything makes that happen again,
first of all, I don't need to happen it again.
But if you can provide more sample code to repeat it, would you like to provide it also?
When I printed out the currentmicros and compare when I= 0 and i=127, it is not exact time (50ms). It is almost 3 seconds. Is this what I want to describe as I have shown before?
unsigned long IntervalInMicros = 50000ul / 127;
int DelaySteps = 40000ul / IntervalInMicros;
byte i = 0;
byte j = 0;
bool i_Going_Up = true;
bool j_Going_Up = true;
void setup()
{
Serial.begin(9600);
}
void loop()
{
unsigned long currentMicros = micros();
static unsigned long lastStepTime = micros();
Serial.println(String(currentMicros));
if (currentMicros - lastStepTime >= IntervalInMicros)
{
Serial.println(String(currentMicros));
lastStepTime += IntervalInMicros;
if (i_Going_Up)
{
if (i < 127){
i++;
Serial.println("i");
Serial.println(i);
}
else{
i_Going_Up = false;}
}
else
{
if (i > 0){
i--;}
}
if (!i_Going_Up || i > DelaySteps)
{
if (j_Going_Up)
{
if (j < 127){
j++;}
else{
j_Going_Up = false;}
}
else
{
if (j > 0){
j--;}
}
}
}
}
This is what I did. I thought that when printing out currentMicro at 127 and 0, the time difference should be 50ms. but it is not... I am just curious about it .