locked
Ev3 color sensor with small basic RRS feed

  • Question

  • Hey im new in programming ev3 with small basic and i need your help.

    I want that if i scan colors that the small basic programm save the color sequence for sort 4 cubes with diffrent colors with a motor

    So for example if the black color is scanned first, then the programm should save the first color as black and if red color is scanned, then it should save the red color as 2nd color and so on

    Friday, December 8, 2017 2:26 PM

Answers

  • see here 

    https://sites.google.com/site/ev3basic/ev3-basic-programming/using-sensors#TOC-Color-Sensor

    maybe you want to do is below. ( I don't have EV3 )

    path=program.Directory+"\detectedcolor.txt"

    filename="\detectedcolor.txt"
    CLR="0=UNKNOWN;1=BLACK;2=BLUE;3=GREEN;4=YELLOW;5=RED;6=WHITE;7=BROWN"
    '  port 1 = touch, port 2 = gyro, port 3 = color, port 4 = infrared or ultrasonic.
    Sensor.SetMode(3,2)  '   will set color sensor on port 3 to mode 2: detect color
    While "True"
      handle=EV3File.OpenAppend (filename) ' open EV3file
      LCD.StopUpdate()    '      don't update the screen until all the text is ready
      LCD.Clear()         '  
      code=Sensor.ReadRawValue(3, 0)  '  single raw value
      LCD.Text(1,33,40, 2, "Color "+ code)  ' 
      LCD.Text(1,33,75, 2, CLR[code])   ' 
      LCD.Update()    '
      File.AppendContents(path,CLR[code])  '  save color data with append mode // to small basic
      text=CLR[code]+Text.GetCharacter(10) '
      EV3File.WriteLine (handle, text)     '  save color data to EV3 brick file
      Program.Delay(100)
      EV3File.Close (handle)               ' close EV3file
      Program.Delay(100)
    EndWhile

    * added EV3File code.  (refered below)

    https://sites.google.com/site/ev3basic/ev3-basic-programming/ev3-basic-manual


    Saturday, December 9, 2017 4:17 AM
    Answerer

All replies

  • see here 

    https://sites.google.com/site/ev3basic/ev3-basic-programming/using-sensors#TOC-Color-Sensor

    maybe you want to do is below. ( I don't have EV3 )

    path=program.Directory+"\detectedcolor.txt"

    filename="\detectedcolor.txt"
    CLR="0=UNKNOWN;1=BLACK;2=BLUE;3=GREEN;4=YELLOW;5=RED;6=WHITE;7=BROWN"
    '  port 1 = touch, port 2 = gyro, port 3 = color, port 4 = infrared or ultrasonic.
    Sensor.SetMode(3,2)  '   will set color sensor on port 3 to mode 2: detect color
    While "True"
      handle=EV3File.OpenAppend (filename) ' open EV3file
      LCD.StopUpdate()    '      don't update the screen until all the text is ready
      LCD.Clear()         '  
      code=Sensor.ReadRawValue(3, 0)  '  single raw value
      LCD.Text(1,33,40, 2, "Color "+ code)  ' 
      LCD.Text(1,33,75, 2, CLR[code])   ' 
      LCD.Update()    '
      File.AppendContents(path,CLR[code])  '  save color data with append mode // to small basic
      text=CLR[code]+Text.GetCharacter(10) '
      EV3File.WriteLine (handle, text)     '  save color data to EV3 brick file
      Program.Delay(100)
      EV3File.Close (handle)               ' close EV3file
      Program.Delay(100)
    EndWhile

    * added EV3File code.  (refered below)

    https://sites.google.com/site/ev3basic/ev3-basic-programming/ev3-basic-manual


    Saturday, December 9, 2017 4:17 AM
    Answerer
  • Thanks i will try it on monday because i dont got the ev3 brick now
    Saturday, December 9, 2017 9:44 AM
  • Ok the programm saves the color sequence in a txt file. That works fine but how can i read the file and get the sequence?

    Monday, December 11, 2017 1:13 PM
  • The standard file commands in SB start with File.  See the Intellisense.

    If you can use the LD extension, there are also a number of file commands starting with LDFile.  Choose the command which fits your needs.


    Jan [ WhTurner ] The Netherlands

    Monday, December 11, 2017 1:18 PM
    Answerer
  • Ok the programm saves the color sequence in a txt file. That works fine but how can i read the file and get the sequence?

    Hi,

    Use EV3File.ReadLine()

    or File.ReadLine()

    or File.ReadContents()

    Thanks,


    Roshan - 7th Grade Student (Age 11) - Learning Small Basic and Visual Basic


    Monday, December 11, 2017 2:23 PM
  • Ok the programm saves the color sequence in a txt file. That works fine but how can i read the file and get the sequence?

    SBdata=File.ReadContents(path)       ' Small basic file read all contents
     handle2=EV3File.OpenRead (filename)   ' EV3 File open Read mode
     EVdata=EV3File.ReadLine (handle2)     ' EV3 read a line
     TextWindow.WriteLine("The color is ... EV; "+EVdata+Text.GetCharacter(10)+" SB; "+SBdata)
     EV3File.Close (handle2)
     Program.Delay(100)
    Monday, December 11, 2017 11:42 PM
    Answerer
  • While "True"                
      Motor.Start("A",20)
      Program.Delay(100)
      code=Sensor.ReadRawValue(3, 0)
      If code = 1 Then     'First Position
        Program.Delay(100)
        Motor.Stop("A","False")
        Program.Delay(100)
        Motor.Move("B",100,70,"True")
        Program.Delay(100)
        Motor.Start("A",20)
        Program.Delay(500)
        Motor.Move("B",-35,140,"False")
        Program.Delay(500)
        Motor.Move("B",100,70,"True")
        Program.Delay(100)
      ElseIf code = 2 Then             'Second Position
        Program.Delay(500)
        Motor.Move("B",100,45,"True")
        Program.Delay(1000)
        Motor.Move("B",-100,45,"True")
      ElseIf code = 4 Then          'third Position
     Program.Delay(100)
        Motor.Move("B",-100,60,"True")
        Program.Delay(600)
        Motor.Move("B",20,60,"True")
      ElseIf code = 5 Then          'Fourth Position
        Program.Delay(100)
        Motor.Stop("A","False")
        Program.Delay(100)
        Motor.Move("B",-100,70,"True")
        Program.Delay(100)
        Motor.Start("A",20)
        Program.Delay(500)
        Motor.Move("B",60,140,"False")
        Program.Delay(100)
        Motor.Move("B",-100,70,"True")
        Program.Delay(100)
      EndIf
      EndWhile

    Is it possible that i can put something in the command code=... so i can read the diffrent line of thedetectedcolor.txt

    In this source code i just wrote the four colors but i want that the brick learn the color sequence

    Thursday, January 11, 2018 10:10 PM
  • How about this?   Color sequence is written in detectedcolor.txt.  

    ' this is written in this page   https://sites.google.com/site/ev3basic/ev3-basic-programming/using-sensors/sensor-appendix
    'Mode Name                 Get reading with        Returns
    '    0         TOUCH                  ReadPercent                0=not pressed, ?100=pressed
    '    0         COL-REFLECT     ReadPercent                0=no reflected light, 100=max
    '    1          COL-AMBIENT    ReadPercent                0=no ambient light, 100=max
    '    2         COL-COLOR          ReadRawValue           0=unknown, 1=black, 2=blue, 3=green, 4=yellow, 5=red, 6=white, 7=brown
    '    4         RGB-RAW              ReadRaw (3 vals)       value0=red intensity, value1=green intensity, value2=blue intensity
    '    0         US-DIST-CM         ReadRawValue           distance in?mm
    '    1          US-DIST-IN          ReadRawValue           distance in?tenths?of an inch
    '    0          GYRO-ANG          ReadRawValue           angle in degrees(**)
    '    1           GYRO-RATE        ReadRawValue           rate of change of angle in deg/s
    '    0          IR-PROX               ReadRawValue           distance in cm (not very accurate)
    '    1          IR-SEEK                ReadRaw (2 vals)        value0=direction?to IR beacon ,  value1=distance to IR beacon
    '    2         IR-REMOTE ReadRawValue           value0=signal from channel 1 (*), value1=signal from channel 2...
    '
    '  port 1 = touch, port 2 = gyro, port 3 = color, port 4 = infrared or ultrasonic
    path=program.Directory+"\detectedcolor.txt"
    CLR="0=UNKNOWN;1=BLACK;2=BLUE;3=GREEN;4=YELLOW;5=RED;6=WHITE;7=BROWN"
    Sensor.SetMode(3,2)  'will set color sensor on port 3 to mode 2: detect color
    NN=0

    While "True"                 
      Motor.Start("A",20)
      Program.Delay(100)
      code=Sensor.ReadRawValue(3, 0) '<-----  this returns a number (0,1,2,3,4,5,6,7) 
      If code>0 Then '  if  known color  
        NN=NN+1
        If code = 1 Then     'First Position    Note : code=1 means the color is "Black"
          Program.Delay(100)
          Motor.Stop("A","False")
          Program.Delay(100)
          Motor.Move("B",100,70,"True")
          Program.Delay(100)
          Motor.Start("A",20)
          Program.Delay(500)
          Motor.Move("B",-35,140,"False")
          Program.Delay(500)
          Motor.Move("B",100,70,"True")
          Program.Delay(100)
        ElseIf code = 2 Then             'Second Position   Note : code=2 means the color is "Blue"
          Program.Delay(500)
          Motor.Move("B",100,45,"True")
          Program.Delay(1000)
          Motor.Move("B",-100,45,"True")
        ElseIf code = 4 Then          'third Position    Note : code=4 means the color is "Yellow"
          Program.Delay(100)
          Motor.Move("B",-100,60,"True")
          Program.Delay(600)
          Motor.Move("B",20,60,"True")
        ElseIf code = 5 Then          'Fourth Position   Note : code=5 means the color is "Red"
          Program.Delay(100)
          Motor.Stop("A","False")
          Program.Delay(100)
          Motor.Move("B",-100,70,"True")
          Program.Delay(100)
          Motor.Start("A",20)
          Program.Delay(500)
          Motor.Move("B",60,140,"False")
          Program.Delay(100)
          Motor.Move("B",-100,70,"True")
          Program.Delay(100)
        EndIf
        File.AppendContents(path,NN+"th color is "+CLR[code])  '  save color data with append mode // to small basic 
      EndIf
    EndWhile


    Friday, January 12, 2018 2:18 AM
    Answerer