Show Posts
Pages: 1 [2] 3 4
16  Using Arduino / Programming Questions / Re: SD card on: October 08, 2012, 11:32:21 am
PaulS, Thanks smiley
I´ll try this now!
If it´s not working, I´ll post it here smiley
17  Using Arduino / Programming Questions / Re: SD card on: October 08, 2012, 11:17:23 am
Hi Guys! Of course it cannot log when the card is not present...
But after startup, I insert a SD card. Then something is happening... I really dont know what...
To be more spesific, It seams to arduino freezes...

I want to start logging when "Card detection" is high. When SD card is present...

*******************************************************************************************************************
***********************   Code  **********************************************************************************
*******************************************************************************************************************
#include <math.h>
#include <Wire.h>
#include <SD.h>

const int ThermistorValue   = 10000;  // 10kohm resistor
const int chipSelect        = 10;     //
const int CD                = 9;      //  Card Detect

int ledPin                  = 8;
int CDState                 = 0;

void setup() {
  Serial.begin(9600);
 
  pinMode(CD, INPUT);
  pinMode(ledPin, OUTPUT);
 
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  pinMode(10, OUTPUT);
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {Serial.println("Card failed, or not present");}
}

void loop() {
 
  CDState = digitalRead(CD);
  int Temp = Thermister(analogRead(0));
 
  if(CDState==1){
   
    digitalWrite(ledPin, LOW);
    String dataString = String(Temp);
    File dataFile = SD.open("datalog3.txt", FILE_WRITE);
   
    if (dataFile) {
      dataFile.println(dataString);
      dataFile.close();
      // print to the serial port too:
      Serial.println(dataString);
    }
  }
  else
  {
    digitalWrite(ledPin, HIGH);
    Serial.println("Initializing SD card...");
  }
 
 
  delay(1000);
}

double Thermister(int RawADC) {
  double k;
  double c;
  k = log(((10240000/RawADC) - ThermistorValue));
 
  k = 1 / (0.001129148 + (0.000234125 * k) + (0.0000000876741 * k * k * k));
  c = k - 273.15;
  return c;
}


*******************************************************************************************************************
***********************   Code  **********************************************************************************
*******************************************************************************************************************
18  Using Arduino / Programming Questions / SD card on: October 08, 2012, 10:22:53 am
Hi all.
There are several examples on how to log onto a SD card.
My problem is that I have my arduino, I mounted a MC SD card holder from sparkfun. When I startup the board empty, I cannot log.
It seams to not recognice the card.
If I have inserted a SD card, and then startup, It all works fine...

Is there a interupt to fix this?

Best,
Andy smiley
19  Using Arduino / Microcontrollers / Atmel on: August 19, 2012, 06:55:46 pm
Hi all.
Now I've been working with arduino for some years now. I need to go one step further now...
How could I make my own cards, my own design and then program it?

Andy  smiley-cool
20  Using Arduino / Displays / Touch screen on: August 13, 2012, 05:25:32 am
Hi all.
I wanna make a touch screen editing setpoints. Press on a button on a screen, pop up a new screen, and edit setpoints.

Annyone have a clue on what type of sreen to choose and the touch screen?
How to do this, an so on...

Best,
Andy
21  Using Arduino / Programming Questions / Arduino.h on: April 01, 2012, 07:45:48 am
Hi all.
Now I´m getting really good in C. And now I wanna try too make my own products. So I started with a plain atmega32 chip.
But programming without arduino, is harder than I thought. Cause basic input and output is different than arduino.
When I use pinMode(Pin, INPUT); it´s kind of a function to set up the input pin. example:
PINB = 0b&01000000. This set up the input 2 as hight.

So my question. How is the pinMode, digitalread, digitalwrite ++++ is made?
22  Using Arduino / Programming Questions / Re: compare a string to an array on: February 23, 2011, 05:42:18 pm
Thanks guys!
I could use this script to have a simmilare function to left(),right() ...
Just use xxxx.length(), and then you could play with the string as you like too.

So thanks guys smiley
23  Using Arduino / Programming Questions / Re: Serial.print(); on: February 23, 2011, 02:02:39 pm
Hi robtillaart.

