Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Arduino Poor man's oscilloscope
#define ANALOG_IN 0
void setup() {
void loop() {
int val = analogRead(ANALOG_IN);
Serial.write( 0xff );
Serial.write( (val >> 8) & 0xff );
Serial.write( val & 0xff );
Copy link

HKH13 commented Nov 27, 2015

That's cool project
But why u shift and
Why u withe 0xff

Copy link

@HKH13 Serial.write only works with bytes, so to send larger values (int is 16 bit) you need to split them up into their components. The first 0xff is likely just for synchronisation and not essential. Then the higher 8 bits are sent by shifting them right until they fit into a byte, and finally the lower 8 bits. For example, let's say val = 1342 (that is actually too high, analogRead only returns values between 0 and 1023, but whatever). 1342 is 00000101 00111110 in binary. So the shift and truncation yields 00000101, and val & 0xff is just the lower byte 00111110 which can then be sent piece wise and reassembled on the other side.

Copy link

tcedar commented Apr 7, 2019

Thanks @stephanmantler , that's really helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment