Posts: 392
Threads: 51
Joined: May 2014
Reputation:
32
Sve je lepo vezano,Drugi kodovi sa adc rade sa tom vezom medjutim nekih nepunih par nedelja sam sa arduinom pa nism siguran da moze tako da razdvoji adc pomocu tastera.Po svoj logici gledajuci kod trebalo bi da radi mada probacu i sa pullup komandom iako sam stavio otpornike sa 5V na ulaze za taster i odatle taster na masu.
Posts: 120
Threads: 4
Joined: Aug 2016
Reputation:
65
10 ti red odozdo je:
if(digitalRead(taster1) == LOW)
a mozda treba da bude :
if(digitalRead(taster2) == LOW)
Posts: 392
Threads: 51
Joined: May 2014
Reputation:
32
02-05-2021, 11:34 PM
(This post was last modified: 02-05-2021, 11:35 PM by MILANNN.)
Jeste,greska u kucanju,sad se upali dioda koja treba kad se pritisne taster ali nereaguje na potenciometar,trebalo bi da se dioda pali i gasi prilikom okretanja potenciometa a ona je uvek upaljena.
const byte taster1 = 2;
const byte taster2 = 4;
const byte ledpin1 = 7;
const byte ledpin2 = 10;
const byte analog1 = A1;
const byte analog2 = A2;
int ana1 = 0;
int ana2 = 0;
void setup() {
pinMode(taster1,INPUT);
pinMode(taster2,INPUT);
pinMode(ledpin1,OUTPUT);
pinMode(ledpin2,OUTPUT);
pinMode(analog1, INPUT);
pinMode(analog2,INPUT);
digitalWrite(ledpin1,LOW);
digitalWrite(ledpin2,LOW);
}
void loop() {
if(digitalRead(taster1) == LOW)
{
ana1 = analogRead(analog1);
if(ana1 > 500)
digitalWrite(ledpin1,HIGH);
}
else
{
digitalWrite(ledpin1,LOW);
}
if(digitalRead(taster2) == LOW)
{
ana2 = analogRead(analog2);
if(ana2 > 100)
digitalWrite(ledpin2,HIGH);
}
else
{
digitalWrite(ledpin2,LOW);
}
}
Posts: 663
Threads: 40
Joined: Jul 2015
Reputation:
388
02-06-2021, 01:55 AM
(This post was last modified: 02-06-2021, 02:01 AM by me[R]a.)
Pa ukljkuci u igru Serail monitor
u setup stavis
Code: Serial.begin(115200);
pa recimo posle:
Code: ana1 = analogRead(analog1);
uradis
Code: Serial.printf("Analogna vrednost : %i",ana1);
pa ces da vidis u serijal monitoru da li pravilno citas analognu vrednost jer ako u tvom slucaju nije veca od 500 nece se led dioda nikad ni upaliti i obrnuto
Posts: 1.157
Threads: 27
Joined: Oct 2014
Reputation:
313
02-06-2021, 08:57 AM
(This post was last modified: 02-06-2021, 09:04 AM by gorankg.)
(02-05-2021, 11:34 PM)MILANNN Wrote: Jeste,greska u kucanju,sad se upali dioda koja treba kad se pritisne taster ali nereaguje na potenciometar,trebalo bi da se dioda pali i gasi prilikom okretanja potenciometa a ona je uvek upaljena.
Code: void loop() {
if(digitalRead(taster1) == LOW)
{
ana1 = analogRead(analog1);
if(ana1 > 500)
digitalWrite(ledpin1,HIGH);
}
else
{
digitalWrite(ledpin1,LOW);
}
}
LED svetli AKO je taster pritisnut i AKO je ana1 > 500. Dakle, tačno ono što si i napisao gore. Probaj ovako:
Code: void loop() {
ana1 = analogRead(analog1);
if(digitalRead(taster1) == LOW)
{
if(ana1 > 500)
digitalWrite(ledpin1,HIGH);
}
else
{
digitalWrite(ledpin1,LOW);
}
}
I ubaci ovaj Serial Monitor pa prati kolike su ti vrednosti za ana1 i ana2
Posts: 392
Threads: 51
Joined: May 2014
Reputation:
32
Sve probao i nece.Na serial monitoru menja vrednosti kad se okrece potenciometar.
int taster1 = 2;
int taster2 = 3;
int ledpin1 = 7;
int ledpin2 = 8;
const int analog1 = A1;
const int analog2 = A2;
int ana1 = 0;
int ana2 = 0;
void setup() {
Serial.begin(115200);
pinMode(taster1,INPUT);
pinMode(taster2,INPUT);
pinMode(ledpin1,OUTPUT);
pinMode(ledpin2,OUTPUT);
pinMode(analog1, INPUT);
pinMode(analog2,INPUT);
digitalWrite(ledpin1,LOW);
digitalWrite(ledpin2,LOW);
}
void loop() {
if(digitalRead(taster1) == LOW)
{
ana1 = analogRead(analog1);
if(ana1 > 500)
digitalWrite(ledpin1,HIGH);
Serial.print ("radi");
Serial.println(ana1);
}
else
{
digitalWrite(ledpin1,LOW);
Serial.print ("**");
Serial.println(ana1);
}
if(digitalRead(taster2) == LOW)
{
ana2 = analogRead(analog2);
if(ana2 > 500)
analogWrite(ledpin2,HIGH);
Serial.print ("radi");
Serial.println(ana2);
}
else
{
digitalWrite(ledpin2,LOW);
Serial.print ("##");
Serial.println(ana2);
}
}
Posts: 1.157
Threads: 27
Joined: Oct 2014
Reputation:
313
Možeš li preciznije da opišeš šta želiš da uradiš?
Posts: 392
Threads: 51
Joined: May 2014
Reputation:
32
Ovako. Kad pritisnem jedan taster, cita analogni pin i ako prekorači pali led. kad padne ispod zadatog led se gasi. Kad pustim taster, tj. oba tastera lig1 obe led ugasene. Isto i sa drugim tasterom.
Posts: 1.157
Threads: 27
Joined: Oct 2014
Reputation:
313
Ne mogu sad da probam kod ali ako te ne mrzi probaj da analogne pinove definišeš ovako:
int analog1 = A1;
int analog2 = A2;
Posts: 392
Threads: 51
Joined: May 2014
Reputation:
32
To sam probao. Nema veze, nemoj da se mucis zbog mene da ga testiras, ja cu kad imam vremena da se igram sa ovim pa kako mu bude. U svakom slucaju hvala najlepse.
Posts: 692
Threads: 8
Joined: Mar 2013
Reputation:
234
Probaj ovako:
Code: #define taster1 2
#define taster2 3
#define ledpin1 7
#define ledpin2 8
#define analog1 A1
#define analog2 A2
int ana1 = 0;
int ana2 = 0;
void setup() {
Serial.begin(115200);
pinMode(taster1, INPUT_PULLUP);
pinMode(taster2, INPUT_PULLUP);
pinMode(ledpin1, OUTPUT);
pinMode(ledpin2, OUTPUT);
pinMode(analog1, INPUT);
pinMode(analog2, INPUT);
digitalWrite(ledpin1, LOW);
digitalWrite(ledpin2, LOW);
}
void loop() {
if (digitalRead(taster1) == LOW)
{
while (!digitalRead(taster1))
{
ana1 = analogRead(analog1);
if (ana1 > 500)
{
digitalWrite(ledpin1, HIGH);
Serial.print ("radi");
Serial.println(ana1);
}
}
}
else
{
digitalWrite(ledpin1, LOW);
Serial.print ("**");
Serial.println(ana1);
}
if (digitalRead(taster2) == LOW)
{
while (!digitalRead(taster2))
{
ana2 = analogRead(analog2);
if (ana2 > 500)
{
analogWrite(ledpin2, HIGH);
Serial.print ("radi");
Serial.println(ana2);
}
}
}
else
{
digitalWrite(ledpin2, LOW);
Serial.print ("##");
Serial.println(ana2);
}
}
Make no mistake between my personality and my attitude.
My personality is who I am.
My attitude depends on who you are.
塞爾維亞文
Posts: 392
Threads: 51
Joined: May 2014
Reputation:
32
Probam sutra. Ovo mi izgledalo prosto da prostije nemoze i neznam koji mu je pa nece a trebalo mi nesto da mogu tako da biram koji cu adc da gledam kad ubasim displej.
Posts: 392
Threads: 51
Joined: May 2014
Reputation:
32
Pozdrav svima.Da pitam i ja nesto.Dosta toga sam nasao i na internetu ali nisam siguran da sam u pravu kako sam to shvatio.Evo kako su neki bitovi definisani ovako i to mi je jasno
TCCR1B |= (1 << CS12) | (1 << CS11) | (0 << CS10);
A u isto programu ima i ovo:
TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
_BV je vec definisan kao bit i daje vrednost bajta.Koliko sam ja uspeo da shvatim ali kako to ustvari radi?
evo ceo neki kod koji sam pronasao
const int led = LED_BUILTIN; // the pin with a LED
void setup(void)
{
pinMode(led, OUTPUT);
TIMSK1 |= (1 << TOIE1); // interrupt TIMER1 on
TCCR1A = 0; //normal OC1A OC1B off
TCCR1B = 0; //clear register
TCCR1B = 0; //clear register
TCCR1B |= (1 << CS12) | (1 << CS11) | (0 << CS10);
TCNT1 = 0; //clear register
Serial.begin(9600);
pinMode(5, INPUT_PULLUP);
TCCR2B = TCCR2B & B11111000 | B00000001; // for PWM frequency of 31372.55 Hz
pinMode(3, OUTPUT); // output pin for OCR2B, this is Arduino pin number
TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
TCCR2B = _BV(WGM22) | _BV(CS20);
OCR2A = 63;
OCR2B = 0;
}
unsigned long blinkCount = 0; // use volatile for shared variables
ISR(TIMER1_OVF_vect) //interrupção do TIMER1
{
blinkCount++; // increase when overflows
digitalWrite(led, !digitalRead(led));
}
uint32_t millis1;
uint32_t calcMed = 0;
void loop(void)
{
calcMed = (blinkCount * 65536) + TCNT1;
blinkCount = 0;
TCNT1 = 0;
Serial.print("Freq = ");
Serial.println(calcMed);
while ((millis1 + 1000) > millis()) {
_delay_us(5);
if ( OCR2B < 63 )
OCR2B += 5;
else
OCR2B = 0;
}
millis1 = millis();
}
Posts: 392
Threads: 51
Joined: May 2014
Reputation:
32
Pozdrav.Evo jedno pitanje uvezi komande "goto" iako se ona nepreporucuje hteo sam samo da nesto probam i sa tom komandom ali neide.Postavio sam neki kod cisto kao primer,iako znam kako bi to radilo i bez "goto" hteo sam da probam i sa "goto" ali uvek greska pa ako moze pomoc oko izmene da bi proradilo sa "goto" komandom.
int LED_PIN = 13;
int i=0;
int taster = 3;
int led = 4;
void setup () {
pinMode(LED_PIN, OUTPUT);
pinMode(led,OUTPUT);
pinMode(taster,INPUT);
}
void loop(){
for (i = 0; i < 3; i++)
{
SendS();
delay(12);
}
delay(300);
for (i = 0; i < 3; i++)
{
SendO();
delay(12);
}
delay(300);
//------------------------
if(digitalRead(taster)==LOW) {goto sad;}
//------------------------
for (i = 0; i < 3; i++)
{
SendS();
delay(12);
}
delay(5000);
}
void SendS(){
digitalWrite(LED_PIN, HIGH);
delay(120);
digitalWrite(LED_PIN, LOW);
delay(120);
return;
}
void SendO(){
digitalWrite(LED_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN, LOW);
delay(1000);
return;
}
//-------------------------------
sad: {
digitalWrite(led,HIGH);
delay(5000);
digitalWrite(led,LOW);
delay(5000);
}
//----------------------------
Posts: 692
Threads: 8
Joined: Mar 2013
Reputation:
234
Kada je C u pitanju, goto ne bi trebalo dase koristi, što si i sam napisao.
C nije BASIC tako da su strukture programa različite.
Funkcije tretiraj kao podprograme u BASIC-u (subrutine)
U finkcijama SendS i SendO ti na kraju nije potreban return; pošto iste ne vraćaju nikakvu vrednost
Code: //int LED_PIN = 13;
int i = 0;
//int taster = 3;
//int led = 4;
// Ovako je elegantnije
#define LED_PIN 13
#define taster 3
#define led 4
void setup () {
pinMode(LED_PIN, OUTPUT);
pinMode(led, OUTPUT);
pinMode(taster, INPUT);
}
void loop() {
for (i = 0; i < 3; i++)
{
SendS();
delay(12);
}
delay(300);
for (i = 0; i < 3; i++)
{
SendO();
delay(12);
}
delay(300);
//------------------------
if (digitalRead(taster) == LOW) {
// goto sad; <- neće da može
sad();
}
//------------------------
for (i = 0; i < 3; i++)
{
SendS();
delay(12);
}
delay(5000);
}
// Funkcije
void SendS() {
digitalWrite(LED_PIN, HIGH);
delay(120);
digitalWrite(LED_PIN, LOW);
delay(120);
// return;
}
void SendO() {
digitalWrite(LED_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN, LOW);
delay(1000);
// return;
}
//sad: {
void sad() {
digitalWrite(led, HIGH);
delay(5000);
digitalWrite(led, LOW);
delay(5000);
}
Make no mistake between my personality and my attitude.
My personality is who I am.
My attitude depends on who you are.
塞爾維亞文
Posts: 392
Threads: 51
Joined: May 2014
Reputation:
32
Da,tako mi radi,probao sam,istinacin kao sa SenS a ovo sam hteo samo za probu i na internetu video nekoliko primera koji izgleda nerade.Evo neki primer sa interneta koji sam sad prekopirao a ima ih bezbroj istih pa sam tako i ja probao.
void changePinState(int pin)
// if pin reads LOW, set it HIGH
// if pin reads HIGH, set it LOW
{
int state=digitalRead(pin);
if (state == HIGH) goto skip_1;
if (state == LOW) goto skip_2;
goto error;//---------------------------------------------------
skip_1:
state = LOW;
goto done;
skip_2:
state = HIGH;
goto done;//------------------------------------------------
error:
Serial.println("Internal Function Error");
done: //----------------------------------------------- stavljeno bas kao u picbasicu
digitalWrite(pin, state);
}
Posts: 692
Threads: 8
Joined: Mar 2013
Reputation:
234
I opet da ti napomenem, C nije BASIC!
Zaboravi na goto!!!
Ovaj poslednji snipet koji si postavio je teška katastrofa, mada je očigledno napisan da pokaže kako NE TREBA pisati program.
Code: void changePinState(int pin)
{
int state = digitalRead(pin);
if (state == HIGH)
{
state = LOW;
}
else
{
state = HIGH;
}
digitalWrite(pin, state);
}
Make no mistake between my personality and my attitude.
My personality is who I am.
My attitude depends on who you are.
塞爾維亞文
Posts: 8.708
Threads: 138
Joined: Apr 2013
Reputation:
3.606
@ZAsto
Postoji GOTO i u C-u, skace na labelu gde postavis, nije strasno koristiti kada se zna tacno sta treba, zgodan je za izaske iz zapetljanih for/switch u funkcijama koje nista ne vracaju (void) pa ne mozes da koristis return za izlazak nego se pred kraj funkcije stavi labela pa tamo odnekud skoci na tu labelu i regularno izadje iz funkcije.
Posts: 719
Threads: 11
Joined: Mar 2013
Reputation:
324
Moze da se koristi return u void funkcijama
return;
Posts: 392
Threads: 51
Joined: May 2014
Reputation:
32
Moze li jedan primer Miki? Ovo sa neta nista neradi.
|