/*
Project Name:GUI stage_2 ver.1.0.1 arduino IDE contorl interface
Description:
Arduino waveform output to control motor motion
using arduino IDE Serial print plotter to display motor output value.
*setting the go up time and holding time more easy to use.
t1 go up and down
t2(holding time)
Copyright (CC) 2024, Ryan Lin.
Date:2024/7/5
*/
//================================================================================waveform Table Stage setup====================================
const int sampleNum = 4; //-------------step3
int waveTable[sampleNum]{80, 80, 0, 0}; //-----------------------------step4
//=================================================================================================================================================
//-----------------------------------1
unsigned long previousMillis_WT = 0;
int num = 0;
int num_next = num+1;
float waveTable_output = waveTable[0];
float m = 0;
unsigned int T = 2000;
unsigned int holding_time = 2000; //-----------------------------------------------------step2
unsigned long previousMillis_WT_HT = 0; //hholding time
unsigned long currentMillis_WT_HT = 0;
//-----------------------------------
// --------------------debug
float value = 0;
// --------------------
int waveFormTable1();
int waveFormTable( int t1, int t2){
m = 10*fabs(waveTable[num_next]-waveTable[num])/ t1;
//if next point is same level adding maintenance time
if(waveTable[num] == waveTable[num_next]){
currentMillis_WT_HT = millis();
if(currentMillis_WT_HT - previousMillis_WT_HT > t2){ //*make sure the new step in the right timing
if(num < (sampleNum - 1)){ //go to next step value
num += 1;
if(num_next == (sampleNum -1)){
num_next = 0;
}else{num_next = num+1;}
}else{
num = 0;
num_next = num + 1;
}
previousMillis_WT_HT = currentMillis_WT_HT;
}
}else if(waveTable[num] > waveTable[num_next] && waveTable[num] != waveTable[num_next] ){ // "!=" function make sure dont go to the next step
if(waveTable_output <= waveTable[num_next]){
if(num < (sampleNum -1)){ //go to next step value
num += 1;
if(num_next == (sampleNum -1)){
num_next = 0;
}else{num_next = num+1;}
}else{
num = 0;
num_next = num + 1;
}
previousMillis_WT_HT = millis();
}else{
unsigned long currentMillis_WT = millis();
if(currentMillis_WT - previousMillis_WT > 10){
if((waveTable_output - m) <= waveTable[num_next]){
waveTable_output = waveTable[num_next];
}else{
if (waveTable_output >= m) {
waveTable_output -= m;
} else {
waveTable_output = 0; // 避免负值
}
}
previousMillis_WT = currentMillis_WT;
}
}
// previousMillis_WT_HT = millis(); //*make shure the new step in the right timing
}else if(waveTable[num] < waveTable[num_next] && waveTable[num] != waveTable[num_next] ){
if(waveTable_output >= waveTable[num_next]){
if(num < (sampleNum -1)){ //go to next step value
num += 1;
if(num_next == (sampleNum -1)){
num_next = 0;
}else{num_next = num+1;}
}else{
num = 0;
num_next = num + 1;
}
previousMillis_WT_HT = millis();
}else{
unsigned long currentMillis_WT = millis();
if(currentMillis_WT - previousMillis_WT > 10){
if((waveTable_output + m) > waveTable[num_next]){
waveTable_output = waveTable[num_next];
}else{
waveTable_output += m;
}
previousMillis_WT = currentMillis_WT;
}
}
}
return waveTable_output;
}
void setup() {
Serial.begin(115200);
}
void loop() {
// int output1 = map(waveFormTable(7, holding_time), 0, 100, 0, 255);
// Serial.print("output1:");
Serial.print(waveFormTable(T, holding_time));
// Serial.print(waveFormTable1(T, holding_time));
Serial.print(",");
// ----------debug
Serial.print(m);
Serial.print(",");
Serial.print(waveTable[num]);
Serial.print(",");
Serial.print(waveTable[num_next]);
// Serial.print(",");
// Serial.print(abs(waveTable[num_next]-waveTable[num]));
// -----------
//----------------
Serial.println(",");
//----------------
// float a = 10*fabs(0 - 80)/10000;
// value = value += a;
// if(int c = 10 < value){
// Serial.println("P");
// }
// delay(100);
// Serial.print(value);
// Serial.print(",");
// // Serial.print(c);
// // Serial.print(",");
// Serial.println(a);
}