Hello,
My name is aad. i wanna ask about the coding for the arduino. i wanna make some programs that will count number from 1-10 and loop the number. anyone have the program or knows how to make it?
i appreciate it.
thank you
Hello,
My name is aad. i wanna ask about the coding for the arduino. i wanna make some programs that will count number from 1-10 and loop the number. anyone have the program or knows how to make it?
i appreciate it.
thank you
Welcome to the forum
It sounds like you need a for loop
Perhaps you could do some research on for loops in C++, which is the programming language used by the Arduino and/or look at the ForLoopIteration example in the IDE
You might need to explain the why part of your request if you want to achieve anything other than a bit of code going round in circles
What does the and mean?.... start again from 1?
yes, right. may you help me?
Post#2 gave you the answer: use a for() loop (reference link in post #3) inside loop(). The for() will do the counting, and since it's inside loop() it will start over and run until you get bored.
This forum works best when a user tries something and needs help with it
What have you tried ?
#include <SPI.h>
#include <Ethernet.h>
#include "Mudbus.h"
Mudbus Mb;
//Function codes 1(read coils), 3(read registers), 5(write coil), 6(write register)
//signed int Mb.R[0 to 125] and bool Mb.C[0 to 128] MB_N_R MB_N_C
//Port 502 (defined in Mudbus.h) MB_PORT , kalo mau ganti port edit di library
int trig = 2; // membuat varibel trig yang di set ke-pin 3
int echo = 3; // membuat variabel echo yang di set ke-pin 2
long durasi, jarak, level;
int sensorPin = A0;
float volt;
float ntu;
void setup()
{
uint8_t mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x51, 0x06 };// jika membuat arduino banyak dalam 1 jaringan, mac addres silahkan di edit
uint8_t ip[] = { 192, 168, 1, 172 };// ip arduino jangan sama dengan Ip laptop/pc
uint8_t gateway[] = { 0, 0, 0, 0 };
uint8_t subnet[] = { 255, 255, 255, 0 };
Ethernet.begin(mac, ip, gateway, subnet);
//Avoid pins 4,10,11,12,13 when using ethernet shield
delay(5000);
Serial.begin(9600);
pinMode(trig, OUTPUT); // set pin trig menjadi OUTPUT
pinMode(echo, INPUT); // set pin echo menjadi INPUT
Serial.begin(9600); // digunakan untuk komunikasi Serial dengan komputer
}
void loop()
{
// program dibawah ini agar trigger memancarakan suara ultrasonic
digitalWrite(trig, LOW);
delayMicroseconds(8);
digitalWrite(trig, HIGH);
delayMicroseconds(8);
digitalWrite(trig, LOW);
delayMicroseconds(8);
durasi = pulseIn(echo, HIGH); // menerima suara ultrasonic
jarak = (durasi / 2) / 29.1; // mengubah durasi menjadi jarak (cm)
level = 30 - jarak;
Serial.print("Jarak = ");
Serial.println(jarak);
delay (200);
Serial.print("Level = ");
Serial.println(level); // menampilkan jarak pada Serial Monitor
delay (200);
volt = 0;
for(int i=0; i<800; i++)
{
volt += (((float)analogRead(sensorPin)- 50.68)/1023)5;
}
volt = volt/800;
volt = round_to_dp(volt,2);
if(volt < 2.5){
ntu = 3000;
}else{
ntu = -1120.4square(volt)+5742.3*volt-4353.8;
}
Serial.print((float)analogRead(sensorPin));
Serial.println(" x");
Serial.print(volt);
Serial.println(" V");
Serial.print(ntu);
Serial.println(" NTU");
delay(10);
Mb.Run();
//Analog inputs 0-1023 = holding register addres mulai dari 40000-40002
Mb.R[0] = (level); //pin A0 to Mb.R[0]
Mb.R[1] = (ntu);
Mb.R[2] = analogRead(A2);
//Analog outputs 0-255 = holding register addres mulai dari 40006
analogWrite(6, Mb.R[6]); //pin ~6 from Mb.R[6]
//Digital inputs = coil status addres mulai dari 00007, kalo mau kontrol kasih ON
Mb.C[7] = digitalRead(7); //pin 7 to Mb.C[7]
//Digital outputs = coil status addres mulai dari 00008
digitalWrite(8, Mb.C[8]); //pin 8 from Mb.C[8]
}
float round_to_dp( float in_value, int decimal_place )
{
float multiplier = powf( 10.0f, decimal_place );
in_value = roundf( in_value * multiplier ) / multiplier;
return in_value;
}
i wanna make the counter at the bold coding above.
i was tried the function for(int count = 0; count<0; count =count+1)
Thank you for posting the code. Where did you get it from ?
Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'
Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination
It looks like you pasted the first code you found on the internet.
What does this dozens of lines have to do with your question?
Isn't it better to start by trying to write the counter you need yourself?
i wanna include the counter program in my code (above).
i just wanna ask how to include it and whats the code that i type in the program/my code
It is not customary on the forum to write code for others. It will be better if you can try it yourself.
First just the counter code in few lines and test it. If it will be works fine - next start to insert it in your program.
Does the sketch work as it is and what does it do ?
Is this what you mean? Seems way too obvious:
Mb.R[2] = analogRead(A2);
for (int i = 0; i <= 10; i++) { } // <<<<<<<<<<<<<<< new
//Analog outputs 0-255 = holding register addres mulai dari 40006
Why do you want to do this count? Is it supposed to be some kind of delay or what? Do you want to display the counter, or what?
i've did it, but can't.
i want to make it like " Mb.R[2] = count 1-10;"
but i dont know how to include it.
i want the number do counting on that adress
fyi : Mb.R [2] is holding register adress for plc
Sorry to say I don't know what you're asking.
The array location Mb.R[2]
can only hold one value at. time. Did you want this line to cycle through the values 1, 2, ... 10 each time it is executed?
static int count = 1;
Mb.R[2] = count;
count++;
if (count > 10)
count = 1;
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.