Arduino code:
Code:
void printSerialString (){
  if(serInIndx > 0){
     String myData;
     //Serial.println(serInIndx);                                // serInIndx er antall tall / bokstaver som er sendt i seriellport.
                                                                 // serInString er text'en i serial data!
     for(serOutIndx=0; serOutIndx < serInIndx; serOutIndx++){    //loop through all bytes in the array and print them out
       myData += serInString[serOutIndx];
     }
     
     //Serial.print(myData);Serial.print(myData);
     
  int myDataLength = myData.length();
  String myPrefix  = myData.substring(0,3);
  if (myData.substring(0,1) == "k") {
    /* her leses input fra serieldata av, og behandler disse dataene */
    if (myPrefix == "k1:") {
      String value = myData.substring(3,myDataLength);
      String k1 = value;
*******************************************************************************************************
I need to print twice for value appear in VB "k1.Text = myText(i) & PercentPrefix"

      Serial.print(myPrefix);
      Serial.print(k1);
      Serial.print(myPrefix);
      Serial.print(k1);
*******************************************************************************************************     
     Serial.println();
     myData = "";
     serOutIndx = 0;
     serInIndx  = 0;
   
   }
}

VB code:
Code:
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        ReceivedText(SerialPort1.ReadExisting())    'Automatically called every time a data is received at the serialPort
        Thread.Sleep(1000)
    End Sub

    Private Sub ReceivedText(ByVal [text] As String)
        'compares the ID of the creating Thread to the ID of the calling Thread
        If Me.rtbReceived.InvokeRequired Then
            Dim x As New SetTextCallback(AddressOf ReceivedText)
            Me.Invoke(x, New Object() {(text)})
        Else
            Me.rtbReceived.Text &= [text]
        End If

        Dim myFile As String = [text]
        Dim aryTextFile() As String
        myFile = Replace(myFile, " ", "")
        aryTextFile = myFile.Split(",")

        'Sett inn sensorene her
        Dim i As Integer
        For i = 0 To UBound(aryTextFile)
            Dim countTextChar(i) As Integer
            Dim myLeft(i) As String
            Dim myInput(i) As String
            Dim myText(i) As String

            countTextChar(i) = Len(aryTextFile(i))
            myInput(i) = Microsoft.VisualBasic.Left(aryTextFile(i), 1)
            myLeft(i) = Microsoft.VisualBasic.Left(aryTextFile(i), 3)

            If myInput(i) = Chr(107) Then
                If myLeft(i) = "k1:" Then
                    myText(i) = Replace(aryTextFile(i), myLeft(i), "")
                    k1.Text = myText(i) & PercentPrefix
                End If
                If myLeft(i) = "k2:" Then
                    myText(i) = Replace(aryTextFile(i), myLeft(i), "")
                    k2.Text = myText(i) & PercentPrefix
                End If
            End If

            If myInput(i) = Chr(115) Then
                If myLeft(i) = "s1:" Then
                    myText(i) = Replace(aryTextFile(i), myLeft(i), "")
                    s1.Text = myText(i) & TempPrefix
                End If
                If myLeft(i) = "s2:" Then
                    myText(i) = Replace(aryTextFile(i), myLeft(i), "")
                    s2.Text = myText(i) & TempPrefix
                End If
            End If
        Next i

    End Sub

I think this is strange...
24  Using Arduino / Programming Questions / Serial.print(); on: February 23, 2011, 09:36:47 am
Hi.
I have a really strange question.
Inside a function I have in my program, I have a "Serial.print(value);".
I send this value via serial port to av vb soft.
Here come the strange part:
I get the value in a serial window. But when I use the data to appear in a textbox, I need to send the data like:
"Serial.print(value);Serial.print(value);".
I need to send the data two times for this to work.

Annyone?
-Andy
25  Using Arduino / Programming Questions / Re: programming serial.data on: February 21, 2011, 03:59:35 pm
Hmmm....
I thought I was confused earlier...! But when I wrote "Serial.print(myData); Serial.print(myData);", VB could read the data!

Code:
void printSerialString (){
  if(serInIndx > 0){
    /*
     String ab(serInString);
     Serial.print(ab);
     Serial.println();
     */
 
   
     String ab;
     //Serial.println(serInIndx);                                // serInIndx er antall tall / bokstaver som er sendt i seriellport.
                                                                 // serInString er text'en i serial data!
     for(serOutIndx=0; serOutIndx < serInIndx; serOutIndx++){    //loop through all bytes in the array and print them out
       ab += serInString[serOutIndx];
     }
     
     String myData;
     myData = ab;
     
     Serial.print(myData); Serial.print(myData);
     Serial.println();
     myData = "";
     serOutIndx = 0;
     serInIndx  = 0;
   
   }
}

When I did this, I got value for "k1"
26  Using Arduino / Programming Questions / Re: programming serial.data on: February 21, 2011, 03:25:44 pm
Hi PaulS.
I'm no c code guru. I've merged from PLC programming. Cause I'm curious aboute how things inside  PLC are...
And I love creating my own stuff...

Here is my VB test program.
Code:
Imports System
Imports System.Threading
Imports System.IO
Imports System.IO.Ports

Public Class frmMain
    Dim TempPrefix As String = " °C"
    Dim PercentPrefix As String = " %"
    Dim myPort As Array                                     'COM Ports detected on the system will be stored here
    Delegate Sub SetTextCallback(ByVal [text] As String)    'Added to prevent threading errors during receiveing of data

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'When our form loads, auto detect all serial ports in the system and populate the cmbPort Combo box.
        myPort = IO.Ports.SerialPort.GetPortNames() 'Get all com ports available
        cmbBaud.Items.Add(9600)     'Populate the cmbBaud Combo box to common baud rates used
        cmbBaud.Items.Add(19200)
        cmbBaud.Items.Add(38400)
        cmbBaud.Items.Add(57600)
        cmbBaud.Items.Add(115200)

        For i = 0 To UBound(myPort)
            cmbPort.Items.Add(myPort(i))
        Next
        cmbPort.Text = cmbPort.Items.Item(0)    'Set cmbPort text to the first COM port detected
        cmbBaud.Text = cmbBaud.Items.Item(0)    'Set cmbBaud text to the first Baud rate on the list

        btnDisconnect.Enabled = False           'Initially Disconnect Button is Disabled

        valueTextBox()
    End Sub
    Private Sub frmMain_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        If SerialPort1.IsOpen() Then
            SerialPort1.Close()
        End If
    End Sub

    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        SerialPort1.PortName = cmbPort.Text         'Set SerialPort1 to the selected COM port at startup
        SerialPort1.BaudRate = cmbBaud.Text         'Set Baud rate to the selected value on

        'Other Serial Port Property
        SerialPort1.Parity = IO.Ports.Parity.None
        SerialPort1.StopBits = IO.Ports.StopBits.One
        SerialPort1.DataBits = 8            'Open our serial port
        SerialPort1.Open()

        btnConnect.Enabled = False          'Disable Connect button
        btnDisconnect.Enabled = True        'and Enable Disconnect button

        ' Set the read/write timeouts
        SerialPort1.ReadTimeout = 500
        SerialPort1.WriteTimeout = 500

    End Sub

    Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
        SerialPort1.Close()             'Close our Serial Port

        btnConnect.Enabled = True
        btnDisconnect.Enabled = False
    End Sub
'************************************************************************************************************
'this is where I get my serial data

    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        ReceivedText(SerialPort1.ReadExisting())    'Automatically called every time a data is received at the serialPort
        Thread.Sleep(1000)
    End Sub

    Private Sub ReceivedText(ByVal [text] As String)
        'compares the ID of the creating Thread to the ID of the calling Thread
        If Me.rtbReceived.InvokeRequired Then
            Dim x As New SetTextCallback(AddressOf ReceivedText)
            Me.Invoke(x, New Object() {(text)})
        Else
            Me.rtbReceived.Text &= [text]
        End If
'************************************************************************************************************
'this is where I get handle my serial data

        Dim myFile As String = [text]
        Dim aryTextFile() As String
        myFile = Replace(myFile, " ", "")
        aryTextFile = myFile.Split(",")

        'Sett inn sensorene her
        Dim i As Integer
        For i = 0 To UBound(aryTextFile)
            Dim countTextChar(i) As Integer
            Dim myLeft(i) As String
            Dim myInput(i) As Char
            Dim myText(i) As String

            countTextChar(i) = Len(aryTextFile(i))
            myInput(i) = Microsoft.VisualBasic.Left(aryTextFile(i), 1)
            myLeft(i) = Microsoft.VisualBasic.Left(aryTextFile(i), 3)

            Dim test As String = myInput(i)
'************************************************************************************************************
'this is where I get my k value.

            If test = Chr(107) Then
                MsgBox("k")
            End If

            If myInput(i) = Chr(107) Then
                If myLeft(i) = "k1:" Then
                    myText(i) = Replace(aryTextFile(i), myLeft(i), "")
                    k1.Text = myText(i) & PercentPrefix
                End If
            End If
'************************************************************************************************************
            If myInput(i) = Chr(115) Then
                If myLeft(i) = "s1:" Then
                    myText(i) = Replace(aryTextFile(i), myLeft(i), "")
                    s1.Text = myText(i) & TempPrefix
                End If
                If myLeft(i) = "s2:" Then
                    myText(i) = Replace(aryTextFile(i), myLeft(i), "")
                    s2.Text = myText(i) & TempPrefix
                End If
            End If
        Next i

    End Sub

    Private Sub cmbPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPort.SelectedIndexChanged
        If SerialPort1.IsOpen = False Then
            SerialPort1.PortName = cmbPort.Text                         'pop a message box to user if he is changing ports
        Else                                                            'without disconnecting first.
            MsgBox("Valid only if port is Closed", vbCritical)
        End If
    End Sub

    Private Sub cmbBaud_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBaud.SelectedIndexChanged
        If SerialPort1.IsOpen = False Then
            SerialPort1.BaudRate = cmbBaud.Text                             'pop a message box to user if he is changing baud rate
        Else                                                                'without disconnecting first.
            MsgBox("Valid only if port is Closed", vbCritical)
        End If
    End Sub

    Private Sub cmbTrackBar_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbTrackBar.Scroll
        cmbTrackBar.Orientation = Orientation.Horizontal
        cmbTrackBar.Minimum = 0
        cmbTrackBar.Maximum = 255
        'cmbTrackBar.

        Dim TrackBar1
        TrackBar1 = cmbTrackBar.Value
        Label3.Text = TrackBar1 & " %"
    End Sub

    Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click
        SerialPort1.Write("Text fra data: " & rtbSend.Text & ",")
    End Sub

    Private Sub cmdClose_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
        Me.Close()
        If SerialPort1.IsOpen() Then
            SerialPort1.Close()
        End If
    End Sub

    Private Sub cmbTrackBar_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cmbTrackBar.MouseUp
        Dim TrackBar1
        TrackBar1 = cmbTrackBar.Value

        'SerialPort1.Write("k1:" & TrackBar1.ToString & "," & vbCr)
        SerialPort1.Write("k1:" & TrackBar1.ToString & ",")
    End Sub
    Public Sub valueTextBox()
        With k1
            .Font = New Font("Microsoft Sans Serif", 8.0!, FontStyle.Bold)
            .ForeColor = Color.DarkBlue
        End With
        With s1
            .Font = New Font("Microsoft Sans Serif", 8.0!, FontStyle.Bold)
            .ForeColor = Color.Black
        End With
        With s2
            .Font = New Font("Microsoft Sans Serif", 8.0!, FontStyle.Bold)
            .ForeColor = Color.Black
        End With
    End Sub

    Private Sub rtbSend_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles rtbSend.MouseDown
        rtbSend.Text = ""
    End Sub
End Class


But should I write "String ab(serInString);" inside my printSerialString()?
-Andy
27  Using Arduino / Programming Questions / Re: programming serial.data on: February 21, 2011, 02:30:57 pm
Hmm...
I've tested maaaaaaaaaaaany things now.

When I use this code:
Code:
void printTemperature (){
  Serial.print("s1:");
  Serial.print(int(Thermistor(sensorValue1)));
  Serial.print(", ");
 
  Serial.print("k1: 200,");
  Serial.println();
}
I get my k1 value, but when I call it like this:
Code:
void printSerialString(){
   if(serInIndx > 0){
     String ab;
     //Serial.println(serInIndx);    // serInIndx er antall tall / bokstaver som er sendt i seriellport.
                                             // serInString er texten i serial data!
     
     //loop through all bytes in the array and print them out
     for(serOutIndx=0; serOutIndx < serInIndx; serOutIndx++){
       ab += serInString[serOutIndx];
     }
     
     String myData;
     myData = ab;
     
     Serial.println(myData);
     Serial.println("k1: 234,");------------------------> I dont get this code. It appears in serial monitor,
                                                                      but in VB I can't read it!!!!
     
     myData = "";
     serOutIndx = 0;
     serInIndx  = 0;
   }
}

Anyone understand why?
I'm confused...!

-Andy
28  Using Arduino / Programming Questions / Re: programming serial.data on: February 21, 2011, 08:10:16 am
Thanks PaulS.
I'll try this out.
29  Using Arduino / Programming Questions / Re: programming serial.data on: February 21, 2011, 07:14:34 am
The problem is I can't figure out how to "grab" the data I want. Like in html i use request.querystring("").
Cause when I send some values from VB, I don't know how to deal with the recieved data in Arduino side.
The data I recieve, is like:
"k1: 214," or "k4: 23," or "t1: on-07:00#off-23:00" or "w6: on,".
K is from k1-k20, w is from w1-w20, z is from z1-z6.

So what I need to do is:
Grab "k4: 23,", split in array if more values after ",". Replace " " to "". count letters, "5"("k4:23"), grab 3 first letters "k4:", then the rest "23".
Now I have something I can use in a if(), like
Code:
if (first letter == "k"){
    if (first 3 letters =="k4"{
    int k4value;
    store in something so I coule use it later somewhere
    }
}

I hope this was understandable.

-Andy
30  Using Arduino / Programming Questions / Re: programming serial.data on: February 21, 2011, 06:43:42 am
Ok.

Code:
#include <math.h>

int ledPin        = 10;     // the number of the LED pin
int brightness    = 0;      // how bright the LED is
int buttonPin2    = 2;      // the number of the pushbutton pin
int sensorValue1  = 0;      // variable to store the value coming from the sensor
int sensorValue2  = 0;      // variable to store the value coming from the sensor
int sensorPin1    = A0;     // select the input pin for the potentiometer
int sensorPin2    = A1;     // select the input pin for the potentiometer

int serIn;              // var that will hold the bytes-in read from the serialBuffer
char serInString[1000]; // array that will hold the different bytes  100=100characters;
                        // -> you must state how long the array will be else it won't work.
int serInIndx  = 0;     // index of serInString[] in which to insert the next incoming byte
int serOutIndx = 0;     // index of the outgoing serInString[] array;

//Oppsett for temperatur
double Thermistor(int RawADC) {
  double Temp;
  Temp  = log(((10240000/RawADC) - 10000));
  Temp  = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
  Temp  = (Temp - 273.15);
  return Temp;
}

void readTemperature () {
  sensorValue1 = analogRead(sensorPin1);
  sensorValue2 = analogRead(sensorPin2);
}
 
void readSerialString () {
    char sb;   
    if(Serial.available()) {
       //Serial.print("reading Serial String: ");     //optional confirmation
       while (Serial.available()){
          sb = Serial.read();
         
          serInString[serInIndx] = sb;
          serInIndx++;
          //Serial.println(sb);                        //optional confirmation
       }
       //Serial.println(sb);
    } 
}
void printTemperature (){
 
  Serial.print("s1: ");
  Serial.print(int(Thermistor(sensorValue1)));
  Serial.print(", ");
         
  Serial.print("s2: ");
  Serial.print(int(Thermistor(sensorValue2)));
  Serial.print(", ");
 
  Serial.println();
}
 
void printSerialString() {
   if(serInIndx > 0){
      //Serial.print("You said: ");     
     
      //loop through all bytes in the array and print them out
      for(serOutIndx=0; serOutIndx < serInIndx; serOutIndx++){
        Serial.print(serInString[serOutIndx]);    //print out the byte at the specified index
        //serInString[serOutIndx] = "";            //optional: flush out the content
      }
     
      //reset all the functions to be able to fill the string back with content
      serOutIndx = 0;
      serInIndx  = 0;
      //serInString er texten i serial data!
     
      remove(serInString, ' ');     
      char * token = strtok(serInString, ",");
     
      int i = 0;
      while(token){              // Do something with this word

      Serial.print(token);       // print as an ASCII-encoded decimal
      Serial.print(", ");
      // Lengden av stringe strlen (token).
     
      token = strtok(NULL, ",");
      i +=1;
      //Serial.print(", ");
      }
   }
}

void setup() {
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(sensorPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin2, INPUT);
}

void loop(){
  readTemperature();
  readSerialString();
  printTemperature();
  printSerialString();

    delay(1000);
    // *******  End Program  *******
}

void remove(char *str, char r){
  char *p, *q;
  p = q = str;
 
  while (*p != '\0')
  {
    while (*p == r) p++;
    *q++ = *p++;
  }
  *q = '\0';
}

What I wanna do with this, is to send values from arduino to Visual Basic and show the values in forms.
Then I'll send values from VB to arduino like LED on off, LED fade in out, setpoint to later use to example panel heaters.

Pages: 1 [2] 3 4