Help with simple programming - analog input
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- Sparkystav
- Posts: 19
- Joined: Thu May 01, 2014 10:03 am
Re: Help with simple programming - analog input
Ah! That makes sense! I suppose I guessed it would reset automatically. So I need something that tells then all to go high until it gets one of the readings?
- Sparkystav
- Posts: 19
- Joined: Thu May 01, 2014 10:03 am
Re: Help with simple programming - analog input
Ok so it will read the equivelent of '0' when not being pressed, so how do I command it to send all 4 outputs 'not low', do I wrote 4 lines of code or can it do it with one line?
- Franklin97355
- Posts: 23940
- Joined: Mon Apr 21, 2008 2:33 pm
Re: Help with simple programming - analog input
Show us the code as it stands now you've made the changes.
- Sparkystav
- Posts: 19
- Joined: Thu May 01, 2014 10:03 am
Re: Help with simple programming - analog input
Here you go:
Code: Select all
Arduino correct
/*
Steering controls correct (1024 bits?)
5v voltage differential with 1.2kohm
Menu 2.097v = 442
Select 4.54v = 929
Up 3.84v = 787
Down 3.07v = 630
White menu
Grey down
Blue up
Orange select
120ohm in series with inputs
*/
int down = 0;
int up = 1;
int analogpin = 2;
int menu = 3;
int select = 4;
void setup() {
pinMode (up, OUTPUT);
pinMode (down, OUTPUT);
pinMode (menu, OUTPUT);
pinMode (select, OUTPUT);
}
void loop()
{
int reading = analogRead(analogpin);
if ((reading <470) && (reading >430))
digitalWrite (menu, LOW);
else
if ((reading <650) && (reading >600))
digitalWrite (down, LOW);
else
if ((reading <800) && (reading >750))
digitalWrite (up, LOW);
else
if ((reading <1000 && reading >900))
digitalWrite (select, LOW);
else
If ((reading <50))
digitalwrite (menu, down, up, select, HIGH)
}
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: Help with simple programming - analog input
Code: Select all
else
if ((reading <50))
{
digitalwrite (menu, HIGH);
digitalwrite (down, HIGH);
digitalwrite (up, HIGH);
digitalwrite (select, HIGH);
}
- Sparkystav
- Posts: 19
- Joined: Thu May 01, 2014 10:03 am
Re: Help with simple programming - analog input
Thanks for that, so I do need 4 different lines. I will load that up and see how I get on.
Thank you for the help so far with this newb.
Thank you for the help so far with this newb.
- Sparkystav
- Posts: 19
- Joined: Thu May 01, 2014 10:03 am
Re: Help with simple programming - analog input
Well that has had an effect, now all the outputs sit at around 4.8v. However they still don't go low when I make the selections.
It's as though the trinket isn't understanding the inputs it's getting. Ive measured directly at the pin and it's getting the different voltages.
Im wondering if the trinket isn't understanding the commands. I've converted the 0-5v input to 0-1024 bits as the research and learning I did suggested I need to for an analog input, but is this automatically programmed in or do I need to make the trinket understand that? Does programming analogread automatically assume its a 0-5v input?
Thanks
It's as though the trinket isn't understanding the inputs it's getting. Ive measured directly at the pin and it's getting the different voltages.
Im wondering if the trinket isn't understanding the commands. I've converted the 0-5v input to 0-1024 bits as the research and learning I did suggested I need to for an analog input, but is this automatically programmed in or do I need to make the trinket understand that? Does programming analogread automatically assume its a 0-5v input?
Thanks
- adafruit_support_bill
- Posts: 88152
- Joined: Sat Feb 07, 2009 10:11 am
Re: Help with simple programming - analog input
On a 5v Trinket, the analog reference voltage is 5v, so all analog readings are scaled based on 0-5v.
- Sparkystav
- Posts: 19
- Joined: Thu May 01, 2014 10:03 am
Re: Help with simple programming - analog input
Hmm struggling with this then!
I'll keeping thinking
I'll keeping thinking
- adafruit_support_bill
- Posts: 88152
- Joined: Sat Feb 07, 2009 10:11 am
Re: Help with simple programming - analog input
Are you sure you have the right pins? It can be a bit confusing with all the multi-purpose pins on the Trinket. Note that analog pin 2 is also digital pin 4 and is labeled #4 on the board.
- Sparkystav
- Posts: 19
- Joined: Thu May 01, 2014 10:03 am
Re: Help with simple programming - analog input
I'm using #2 as the analog pin, which is analog 1, so what do I call it then at the beginning?
Int analogpin = 1;
Also I've just noticed I haven't set an input in the setup part. So I should add
Pinmode(analogpin, INPUT);
As well? Will try that in the morning.
Int analogpin = 1;
Also I've just noticed I haven't set an input in the setup part. So I should add
Pinmode(analogpin, INPUT);
As well? Will try that in the morning.
- adafruit_support_bill
- Posts: 88152
- Joined: Sat Feb 07, 2009 10:11 am
Re: Help with simple programming - analog input
From the Trinket Guide: https://learn.adafruit.com/introducing-trinket/pinouts
Note the numbering of analog pins: Pin 2 is Analog 1, Pin 3 is Analog 3, Pin 4 is Analog 2. For the Uno, the terms A1, A2, and A3 are mapped for you. For ATtiny85's, they are not. So for the pinMode calls, use the Pin number (stenciled on Trinket), for analogRead, use the analog number.
- Sparkystav
- Posts: 19
- Joined: Thu May 01, 2014 10:03 am
Re: Help with simple programming - analog input
Right, I'm still massively struggling with this. I've even bought a new trinket board and wired it all up on a test board so it's not dodgy soldering and there's no joined tracks, but still won't work with all four programmed in.
I've gone back to basics and can get it working on channel at a time programmed in, so if I just read the input and output to just one output then it works. But once I program them all in it doesn't work.
I've also removed the pin names as it seems to confuse things when I'm trying to understand on the fly.
Here's the code I'm currently working on, but Im beginning to think the Trinket just isn't up to the job of 4 outputs and an Input.
I've gone back to basics and can get it working on channel at a time programmed in, so if I just read the input and output to just one output then it works. But once I program them all in it doesn't work.
I've also removed the pin names as it seems to confuse things when I'm trying to understand on the fly.
Here's the code I'm currently working on, but Im beginning to think the Trinket just isn't up to the job of 4 outputs and an Input.
Code: Select all
/*
Steering controls 1024 bits
5v voltage differential with 1.2kohm to ground
120ohm in series with input
White #3 Menu 0ohms = 4.54v = 929
Orange #4 Select 1540 = 2.097v = 442
Blue #1 Up 240 = 3.84v = 787
Grey #0 Down 630 = 3.07v = 630
*/
void setup() {
pinMode (1, OUTPUT);
pinMode (0, OUTPUT);
pinMode (3, OUTPUT);
pinMode (4, OUTPUT);
pinMode (2, INPUT);
}
void loop()
{
int reading = analogRead(1);
if ((reading <470) && (reading >430))
digitalWrite (4, LOW);
else
if ((reading <650) && (reading >600))
digitalWrite (0, LOW);
else
if ((reading <800) && (reading >750))
digitalWrite (1, LOW);
else
if ((reading <1000 && reading >900))
digitalWrite (3, LOW);
else
if ((reading <50))
digitalWrite (4, HIGH);
digitalWrite (3, HIGH);
digitalWrite (1, HIGH);
digitalWrite (0, HIGH);
}
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: Help with simple programming - analog input
See if this does what you want:
Code: Select all
/*
Steering controls 1024 bits
5v voltage differential with 1.2kohm to ground
120ohm in series with input
White #3 Menu 0ohms = 4.54v = 929
Orange #4 Select 1540 = 2.097v = 442
Blue #1 Up 240 = 3.84v = 787
Grey #0 Down 630 = 3.07v = 630
*/
void setup() {
pinMode (1, OUTPUT);
pinMode (0, OUTPUT);
pinMode (3, OUTPUT);
pinMode (4, OUTPUT);
pinMode (2, INPUT);
}
void loop()
{
int reading = analogRead(1);
if ((reading <50))
{
digitalWrite (4, HIGH);
digitalWrite (3, HIGH);
digitalWrite (1, HIGH);
digitalWrite (0, HIGH);
}
else {
if (reading <470) {
digitalWrite (4, LOW);
digitalWrite (3, HIGH);
digitalWrite (1, HIGH);
digitalWrite (0, HIGH);
}
else {
if (reading <650) {
digitalWrite (0, LOW);
digitalWrite (4, HIGH);
digitalWrite (3, HIGH);
digitalWrite (1, HIGH);
}
else {
if (reading <800) {
digitalWrite (1, LOW);
digitalWrite (4, HIGH);
digitalWrite (3, HIGH);
digitalWrite (0, HIGH);
}
else {
if (reading <1000) {
digitalWrite (3, LOW);
digitalWrite (4, HIGH);
digitalWrite (1, HIGH);
digitalWrite (0, HIGH);
}
}
}
}
}
}
- Sparkystav
- Posts: 19
- Joined: Thu May 01, 2014 10:03 am
Re: Help with simple programming - analog input
Cheers for that, it's a different way of looking at it I hadn't thought off! Will load it up tomorrow and see how I get on.
Thank you
Thank you
Please be positive and constructive with your questions and comments.