flyingbrick

Arduino stuff/ programing/so cheap

Recommended Posts

Once I've got all of my features locked in I want to design a circuit board that incorporates everything. (Except for Teensy which can still just solder on top)
So looks a bit tidier than a big jumble of shit everywhere.
I like the protection aspect & nerd factor of opto isolators.
They're really cheap anyway.

Just back to the drawing board for the GUI at the moment.

  • Like 2

Share this post


Link to post
Share on other sites

Long time luser first time caller.
I can't get it to react to some simple input from sensors if i have more than one sensor. All i want it to do is output for a certain amount of time, possibly adjusted to the speed of the input signals, while scanning for new inputs but if i add more than one input, nothing happens.
I think it's in my code.

Share this post


Link to post
Share on other sites
23 hours ago, NickJ said:

Can you post up the code and circuit as it stands?

const int sensorPin1 = A1;
const int sensorPin2 = A2;
const int sensorPin3 = A3;
int sensorValue = 0;

int sensorMin = 1023;
int sensorMax = 0; 

void setup() {
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
}

void loop() {
  if (sensorPin1 >= 900);
  {
    pinMode(2, HIGH);
    delay(200);
    pinMode(2, LOW);
  }
  else if (sensorPin1 <= 900);
  {
    pinMode(2, LOW);
  }
  if (sensorPin2 >= 900);
  {
    pinMode(3, HIGH);
    delay(200);
    pinMode(3, LOW);
  }
  else if (sensorPin2 <= 900);
  {
    pinMode(3, LOW);
  }
  if (sensorPin3 >= 900);
  {
    pinMode(5, HIGH);
    delay(200);
    pinMode(5, LOW);
  }
  else if (sensorPin3 <= 900);
  {
    pinMode(5, LOW);
  }
}

 

Proof i'm not a coder.

 

  • Like 1

Share this post


Link to post
Share on other sites

I ran it through another editor, to get thn indenting more helpful, and added the digitalWrite you need

 

const int sensorPin1 = A1;
const int sensorPin2 = A2;
const int sensorPin3 = A3;
int sensorValue = 0;

int sensorMin = 1023;
int sensorMax = 0; 

void setup() {
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
}

void loop() {
  if (sensorPin1 >= 900);
  {
    digitalWrite(2, HIGH);
    delay(200);
    digitalWrite(2, LOW);
  }
  else 
	if (sensorPin1 <= 900);
	{
		digitalWrite(2, LOW);
	}		
  if (sensorPin2 >= 900);
  {
    digitalWrite(3, HIGH);
    delay(200);
    digitalWrite(3, LOW);
  }
  else 
	  if (sensorPin2 <= 900);
  {
    digitalWrite(3, LOW);
  }
  if (sensorPin3 >= 900);
  {
    digitalWrite(5, HIGH);
    delay(200);
    digitalWrite(5, LOW);
  }
  else if (sensorPin3 <= 900);
  {
    digitalWrite(5, LOW);
  }
}

there's great tutorial and refernces at https://www.arduino.cc/en/Tutorial/HomePage 

and a language reference at https://www.arduino.cc/reference/en/

 

Also, have a look at the built in examples: especially             File - Examples - 01.Basics - Blink

  • Like 2

Share this post


Link to post
Share on other sites

Also, loop happens really fast, so if signal 1 is > ~4V, and the other ones are low, then output pin 2 is going to 99.99% stay high, not blink.

If you tell us what you're making, I could show you some pretty ways of making the code, but go have a look at the examples first :)

  • Like 1

Share this post


Link to post
Share on other sites

Found this which is cool:

https://www.aliexpress.com/item/HC-05-master-slave-6pin-JY-MCU-anti-reverse-integrated-Bluetooth-serial-pass-through-module/32688852890.html

A bluetooth to Serial board. 

So you can connect your Arduino to something with bluetooth, then transmit as though it were any other UART serial connection to the Arduino.

my little brother has been saying it's super easy to read/write from bluetooth via a webpage so no need to learn Android app writing or any shit like that.
So you could load a webpage on a phone then send/receive data. Cool.
I've been thinking its a good way to have a config file thats easy to edit.
Fill out a form on a page, then transmit to arduino and store settings in EEPROM.
Instead of recompiling each time just to update a setting or whatever.

Also this is cool. Probably fairly pointless, but, cool all the same. So that ticks all my boxes
 

 

  • Like 1

Share this post


Link to post
Share on other sites

everyone uses those HC-05's and they work. They are shit, but they work :)

Problem with serial is dealing with fuck ups and lost bytes or even just bits... which for a personal project is hardly a problem as it's not a big deal 99% of the time, but if you want to send data that sets up your project which then in turn controls your ECU, you wanna make sure it gets all the data correctly, or else something like "set timing to 38 deg" can turn into "set timing to 110 deg" by the serial timing just being SLIGHTLY off... and serial timing is always slightly off as everyone uses 115200 BAUD, which an arduino cant do with 100% accuracy (it gets plenty close enough as long as it has a crystal, which i think all arduinos have)

