I am trying to make an arduino project with arduino ide and processing ide.
I started doing a simple test to see the enviornment where I display the numbers 0,1..9 using arduino ide, but processing ide doesn't read it right for some reason and I can't figure out why, it reads weird numbers from serial like 10, 13, 53 and so on (only these numbers, nothing changes). Here is my processing code:
import processing.serial.*;
Serial port;
void setup() {
port = new Serial(this,"/dev/ttyUSB0",9600);
}
void draw() {
if(port.available() > 0) {
int info = port.read();
println(info);
println("===");
}
}
And here is my arduino code:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int deg = 0;
int data = 1;
for (deg = 0; deg < 10; deg++) {
Serial.println(deg);
delay(15);
delay(1000);
}
}
Also, the board and processing are using the same port /dev/ttyUSB0.
I am running all of this on Ubuntu 20.04. I tried to look on google but can't seem to find anything.
Thanks in advance, any tip is welcome.
Your are sending ASCII from Arduino and reading binary in your Processing IDE. Here is what your sender is doing:
for (deg = 0; deg < 10; deg++) {
Serial.println(deg);
...
}
Serial.println prints the value, meaning it's formatted for display. That means it's converted to ASCII. The output of this will be each number, in ASCII, followed by a new line (thus the 'ln' in the println function):
48 10 13 49 10 13 50 10 13 ... 57 10 13
For example, Serial.println(0) will yield 48 10 13 which is the ASCII code for 0 followed by the new line sequence 10 13 (CR and LF).
Your receiver is doing this:
int info = port.read();
println(info);
Which will read these values as integers and format those numbers as ASCII outputs with new lines. So you will see on your display:
48
10
13
...
The best way to solve this is to write binary data from Arduino instead of printing the data. On your Arduino, use Serial.write() instead:
for (deg = 0; deg < 10; deg++) {
Serial.write(deg);
...
}
Related
I working one a small Java project controlling a DMX-light, a “moving head”.
So far, I can control the moving head using Art-Net, doing the most simple stuff – like setting a value for a channel.
Now I’m working on moving the moving head. The moving head supports 16bit and this troubles me. I know that the 16bit comes from using two “channels”. 1 channel is one byte, 8 bits, so the maximum value for 1 channel is 255. When using two channels the maximum increases to 65.356 which will give much more precise movement on the pan- & tilt-channel. So, a 16bit moving head uses 4 channels for controlling pan and tilt.
Now I want to create a simple movement generator in Java. Simple looping the pan- and tilt-value from 1 to 255. If I understand it correct, when using 16bit moving head, it means, first setting the pan “fine” (pan only in this example) from 1 to 255 and when pan “fine” reaches 255 then increasing pan “course” with 1, like this:
Pan “fine” 1->255, when reaching 255 increase pan course with 1 then again Pan “fine” 1->255, when reaching 255 increase pan course with 1 and then again Pan “fine” 1->255, when reaching 255 increase pan course with 1, and so on.
I need to have some kind of delay in the movement generator and I can see on various places that it is best to avoid Thread.sleep() (and while loops) as it can drift, so I have decided to built the generator in its own method and then call this method with an Executor Service (scheduleAtFixedRate), like this:
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) {
test test = new test();
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleAtFixedRate(test::run, 0, 10000000, TimeUnit.NANOSECONDS);
}
}
“run” is the method with the generator and “test” is the class containing the method. That part is working well.
This is the method in test which should serve as the movement generator:
public class test {
int start = 1;
int end = 255;
int counterFine = 1;
int counterCourse = 255;
boolean startOverCourse = false;
public void run() {
if (!startOverCourse) {
counterFine++;
}
if (startOverCourse) {
counterFine--;
}
if (counterFine == end) {
counterCourse++;
}
if (counterCourse == end) {
startOverCourse = true;
counterFine = end;
}
}
}
Yes, I know it is not much to give you, sorry for that, but I can't figure out what to do now and the reason is this:
When “course” reaches (I starts at 1, and increases with 1 when “fine” reaches 255) 255, course will need to start to count down, 255 becoming 254 and so on. But, that will also mean that when course reaches 255 then fine also have to decrease, going from 255-> (before it was 1->255).
Maybe some of you guys can figure out what I want to archive, maybe I’m doing it complete wrong, then please tell me how to approach it.
So, in Java, how to do a simple Pan & Tilt movement generator using 16bit. I know how to do it in 8 bit, using two channels, but I don’t know how to include the fine-channels and making it 16bit.
Kind Regards 😊
EDIT:
This is what my test class look like now:
public class test {
DmxValues dmxValuesOBJ;
SendArtnet SendArtnet;
int start = 1;
int end = 255;
int counterFine = 1;
int counterCourse = 1;
boolean startOverCourse = false;
test(DmxValues obj1, SendArtnet obj2) {
dmxValuesOBJ = obj1;
SendArtnet = obj2;
}
public void run() {
dmxValuesOBJ.setdmxvalues(4 - 1, counterFine);
dmxValuesOBJ.setdmxvalues(2 - 1, counterFine);
SendArtnet.testSend();
if (counterFine == end && !startOverCourse) {
dmxValuesOBJ.setdmxvalues(3 - 1, counterCourse);
dmxValuesOBJ.setdmxvalues(1 - 1, counterCourse);
SendArtnet.testSend();
counterCourse++;
}
if (counterFine == end && startOverCourse) {
dmxValuesOBJ.setdmxvalues(3 - 1, counterCourse);
dmxValuesOBJ.setdmxvalues(1 - 1, counterCourse);
SendArtnet.testSend();
counterCourse--;
}
if (counterFine == end) {
counterFine = start;
}
if (counterCourse == end) {
startOverCourse = true;
}
if (counterCourse == start) {
startOverCourse = false;
}
System.out.println(counterFine);
counterFine++;
}
}
SendArtnet and DmxValues are classes for sending and containing the dmx-data, should not be relevant, it's there so I can see in my dmx-monitor what is going on.
Above test class does what I want when it comes to the course-value (counterCourse), goes up to 255 and down to 1. The issue is I can not figure out how to do the same with the fine value (counterfine) - now it runs in one direction (from 1 to 255) but I need it to change direction when countercourse reaches 255, from 255 to 1.
I hate all those "if" - any smarter way to do all this?
Conceptually, you're trying to smoothly ramp a single 16 bit value (between 0 and 65535), and the two 8-bit values are simply a way of expressing that data in a way that works with DMX512. Trying to express the ramping/counting in terms of separate coarse and fine counters is complicating the logic significantly since you're essentially implementing your own arithmetic carry, in a way.
Keep a single counter, sweep it between 0 and 65535 (or your range of interest), and convert it at each step: the coarse part is counter / 256, and the fine part is counter % 256.
In the below code when i enter the input as***(i just don't enter the input one by one instead i copy and paste the entire input)***
4
101
1111
00110
111111
i am supposed to get
5
15
6
63
instead i get
5
15
6
and after i press enter here
i get the 63
import java.util.Scanner;
public class VonNeumanLovesBinary {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int no = scn.nextInt();
while (no > 0)
{
int binary = scn.nextInt();
int i = 0 ;
int sum = 0;
while(binary > 0 )
{
int digit = binary % 10;
sum += Math.pow(2,i) * digit ;
binary /= 10;
i++;
}
System.out.println(sum);
no--;
}
}
}
This code is written Intellij IDE.
Please help me out . this is the problem of the ide ?
Its not the fault of the IDE. It is how it deals with copy-pasting text into the console.
The text is handed over to the Java code as you paste it, line by line. Input is only handed over once you finish the line. The previous lines are all terminated already with a newline symbol but the last line is not.
So you have to either add a newline to the end of your copy-pasta or hit enter to produce one yourself. So before you terminate the last line, the last line is never handed over to Java but is still only in the console.
For example, if you copy pasta this instead:
4
101
1111
00110
111111
(note the last, empty line)
it will work as expected, since you finished the 111111 line, i.e. it is 111111\n and not just 111111.
My homework is to create a program that takes a list of numbers and prints out the highest number divisible by four.
List would look like this:
12
16
87
58
25
73
86
36
79
40
12
89
32
Input should be:
40 because it is the highest number there divisible by four.
Here is my code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int saved = 0;
int saved2 = 0;
for (int i = scanner.nextInt(); i % 4 == 0; i = scanner.nextInt()) {
for (boolean bull = true; bull == true; bull ^= true) {
if (i > saved) {
saved -= saved2;
saved += i;
saved2 += i;
}
}
System.out.println(saved);
}
}
}
The input of my code is
12
16
I don't really understand why this is doing it, but it seems to me that I'm adding the variables wrong. The homework page on adding variables does not specify how to add variables to each other.
Does anyone have a tip to improve the code in anyway, or find a way to make a fix my code? Thank you.
welcome to Java.
First you are saying you got input, but that is output. Input is what you enter, and output is what you get printed.
Then there is a mistake in your for loops. You have too much going on in one place. By the logic which is implemented, your program will exit first level for loop whenever your entered value is not divisable by 4.
Read on for loops if you want to learn more https://www.learnjavaonline.org/en/Loops.
I recommend to start from while loops instead. The logic whould be this:
1. create variable to hold the correct answer saved
2. create another one to hold the value read from console i
3. start the while loop with condition i = scanner.nextInt()
3.1 check if the value just entered i is divisable by 4
3.2 if it is, then compare if it's larger than the one was saved before (initially saved value will be 0)
3.3 if it is larger, then assign the read value i to the saved
4. At the end of the loop, you will have the highest number divisable by four in your saved variable. Print it.
I will provide some help, according to
How do I ask and answer homework questions?
for (int i = scanner.nextInt(); i % 4 == 0;i = scanner.nextInt())
This only reads as long as ALL inputs are divisible by 4, that is why it ends at 16, because 87 is not divisible by 4.
for (boolean bull = true; bull == true ;bull ^= true)
This needs explanation by you, but I am pretty sure that it unconditionally executes the body of the inner loop exactly once. (Not 100% sure, because the representation of true and false could be weird in your machine. Should 0 be the representation of true, i.e. really weird, then it is an endless loop, which does not match the output you describe...)
System.out.println(saved);
This executes exactly once per input, except the last one, which is not a multiple of 4.
The value of saved is identical to input, as long as it is increasing.
These hints explain the unexpected output.
If you inspect the details of what the problem is, you should be able to improve your coding attempt.
This is how I super-quickly fixed in your code.
Note that there are no statements about the possible minimum value and about how do you stop the input. Therefore the solution is pretty-straightforward, it just reads the input until integers are present there.
This article may be useful about handling the input from the Scanner.
I hope the comments in the code will help. Add comments if there are any questions. Good luck!
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int currentMax = Integer.MIN_VALUE; // you may set negative or 0 if you know that all the input is positive
// int saved2 = 0; // no need for this variable
while (scanner.hasNextInt()) { // you can make a better input handling, especially if you know when it should end the input. Now it will end on any non-integer input line
int i = scanner.nextInt();
// for (int i = scanner.nextInt(); i % 4 == 0; i = scanner.nextInt()) {
// for (boolean bull = true; bull == true; bull ^= true) {
if (((i % 4) == 0) && (i > currentMax)) {
currentMax = i;
// saved -= saved2;
// saved += i;
// saved2 += i;
// }
}
}
System.out.println(currentMax); // moved out of "for" or "while" cycles. Print the value after the input has ended.
}
}
I have a program with one class, which looks like this.
public class Functions {
public static void main(String[] args) {
System.out.println(summationFunction(1)); //Prints 13
System.out.println(summationFunction(2)); //Prints 29
System.out.println(summationFunction(3)); //Prints 48
System.out.println(summationFunction(4)); //Prints 70
}
public static int summationFunction(int input) {
int summedNumber = 0;
int i = input;
while (i > 0) {
summedNumber += i * 3;
i--;
}
return 10 * input + (summedNumber);
}
}
So, this program will take in a given number and apply this function to it:
And this all works well (I have run the class Functions and everything prints just as it's supposed to.) BUT, I need to find the inverse of this function, and I need to be able to translate it to code; I do not know how to do this.
I basically need a function that will return values like this:
public static void main(String[] args) {
System.out.println(summationFunction(13)); //Prints 1
System.out.println(summationFunction(29)); //Prints 2
System.out.println(summationFunction(48)); //Prints 3
System.out.println(summationFunction(70)); //Prints 4
}
which, (as you can tell) is the opposite of the original function.
So to sum everything up, I need a function that will return the inverse of my original function (summationFunction), and I would like to know how I would model this or if there is a quick solution, in code.
One more thing: I know that I can have the method take an input and search for the most similar output of the original method, but I would like to see if there is a simpler way to do this which does not involve searching, thus giving a quicker output speed. And if you wish you can safely assume that the input of the inversed function will always be a number which will give an integer output, like 13, 29, 48, 70, etc...
By the way, if you are going to downvote the question, will you at least give a reason somewhere? The comments perhaps? I can not see any reason that this question is eligible for being downvoted, and a reason would help.
Wolfram Alpha to the rescue !
It tells you that this function can be written as :
1/24*(6*x+23)^2-529/24
So if you want to solve f(x)=a, you have :
x = 1/6*(sqrt(24*a+529)-23)
a = 70
# => x = 4
Note : Using Wolfram shouldn't prevent you from finding the answer on your own.
sum(something*i) is equal to something*sum(i) because something (3 in this case ) doesn't depend on i.
sum(i,i=1..n) is equal to n*(n+1)/2, and it's easy to prove (see Wikipedia)
So your function becomes 10*x+3*x*(x+1)/2
Expanded, it is :
(3 x^2)/2+(23 x)/2
You need to solve (3 x^2)/2+(23 x)/2 = 70, in other words :
(3 x^2)/2+(23 x)/2 - 70 = 0
It is a quadratic equation, with a=3/2, b=23/2 and c=-70 or c=-29 or c=....
You sum can be written like this 3*x*(x+1)/2 so you have equation 10*x + 3*x*(x+1)/2 = y you need to solve it.
Wolfram alpha tells that result will be 1/6.0 * (-23.0+sqrt(529.0+24.0 * y))
I got a series like this:
20 22 25 27 30 31 30 25 22 19 21 25 28 30 28 27...
As soon as the numbers reach near 30, they start moving negatively, and as soon as they reach near 20, they start moving positively.
I need to find these 2 points using some sort of algo. I'm totally lost.
I can't just do a sort because then I get 31 as max and 19 as min.
In real implementation, the numbers can change, and can be Float as well, instead of just int. It can be something like this:
55.20 57.35 54.30 59.25 61.00 58.20 55.40 53.50 58.75 60.10 55.15 53.40 50.00 51.10 52.00
In this case 53 and 60 are the points, and additionally, a third lower point 50.00.
How would I go ahead on this?
import java.util.List;
import java.util.ArrayList;
public class GetExtrema {
public static <T extends Comparable<T>> List<T> getExtrema(T[] series) {
List<T> extrema = new ArrayList<T>();
extrema.add(series[0]);
boolean upElseDown = series[1].compareTo(series[0]) > 0;
for (int i = 2; i < series.length; ++i) {
if (series[i].compareTo(series[i-1]) > 0 != upElseDown) {
extrema.add(series[i-1]);
upElseDown = !upElseDown;
} // end if
} // end for
extrema.add(series[series.length-1]);
return extrema;
} // end getExtrema()
public static void main(String[] args) {
Integer[] s1 = {20,22,25,27,30,31,30,25,22,19,21,25,28,30,28,27};
List<Integer> extrema = getExtrema(s1);
System.out.println(extrema);
Double[] s2 = {55.2,57.3,54.3,59.2,61.,58.2,55.4,53.5,58.7,60.1,55.1,53.4,50.,51.1,52.};
List<Double> extrema2 = getExtrema(s2);
System.out.println(extrema2);
System.exit(0);
} // end main()
} // end class GetExtrema
Compilation and execution:
javac GetExtrema.java;
CLASSPATH=. java GetExtrema;
## [20, 31, 19, 30, 27]
## [55.2, 57.35, 54.3, 61.0, 53.5, 60.1, 50.0, 52.0]
If this is not a homework assignment and you can use outside libraries, consider something like Apache Commons StatUtils min function. There is a corresponding max function also. This particular library expects doubles, there should be something similar for floats.
If this is a homework assignment and the task at hand is to learn how to develop an algorithm, avoid sorting. It is not necessary. A simple loop and two tracking variables for min and max will do. For each iteration over the series, check the value and check if:
the current value is less than min, if it is set min to the current value
the current value is greater than max, if it set max to the current value