Basically, I made a program in processing with a knob and slider and everything.
And I am sending the values of the knob and slider to my Arduino.
My Arduino should set the blinking time when it reads 'n'.
When it reads 'k', know that the value it reads after it, is the time that the LED is turned on.
And when it reads 's', know that the value it reads after it, is the time that the LED is turned off.
It should also, at the same time all of this happens, send the value of a sensor to processing (that part works).
I have been struggling with this for a few weeks now, but I can not get it to work. This is the code I have made so far and I just do not know what is wrong and how I can fix it.
int analogPin = A0;
int SensorVal = 0;
char val; // Data received from the serial port
int OnTimeString;
int OffTimeString;
int OnTime;
int OffTime;
String inStringOn = "";
String inStringOff = "";
const int ledPin = LED_BUILTIN; // Set the pin to digital I/O 1
int ledState = HIGH; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
void setup() {
pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
int StartTime = millis();
do {
SensorVal = analogRead(analogPin); // read the input pin
Serial.println(SensorVal); // debug value
}
while(Serial.available());
if(Serial.available()){ // If data is available to read,
if (Serial.read() == 'n'){
if (Serial.read() == 'k'){ // if k (= knob is changed) is recieved
// value send after should control the time the led is HIGH
OnTimeString = Serial.read();
if (isDigit(OnTimeString)) {
inStringOn += (char)OnTimeString;
}
if (OnTimeString == '\n'){
OnTime = inStringOn.toInt();
}
}
if (Serial.read() == 's'){ // if s (= slider is changed) is recieved
// value send after should control the time the led is LOW
OffTimeString = Serial.read();
if (isDigit(OffTimeString)) {
inStringOff += (char)OffTimeString;
}
if (OffTimeString == '\n'){
OffTime = inStringOff.toInt();
}
}
}
}
unsigned long currentMillis = millis();
if (currentMillis - StartTime >= OnTime) {
ledState = LOW;
digitalWrite(ledPin, ledState); // set the LED with the ledState of the variable
}
if (currentMillis - StartTime <= OnTime){
ledState = HIGH;
digitalWrite(ledPin, ledState);
}
}
Read the entire input and then parse the parts that you want.
I looked at all off the examples, but I am fairly new to programming.
Those codes seem so complicated and they have almost no comments in the code so I have no clue wat is going on.
It has been very difficult for us to understand what you have wanted to convey through your post. If I would be the poster, I would be saying the following:
I wish to create a sketch for Arduino UNO which will control the following events.
(1) I have a knob (a variable resistor type sensor feeding analog voltage) connected at A0-pin of Arduino UNO.
(2) I have a slider (another variable resistor type sensor feeding analog voltage) connected at A1-pin of UNO.
(3) The units of 'ON-period' and 'OFF-period' of the built-in LED (L) of UNO are ms.
(4) The UNO will set the ON/OFF period of L when it receives the character n from the Serial Monitor of UNO.
(5) The 'ON-period' of L will be proportional to the value that the UNO reads form the knob.
(6) The 'OFF-period' of L will be proportional to the value that the UNO reads form the slider.
(7) The UNO will be continuously reading the input values of the sensors (the knob and the slider) and will print them on the Serial Monitor.
If you think that the above statements are close to what you have wanted to say in your post, then we may try to help you by modifying your posted codes. If not, please rewrite your post clearly so that we can understand your purposes.
It has been very difficult for us to understand what you have wanted to convey through your post. If I would be the poster, I would be saying the following:
I wish to create a sketch for Arduino UNO which will control the following events.
(1) I have a knob (a variable resistor type sensor feeding analog voltage) connected at A0-pin of Arduino UNO.
(2) I have a slider (another variable resistor type sensor feeding analog voltage) connected at A1-pin of UNO.
(3) The units of 'ON-period' and 'OFF-period' of the built-in LED (L) of UNO are ms.
(4) The UNO will set the ON/OFF period of L when it receives the character n from the Serial Monitor of UNO.
(5) The 'ON-period' of L will be proportional to the value that the UNO reads form the knob.
(6) The 'OFF-period' of L will be proportional to the value that the UNO reads form the slider.
(7) The UNO will be continuously reading the input values of the sensors (the knob and the slider) and will print them on the Serial Monitor.
If you think that the above statements are close to what you have wanted to say in your post, then we may try to help you by modifying your posted codes. If not, please rewrite your post clearly so that we can understand your purposes.
That is exactly what I want!! I am so sorry for being unclear!
Hopefully, you can help me. Thank you
Now, you have to follow SSS (Small Start Strategy) approach to develop program for your project.
(1) Connect your sensor at A0-pin and A1-pin. Create a sketch to read their values and display them on the Serial Monitor. Add codes in your sketch so that there is a check if any character is coming from the Serial Monitor. If there is no character from the Serial monitor, only then the values of the sensors will be printed on the Serial Monitor. Please post your codes (whether wrong or right), we will help you to correct your codes. Also, please attach the screenshot of the Serial Monitor so that we can see what is being printed over there in response to your program.
BTW: We will not make a new post; further instruction will be given through this post via 'post modification' option. You also do not make separate post for each reply.
This is the program for you which (I believe) you will be able to follow. Upload the program; change the value of Knob and Slider; issue n, k, s commands as needed from the Serial Monitor; observe that ON/OFF period of L changes. (This is an un-optimized tested program.) Please, modify the program as needed to suit your requirements.
(2) So I changed the code you gave me, but it does not work
int sK = 0;
int sS = 0;
bool flag1 = LOW;
void setup(){
Serial.begin(9600);
pinMode(13, OUTPUT); //built-in LED L
digitalWrite(13, LOW);
analogReference(DEFAULT);
}
void loop(void) {
if (flag1 != HIGH){
if (Serial.available() == 0){
byte x = Serial.read();
if (x == 'n'){
flag1 = HIGH;
}
}
} else {
if (Serial.available() > 0){
byte x1 = Serial.read();
if (x1 == 'k'){
sK = analogRead(A0);
ledControl();
} else {
if (x1 == 's'){
sS = analogRead(A1);
ledControl();
}
}
}
}
}
void ledControl(){
int mapK = map(sK, 0, 1023, 0, 5000); //0ms - 5000ms
int mapS = map(sS, 0, 1023, 0, 5000); //0ms - 5000ms
digitalWrite(13, HIGH);
delay(mapK);
digitalWrite(13, LOW);
delay(mapS);
flag1 = LOW;
}
This is the program for you which (I believe) you will be able to follow. Upload the program; change the value of Knob and Slider; issue n, k, s commands as needed from the Serial Monitor; observe that ON/OFF period of L changes. (This is an un-optimized tested program.) Please, modify the program as needed to suit your requirements.
bool flag1 = LOW;
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT); //built-in LED L
digitalWrite(13, LOW);
analogReference(DEFAULT);
}
void loop()
{
if (flag1 != HIGH)
{
if (Serial.available() == 0)
{
Serial.print("Printing Knob's Input = ");
Serial.println(analogRead(A0), HEX);
Serial.print("Printing Slider's Input = ");
Serial.println(analogRead(A1), HEX);
Serial.println("=======================");
}
else
{
byte x = Serial.read();
if (x == 'n')
{
flag1 = HIGH;
}
}
}
else
{
if (Serial.available() > 0)
{
byte x1 = Serial.read();
if (x1 == 'k')
{
ledControl();
}
else
{
if (x1 == 's')
{
ledControl();
}
}
}
}
delay(1000);
}
void ledControl()
{
int sK = analogRead(A0);
int mapK = map(sK, 0, 1023, 0, 5000); //0ms - 5000ms
int sS = analogRead(A1);
int mapS = map(sS, 0, 1023, 0, 5000); //0ms - 5000ms
digitalWrite(13, HIGH);
delay(mapK);
digitalWrite(13, LOW);
delay(mapS);
flag1 = LOW;
}
I found out what my problem is;
I used the wrong USB port, my computer has one USB port that is kind of broken, when I plugged it into another one it worked just fine.
The program is working I used all of the tips and code. Thank you guys very much!!!!