but yeah, good idea, and do it! I can send you somne ideas on how to packetise data etc if arduino doesnt already have a decent example (which im guessing they probably do)

 

also, roman, this guys is 100% you and you need to watch this!

 

  • Like 1

Share this post


Link to post
Share on other sites

Ahhh thanks Ned, hadnt thought about any of that.

Maybe a good scheme for working around potential dataloss is to send a few strings with the same data in different order.
So send a string SettingA, SettingB, SettingC, SettingD
Then SettingD, SettingA, SettingB, Setting C
Then Setting C, SettingD, SettingA, Setting B

Then doing a comparison to make sure all of the SettingA results are identical. and if not ignore the results and send again.

Not good if you're trying to push shitloads of info through I guess, but I'd want to be sending just a handful of data and not very often.
 

Share this post


Link to post
Share on other sites

Also my code is getting pretty bloated again so I've been looking to library-ize various portions of code. 

How does this work though with data that is shared between different parts of my code though?

Like is it best to keep all of the data in a library completely internal? Apart from what you put in, and take back out?
Like for example if I have an array that a library wants to look up, but other things still look at. 
Is it best to keep that array in the library or in the main code?

I guess the whole point of a library is that its quite self contained.
This makes sense but there's a bit of untangling to do as all my code runs through itself a lot haha.
Rookie mistakes.

And I just broke my LCD screen so its a few weeks until another one gets here. Maybe a good time to fix all the code instead haha.

Share this post


Link to post
Share on other sites
2 hours ago, Roman said:

Also my code is getting pretty bloated again so I've been looking to library-ize various portions of code. 

How does this work though with data that is shared between different parts of my code though?

Like is it best to keep all of the data in a library completely internal? Apart from what you put in, and take back out?
Like for example if I have an array that a library wants to look up, but other things still look at. 
Is it best to keep that array in the library or in the main code?

I guess the whole point of a library is that its quite self contained.
This makes sense but there's a bit of untangling to do as all my code runs through itself a lot haha.
Rookie mistakes.

And I just broke my LCD screen so its a few weeks until another one gets here. Maybe a good time to fix all the code instead haha.

what do you mean dave? rookie never makes mistakes

  • Like 1

Share this post


Link to post
Share on other sites

ok, few things then :) Keep in mind i dont really do 'arduino' as such, so not 100% sure how this works, but im gonna assume its pretty similar to 'real world' C coding

first, serial stuff, the best way is to add a checksum to the data. To do that, you basically need a way to say "this is a new bunch of data", "here comes the checksum" and "this is the end" and then the code knows when it starts and ends and where to find the checksum. The easiest way to do this (in my opinion) is to use the way GPS NMEA strings do it. They start with a '$', place the checksum after '*' and end with '\r\n' (carriage return and line feed, or simply 'enter')
So that means you cant sendthose things as data in the string, but how often do you need to send $ and * right?
I like that way as it means your data is totally still readable in a serial console, and you can find online details and calculators to check that your checksum is right. Plus the checksum is a simple XOR of the data i believe, so super easy and fast for the micro... Not 100% reliable, as 2 bits of error could cancel out, as well as some data missing isnt picked up necessarily (ask me how i know) so there is better again, but gets harder. The NMEA one is pretty good most the time though!
you can make it better by a) sending small packets, and b) adding a length to it so you know you got everything correctly. It's now no longer a 'standard' method, so calculators no longer work, but will make the code better and still readable

A better way is going full big dick swinging binary... so no more human readable data, which is a cunt when you're tyring to debug (but good coders dont need to debug right? as all their shit works first time? :rolleyes::rolleyes::rolleyes::rolleyes: )
so you basically need the same way of being able to say "Start" and "End", but you now use binary data (0-255) so you need to pick a number. Standard number is 0x02 for start, and 0x03 for end. But that means you cant send those 2 bytes as data anymore, because the code will think you are starting or ending a packet instead of sending data. Follow? So they use an escape character... 0x10 is standard here again. So what you do is, when you receive 0x10, you just assume that whatever comes next is data, not a command for start or stop or anything.
so say you want to send start, 0x01, 0x02, 0x03, End, you send 0x02, 0x01, 0x10, 0x02, 0x10, 0x03, 0x03. Follow still? same goes for sending 0x10, you send 0x10, 0x10.
you also want to add a length and CRC to it. So you just make a packet where the first byte after the start is the length, and the last byte before end is the crc. This way you get pretty reliable data through the other end. Granted, if you go and send giant packets, the chances of fucking it up are still quite high... so send 'small' packets.

While we're at it, you now also need to tell the code what the data means. I use a command byte for this (or command word if you have lots of shit to send) so my packets look like this

[STX] [CMD] [LEN] ...[DATA]... [CRC] [ETX] 

where STX is start of transmission, CMD is command, LEN is length, Data is a variable length amount of data, CRC is a checksum byte of everything from CMD, LEN and DATA xored together and ETX is end of transmission.

Then command usually has a 'set' and a 'get' which i usually assign to the top bit (0x80 for byte and 0x80000 for word) so then i can have a list of commands (in an enum usually) that the code can then use. So e.g. enum commands { CMD_MIN = 1, CMD_MAX, CMD_AVG, CMD_SET = 0x80};
Then you can send CMD_MIN if you want to know what your min value is, opr you can send CMD_MIN | CMD_SET to signal you want top set the min value.

I have no idea if you followed all of this... it's a lot of fucking around! and i suck at complaining, especially when i cant post actual code for you to follow haha. So i hope that showed you something at least? maybe?

Now, next,

Libraries, libraries are great for stand alone things, but you can also tidy up things that arent stand alone. Regardless, libraries still need to work with outside world stuff, they likely never function within themselves and require either inputs or outputs of some description right?

actually, fuck it, i wrote a bunch of shit that was impossible to follow, and heres a google result haha. It explains is quite well :)
https://www.arduino.cc/en/Hacking/LibraryTutorial

So, now, sharing data

say you have a struct or array called data, which has a max, min and avg in it... and you have that in data.c or something. Now you want to change min from usart.c because thats where you got the data from, and you want to read it from the main loop as well to see if you should turn an LED on or something.

What you do is you tell the rest of the code you have a variable called data already by putting extern struct data; in the data.h file.
Now in the rest of the code, if you include data.h, it tells that copde there is a variable called data and that they can use it if they want. So you can now do stuff like  data.min = 10; in your usart.c code, or if (data.avg < 7) digitalWrite(1, HIGH); or whatever.

I know that explains it badly, but i hope it gave you enough info to know what to search for on google to get better answers lol

  • Thanks 1

Share this post


Link to post
Share on other sites

Thanks Ned. 

I dont understand all of that but I think I need to read it a few times and let it stir in my brain for a bit. 

I've read that example for library on Arduino but then realised what a tangled mess all of my code is haha. 

So maybe I should just start with a more simple thing first till I get my head around it.

I think I need to plan out on a whiteboard how all of my different things interact so I can see what stuff I can just keep entirely within a library.

Share this post


Link to post
Share on other sites

dude, i've been doing this every day for over 10 years and my code is still a giant tangled mess haha

even if you plan, it always changes... or you just want to quickly add something to test something which often stays like that forever haha.

Share this post


Link to post
Share on other sites

"I'm going to come up a plan for my code, and stick to it" 

Says everyone ever 

Does no one ever 

  • Like 2

Share this post


Link to post
Share on other sites
On 18/12/2018 at 22:45, h4nd said:

I ran it through another editor, to get thn indenting more helpful, and added the digitalWrite you need

 


const int sensorPin1 = A1;
const int sensorPin2 = A2;
const int sensorPin3 = A3;
int sensorValue = 0;

int sensorMin = 1023;
int sensorMax = 0; 

void setup() {
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
}

void loop() {
  if (sensorPin1 >= 900);
  {
    digitalWrite(2, HIGH);
    delay(200);
    digitalWrite(2, LOW);
  }
  else 
	if (sensorPin1 <= 900);
	{
		digitalWrite(2, LOW);
	}		
  if (sensorPin2 >= 900);
  {
    digitalWrite(3, HIGH);
    delay(200);
    digitalWrite(3, LOW);
  }
  else 
	  if (sensorPin2 <= 900);
  {
    digitalWrite(3, LOW);
  }
  if (sensorPin3 >= 900);
  {
    digitalWrite(5, HIGH);
    delay(200);
    digitalWrite(5, LOW);
  }
  else if (sensorPin3 <= 900);
  {
    digitalWrite(5, LOW);
  }
}

there's great tutorial and refernces at https://www.arduino.cc/en/Tutorial/HomePage 

and a language reference at https://www.arduino.cc/reference/en/

 

Also, have a look at the built in examples: especially             File - Examples - 01.Basics - Blink

I looked at the examples and got a bunch of error codes when i tried to make it function.
 

Quote

 

very_crude_sensor_array_ver_01223:45:3: error: 'else' without a previous 'if'

   else if (sensorPin3 <= 900);

   ^

exit status 1
stray '\357' in program


 

I don't even know what a stray slashNumber is..

Pretty sure the pages of reference is where i got this unusable code from.  "How hard could it be, that guy down south made his dashboard indicator light controller in one night".  Turns out it's impossible.  And now everyone will know i'm dumber than shit & shouldn't exist.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now